Feature 18: HA Integration as API Client (#173)

Co-authored-by: dev_agent <17+dev_agent@code.cubecraftcreations.com>
Co-committed-by: dev_agent <17+dev_agent@code.cubecraftcreations.com>
This commit was merged in pull request #173.
This commit is contained in:
2026-09-23 09:19:48 -04:00
committed by approver_agent
parent d78ef196d0
commit 6059a67122
79 changed files with 4874 additions and 7457 deletions
+131 -120
View File
@@ -1,50 +1,85 @@
"""Tests for the per-child binary sensors (Feature 10, task 4)."""
"""Tests for the per-child binary sensors (Feature 10, task 4).
The snapshot comes from the API service's panel route (the presence
engine resolves server-side), so these tests script the route's
payload through :class:`conftest.StubSnapshotClient` instead of
seeding a local database.
"""
from __future__ import annotations
import datetime
from conftest import wire_entry_to_registry
from conftest import StubSnapshotClient, set_coordinator_client, wire_entry_to_registry
from custom_components.nestquest import async_setup_entry
from custom_components.nestquest.children import create_child, list_children
from custom_components.nestquest.const import DOMAIN
from custom_components.nestquest.dao_presence import PresenceSchedulesDao
from custom_components.nestquest.materialize import materialize
from custom_components.nestquest.quest_definitions import (
create_quest_definition,
)
from custom_components.nestquest.recurrence import ScheduleRule
from custom_components.nestquest.completion import complete_instance
SEED_HORIZON_DAYS = 3
def _binary(hass, child_id: int, kind: str):
return hass.entities[f"{DOMAIN}_child_{child_id}_{kind}"]
async def _setup_entry(hass, make_entry):
entry = wire_entry_to_registry(make_entry(), hass.registry)
assert await async_setup_entry(hass, entry) is True
return entry
def _child(
child_id: int,
name: str,
*,
present=True,
next_present=None,
instances=(),
):
completed = sum(1 for i in instances if i["state"] == "completed")
due = len(instances)
return {
"child_id": child_id,
"child_name": name,
"present": present,
"next_present": next_present,
"due_today": due,
"completed_today": completed,
"remaining_today": due - completed,
"completion_pct": 100 if due == 0 else round(completed / due * 100),
"instances": list(instances),
}
async def _seed_two_children(database) -> tuple:
ada = await create_child(database, "Ada")
bo = await create_child(database, "Bo")
today = datetime.date.today()
await create_quest_definition(
database,
"Brush teeth",
ScheduleRule.from_dict(
{"rule_type": "daily", "start_date": today.isoformat()}
def _instance(instance_id: int, child_id: int, *, state="open") -> dict:
return {
"id": instance_id,
"definition_id": 3,
"child_id": child_id,
"title": "Brush teeth",
"icon": "🦷",
"window": "morning",
"due_time": "08:00",
"state": state,
"overdue": False,
"completed_at": None if state == "open" else "2026-09-23T07:55:00+00:00",
"on_time": None if state == "open" else True,
}
def _payload(children) -> dict:
return {
"today_iso": "2026-09-23",
"cycle_day": 0,
"children": list(children),
}
ADA = 1
BO = 2
CORY = 3
async def _setup_entry(hass, make_entry, *payloads):
"""Set up the integration with a scripted snapshot client."""
entry = wire_entry_to_registry(
make_entry(
data={"api_base_url": "http://api.test:8000", "panel_token": "tok"}
),
[ada.id, bo.id],
["morning"],
hass.registry,
)
end = (today + datetime.timedelta(days=SEED_HORIZON_DAYS)).isoformat()
await materialize(database, today.isoformat(), end, today=today)
return ada, bo
set_coordinator_client(entry, StubSnapshotClient(*payloads))
assert await async_setup_entry(hass, entry) is True
return entry, entry.runtime_data.coordinator
async def test_all_done_off_with_open_quests_and_zero_quests(
@@ -52,88 +87,76 @@ async def test_all_done_off_with_open_quests_and_zero_quests(
) -> None:
"""all_done is OFF while quests are owed AND on a zero-quest day;
a child with no quests never fires the celebration automation."""
entry = await _setup_entry(hass, make_entry)
coordinator = entry.runtime_data.coordinator
database = entry.runtime_data.database
ada, _bo = await _seed_two_children(database)
await create_child(database, "Cory") # zero quests
await coordinator.async_refresh()
_entry, coordinator = await _setup_entry(
hass,
make_entry,
_payload(
[
_child(ADA, "Ada", instances=[_instance(7, ADA)]),
_child(BO, "Bo", instances=[_instance(8, BO)]),
_child(CORY, "Cory"),
]
),
)
assert _binary(hass, ada.id, "all_done").is_on is False
cory = [
child for child in coordinator.data.children
if child.child_name == "Cory"
][0]
assert _binary(hass, cory.child_id, "all_done").is_on is False
assert _binary(hass, ADA, "all_done").is_on is False
assert _binary(hass, BO, "all_done").is_on is False
assert _binary(hass, CORY, "all_done").is_on is False
async def test_all_done_on_only_when_day_clear(hass, make_entry) -> None:
entry = await _setup_entry(hass, make_entry)
coordinator = entry.runtime_data.coordinator
database = entry.runtime_data.database
ada, _bo = await _seed_two_children(database)
await coordinator.async_refresh()
snapshot_ada = [
child for child in coordinator.data.children
if child.child_id == ada.id
][0]
instance_id = snapshot_ada.instances[0].instance_id
await complete_instance(
database,
instance_id,
actor_source="panel",
actor_child_id=ada.id,
_entry, coordinator = await _setup_entry(
hass,
make_entry,
_payload([_child(ADA, "Ada", instances=[_instance(7, ADA)])]),
_payload(
[
_child(
ADA,
"Ada",
instances=[_instance(7, ADA, state="completed")],
)
]
),
)
await coordinator.async_refresh()
assert _binary(hass, ada.id, "all_done").is_on is True
assert _binary(hass, ADA, "all_done").is_on is True
async def test_present_today_on_without_schedule_off_when_absent(
hass, make_entry
) -> None:
"""No schedule means present every day; an all-absent pattern
today reports OFF."""
entry = await _setup_entry(hass, make_entry)
coordinator = entry.runtime_data.coordinator
database = entry.runtime_data.database
ada, _bo = await _seed_two_children(database)
await create_child(database, "Cory")
today = datetime.date.today()
await coordinator.async_refresh()
cory = [
child for child in coordinator.data.children
if child.child_name == "Cory"
][0]
await PresenceSchedulesDao(database).upsert_by_child(
cory.child_id, 1, today.isoformat(), ""
"""Presence is resolved by the API's presence engine: present=True
renders ON, present=False renders OFF."""
_entry, _coordinator = await _setup_entry(
hass,
make_entry,
_payload([_child(ADA, "Ada"), _child(CORY, "Cory", present=False)]),
)
await coordinator.async_refresh()
assert _binary(hass, ada.id, "present_today").is_on is True
assert _binary(hass, cory.child_id, "present_today").is_on is False
assert _binary(hass, ADA, "present_today").is_on is True
assert _binary(hass, CORY, "present_today").is_on is False
async def test_binary_sensor_identity_and_attributes(hass, make_entry) -> None:
entry = await _setup_entry(hass, make_entry)
coordinator = entry.runtime_data.coordinator
database = entry.runtime_data.database
ada, _bo = await _seed_two_children(database)
await coordinator.async_refresh()
_entry, _coordinator = await _setup_entry(
hass, make_entry, _payload([_child(ADA, "Ada")])
)
all_done = _binary(hass, ada.id, "all_done")
present = _binary(hass, ada.id, "present_today")
assert all_done.unique_id == f"nestquest_child_{ada.id}_all_done"
assert present.unique_id == f"nestquest_child_{ada.id}_present_today"
all_done = _binary(hass, ADA, "all_done")
present = _binary(hass, ADA, "present_today")
assert all_done.unique_id == f"nestquest_child_{ADA}_all_done"
assert present.unique_id == f"nestquest_child_{ADA}_present_today"
assert all_done.name == "NestQuest Ada all done"
assert present.name == "NestQuest Ada present today"
for entity in (all_done, present):
assert entity.device_info.name == "NestQuest Ada"
assert all_done.extra_state_attributes == {
"child_id": ada.id,
"child_id": ADA,
"child_name": "Ada",
}
assert present.extra_state_attributes == {
"child_id": ada.id,
"child_id": ADA,
"child_name": "Ada",
"next_present": None,
}
@@ -145,40 +168,28 @@ async def test_present_today_carries_next_present_when_away(
"""A child away today carries the ISO date of their next present
day (the panel's away plate renders ``Returns <weekday>, <Mon D>``
from it); an always-absent pattern carries ``None``."""
entry = await _setup_entry(hass, make_entry)
coordinator = entry.runtime_data.coordinator
database = entry.runtime_data.database
ada, _bo = await _seed_two_children(database)
await create_child(database, "Cory")
today = datetime.date.today()
pattern = ",".join(
str(day) for day in range(7) if day != today.weekday()
_entry, _coordinator = await _setup_entry(
hass,
make_entry,
_payload(
[
_child(
ADA,
"Ada",
present=False,
next_present="2026-09-24",
),
_child(CORY, "Cory", present=False, next_present=None),
]
),
)
await PresenceSchedulesDao(database).upsert_by_child(
ada.id, 1, today.isoformat(), pattern
)
await PresenceSchedulesDao(database).upsert_by_child(
await _cory_id(database), 1, today.isoformat(), ""
)
await coordinator.async_refresh()
assert _binary(hass, ada.id, "present_today").is_on is False
assert _binary(hass, ada.id, "present_today").extra_state_attributes[
assert _binary(hass, ADA, "present_today").is_on is False
assert _binary(hass, ADA, "present_today").extra_state_attributes[
"next_present"
] == (today + datetime.timedelta(days=1)).isoformat()
] == "2026-09-24"
cory = [
child for child in coordinator.data.children
if child.child_name == "Cory"
][0]
assert _binary(hass, cory.child_id, "present_today").is_on is False
assert _binary(hass, cory.child_id, "present_today").extra_state_attributes[
assert _binary(hass, CORY, "present_today").is_on is False
assert _binary(hass, CORY, "present_today").extra_state_attributes[
"next_present"
] is None
async def _cory_id(database) -> int:
for child in await list_children(database):
if child.display_name == "Cory":
return child.id
raise AssertionError("Cory was not created")