HA-008 HA-009: add discovery and history pipeline
Closes #17 Closes #18
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
@@ -68,4 +69,60 @@ def test_list_entities_rejects_invalid_json() -> None:
|
||||
def test_list_entities_rejects_non_list_payload() -> None:
|
||||
client = _client_with_response(_response(payload={"entity_id": "sensor.temperature"}))
|
||||
with pytest.raises(HaUnexpectedPayloadError):
|
||||
client.list_entities()
|
||||
client.list_entities()
|
||||
|
||||
|
||||
def test_get_history_calls_home_assistant_history_api() -> None:
|
||||
response = _response(payload=[[{"entity_id": "sensor.temperature", "state": "21.0"}]])
|
||||
client = _client_with_response(response)
|
||||
start = datetime(2026, 6, 1, tzinfo=timezone.utc)
|
||||
end = datetime(2026, 6, 2, tzinfo=timezone.utc)
|
||||
|
||||
payload = client.get_history(["sensor.temperature"], start, end)
|
||||
|
||||
assert payload == [[{"entity_id": "sensor.temperature", "state": "21.0"}]]
|
||||
client._session.get.assert_called_once() # type: ignore[attr-defined]
|
||||
call = client._session.get.call_args # type: ignore[attr-defined]
|
||||
assert "/api/history/period/2026-06-01T00:00:00+00:00" in call.args[0]
|
||||
assert call.kwargs["params"]["filter_entity_id"] == "sensor.temperature"
|
||||
assert call.kwargs["params"]["end_time"] == "2026-06-02T00:00:00+00:00"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("entity_ids", "start", "end"),
|
||||
[
|
||||
(
|
||||
[],
|
||||
datetime(2026, 6, 1, tzinfo=timezone.utc),
|
||||
datetime(2026, 6, 2, tzinfo=timezone.utc),
|
||||
),
|
||||
(
|
||||
["sensor.temperature"],
|
||||
datetime(2026, 6, 1),
|
||||
datetime(2026, 6, 2, tzinfo=timezone.utc),
|
||||
),
|
||||
(
|
||||
["sensor.temperature"],
|
||||
datetime(2026, 6, 2, tzinfo=timezone.utc),
|
||||
datetime(2026, 6, 1, tzinfo=timezone.utc),
|
||||
),
|
||||
(
|
||||
["invalid entity"],
|
||||
datetime(2026, 6, 1, tzinfo=timezone.utc),
|
||||
datetime(2026, 6, 2, tzinfo=timezone.utc),
|
||||
),
|
||||
(
|
||||
["sensor.temperature"],
|
||||
datetime(2026, 5, 1, tzinfo=timezone.utc),
|
||||
datetime(2026, 6, 2, tzinfo=timezone.utc),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_get_history_validates_request(
|
||||
entity_ids: list[str],
|
||||
start: datetime,
|
||||
end: datetime,
|
||||
) -> None:
|
||||
client = HaClient(HaClientSettings(url="http://ha.local", token="test-token"))
|
||||
with pytest.raises(ValueError):
|
||||
client.get_history(entity_ids, start, end)
|
||||
|
||||
Reference in New Issue
Block a user