Repository navigation
feat: add thermostat temperature history #174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matt-thepie
wants to merge
2
commits into
Pyhass:dev
Choose a base branch
from
matt-thepie:feat/temperature-history
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| """Tests for Climate.get_temperature_history and the Hive history API call.""" | ||
|
|
||
| # pylint: disable=too-few-public-methods | ||
| from unittest.mock import AsyncMock, MagicMock | ||
|
|
||
| from apyhiveapi.devices.heating import Climate | ||
| from apyhiveapi.helper.hive_exceptions import HiveApiError | ||
| from apyhiveapi.helper.hivedataclasses import Device, SessionConfig | ||
| from apyhiveapi.helper.map import Map | ||
|
|
||
| _HTTP_OK = 200 | ||
| _ROWS = [ | ||
| {"date": "1792425280000", "temperature": 20.9375}, | ||
| {"date": "1792421680000", "temperature": 21.008695652173913}, | ||
| ] | ||
|
|
||
|
|
||
| def _make_climate(parsed=None, file_mode=False, raise_api_error=False): | ||
| session = MagicMock() | ||
| session.data = Map( | ||
| {"products": {"heat-1": {"type": "heating", "state": {}}}, "devices": {}} | ||
| ) | ||
| session.config = SessionConfig() | ||
| session.config.file = file_mode | ||
| session.api = MagicMock() | ||
| if raise_api_error: | ||
| session.api.get_heating_history = AsyncMock(side_effect=HiveApiError) | ||
| else: | ||
| session.api.get_heating_history = AsyncMock( | ||
| return_value={"original": _HTTP_OK, "parsed": parsed} | ||
| ) | ||
| device = Device( | ||
| hive_id="heat-1", | ||
| hive_name="Downstairs", | ||
| hive_type="heating", | ||
| ha_name="Downstairs", | ||
| ha_type="climate", | ||
| device_id="dev-1", | ||
| device_name="Thermostat", | ||
| device_data={}, | ||
| ) | ||
| return Climate(session), device, session | ||
|
|
||
|
|
||
| class TestGetTemperatureHistory: | ||
| async def test_returns_sorted_rows_with_int_dates(self): | ||
| climate, device, session = _make_climate({"data": _ROWS}) | ||
| history = await climate.get_temperature_history(device, 1, 2, "HOURS", 1) | ||
| assert history == [ | ||
| {"date": 1792421680000, "temperature": 21.008695652173913}, | ||
| {"date": 1792425280000, "temperature": 20.9375}, | ||
| ] | ||
| session.api.get_heating_history.assert_awaited_once_with( | ||
| "heat-1", 1, 2, "HOURS", 1 | ||
| ) | ||
|
|
||
| async def test_defaults_to_hourly(self): | ||
| climate, device, session = _make_climate({"data": []}) | ||
| assert await climate.get_temperature_history(device, 1, 2) == [] | ||
| session.api.get_heating_history.assert_awaited_once_with( | ||
| "heat-1", 1, 2, "HOURS", 1 | ||
| ) | ||
|
|
||
| async def test_skips_malformed_rows(self): | ||
| rows = [{"date": "x", "temperature": 1}, {"temperature": 2}, _ROWS[0]] | ||
| climate, device, _ = _make_climate({"data": rows}) | ||
| history = await climate.get_temperature_history(device, 1, 2) | ||
| assert history == [{"date": 1792425280000, "temperature": 20.9375}] | ||
|
|
||
| async def test_empty_when_data_missing_or_not_a_list(self): | ||
| for parsed in ({}, None, {"data": "nope"}, "garbage"): | ||
| climate, device, _ = _make_climate(parsed) | ||
| assert await climate.get_temperature_history(device, 1, 2) == [] | ||
|
|
||
| async def test_empty_on_api_error(self): | ||
| climate, device, _ = _make_climate(raise_api_error=True) | ||
| assert await climate.get_temperature_history(device, 1, 2) == [] | ||
|
|
||
| async def test_empty_in_file_mode_without_calling_api(self): | ||
| climate, device, session = _make_climate(file_mode=True) | ||
| assert await climate.get_temperature_history(device, 1, 2) == [] | ||
| session.api.get_heating_history.assert_not_awaited() | ||
|
|
||
| async def test_camel_case_alias(self): | ||
| climate, device, session = _make_climate({"data": _ROWS[:1]}) | ||
| history = await climate.getTemperatureHistory(device, 1, 2, "DAYS", 2) | ||
| assert history == [{"date": 1792425280000, "temperature": 20.9375}] | ||
| session.api.get_heating_history.assert_awaited_once_with( | ||
| "heat-1", 1, 2, "DAYS", 2 | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 Claude Code Review
The docstring says this returns an empty list "if … the request fails", but only
HiveApiErroris caught.HiveApiAsync._call_endpointre-raisesasyncio.TimeoutError.request()raisesHiveAuthErroron 401/403 andNoApiTokenwhen no token is stored.Failure scenario: a Hive timeout, or a token that has expired between polls, makes
get_temperature_historyraise instead of returning[]. This method also never refreshes tokens, unlike the_execute_state_changepath.Suggested fix: either catch
(HiveApiError, asyncio.TimeoutError)here, or update the docstring to say that timeout and auth errors propagate. If it's meant to be callable on its own, refresh the token first the way the other handlers do.Minor: for TRV zones the documented 404 is logged at ERROR by
request()before this WARNING, so callers get noisy duplicate logs. Consider gating ondevice.hive_type == "heating".