BEHAVIOR-001: learn and predict actuator actions
This commit is contained in:
@@ -108,6 +108,35 @@ def test_list_entity_metadata_calls_template_api() -> None:
|
||||
}
|
||||
|
||||
|
||||
def test_get_logbook_filters_entity_and_period() -> None:
|
||||
response = _response(payload=[{"entity_id": "light.office"}])
|
||||
client = _client_with_response(response)
|
||||
start = datetime(2026, 6, 1, tzinfo=timezone.utc)
|
||||
end = datetime(2026, 6, 2, tzinfo=timezone.utc)
|
||||
|
||||
payload = client.get_logbook("light.office", start, end)
|
||||
|
||||
assert payload == [{"entity_id": "light.office"}]
|
||||
call = client._session.get.call_args # type: ignore[attr-defined]
|
||||
assert "/api/logbook/2026-06-01T00:00:00+00:00" in call.args[0]
|
||||
assert call.kwargs["params"]["entity"] == "light.office"
|
||||
|
||||
|
||||
def test_call_service_posts_to_home_assistant() -> None:
|
||||
response = _response(payload=[])
|
||||
client = HaClient(HaClientSettings(url="http://ha.local", token="test-token"))
|
||||
client._session.post = Mock(return_value=response) # type: ignore[method-assign]
|
||||
|
||||
result = client.call_service("light", "turn_on", {"entity_id": "light.office"})
|
||||
|
||||
assert result == []
|
||||
client._session.post.assert_called_once_with(
|
||||
"http://ha.local/api/services/light/turn_on",
|
||||
json={"entity_id": "light.office"},
|
||||
timeout=10,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("entity_ids", "start", "end"),
|
||||
[
|
||||
|
||||
@@ -54,6 +54,29 @@ class FakeHaClient(HaClient):
|
||||
}
|
||||
}
|
||||
|
||||
def get_logbook(
|
||||
self,
|
||||
entity_id: str,
|
||||
start_time: datetime,
|
||||
end_time: datetime,
|
||||
) -> list[object]:
|
||||
return [
|
||||
{
|
||||
"entity_id": entity_id,
|
||||
"when": start_time.isoformat(),
|
||||
"message": "turned on",
|
||||
"context_user_id": "user-1",
|
||||
}
|
||||
]
|
||||
|
||||
def call_service(
|
||||
self,
|
||||
domain: str,
|
||||
service: str,
|
||||
service_data: dict[str, object],
|
||||
) -> list[object]:
|
||||
return []
|
||||
|
||||
|
||||
def test_ha_reader_returns_summaries() -> None:
|
||||
reader = HaReader(FakeHaClient())
|
||||
@@ -63,6 +86,7 @@ def test_ha_reader_returns_summaries() -> None:
|
||||
assert domains == {"sensor", "light"}
|
||||
sensor = next(item for item in summaries if item.entity_id == "sensor.temperature")
|
||||
assert sensor.unit_of_measurement == "°C"
|
||||
assert sensor.state == "21.5"
|
||||
assert sensor.area_name == "Kueche"
|
||||
assert sensor.device_name == "Thermometer"
|
||||
|
||||
@@ -87,3 +111,15 @@ def test_ha_reader_normalizes_history() -> None:
|
||||
|
||||
assert history[0].entity_id == "sensor.temperature"
|
||||
assert history[0].points[0].value == 21.5
|
||||
|
||||
|
||||
def test_ha_reader_normalizes_state_history_and_logbook() -> None:
|
||||
reader = HaReader(FakeHaClient())
|
||||
start = datetime(2026, 6, 1, tzinfo=timezone.utc)
|
||||
end = datetime(2026, 6, 2, tzinfo=timezone.utc)
|
||||
|
||||
history = reader.read_state_history(["light.living_room"], start, end)
|
||||
logbook = reader.read_logbook("light.living_room", start, end)
|
||||
|
||||
assert history[0].points[0].state == "21.5"
|
||||
assert logbook[0].context_user_id == "user-1"
|
||||
|
||||
@@ -5,7 +5,11 @@ from datetime import datetime, timezone
|
||||
import pytest
|
||||
|
||||
from app.ha.exceptions import HaUnexpectedPayloadError
|
||||
from app.ha.history import normalize_history_payload
|
||||
from app.ha.history import (
|
||||
normalize_history_payload,
|
||||
normalize_logbook_payload,
|
||||
normalize_state_history_payload,
|
||||
)
|
||||
|
||||
|
||||
def test_normalize_history_payload_groups_and_sorts_numeric_states() -> None:
|
||||
@@ -90,3 +94,46 @@ def test_normalize_history_payload_rejects_malformed_structure(payload: object)
|
||||
|
||||
def test_normalize_history_payload_accepts_empty_series() -> None:
|
||||
assert normalize_history_payload([[]]) == []
|
||||
|
||||
|
||||
def test_normalize_state_history_keeps_categorical_changes() -> None:
|
||||
result = normalize_state_history_payload(
|
||||
[
|
||||
[
|
||||
{
|
||||
"entity_id": "light.office",
|
||||
"state": "off",
|
||||
"last_changed": "2026-06-01T08:00:00+00:00",
|
||||
},
|
||||
{
|
||||
"state": "on",
|
||||
"last_changed": "2026-06-01T08:05:00+00:00",
|
||||
},
|
||||
{
|
||||
"state": "on",
|
||||
"last_changed": "2026-06-01T08:06:00+00:00",
|
||||
},
|
||||
]
|
||||
]
|
||||
)
|
||||
|
||||
assert [point.state for point in result[0].points] == ["off", "on"]
|
||||
|
||||
|
||||
def test_normalize_logbook_preserves_action_origin() -> None:
|
||||
result = normalize_logbook_payload(
|
||||
[
|
||||
{
|
||||
"entity_id": "light.office",
|
||||
"when": "2026-06-01T08:05:00+00:00",
|
||||
"message": "turned on",
|
||||
"context_user_id": "user-1",
|
||||
"context_domain": "light",
|
||||
"context_service": "turn_on",
|
||||
}
|
||||
],
|
||||
"light.office",
|
||||
)
|
||||
|
||||
assert result[0].context_user_id == "user-1"
|
||||
assert result[0].context_service == "turn_on"
|
||||
|
||||
Reference in New Issue
Block a user