-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add external Tibber statistics #62249
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 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,73 @@ | ||
"""Test adding external statistics from Tibber.""" | ||
from unittest.mock import AsyncMock | ||
|
||
from homeassistant.components.recorder.statistics import statistics_during_period | ||
from homeassistant.components.tibber.sensor import TibberDataCoordinator | ||
from homeassistant.util import dt as dt_util | ||
|
||
from tests.common import async_init_recorder_component | ||
from tests.components.recorder.common import async_wait_recording_done_without_instance | ||
|
||
_CONSUMPTION_DATA_1 = [ | ||
{ | ||
"from": "2022-01-03T00:00:00.000+01:00", | ||
"totalCost": 1.1, | ||
"consumption": 2.1, | ||
}, | ||
{ | ||
"from": "2022-01-03T01:00:00.000+01:00", | ||
"totalCost": 1.2, | ||
"consumption": 2.2, | ||
}, | ||
{ | ||
"from": "2022-01-03T02:00:00.000+01:00", | ||
"totalCost": 1.3, | ||
"consumption": 2.3, | ||
}, | ||
] | ||
|
||
|
||
async def test_async_setup_entry(hass): | ||
"""Test setup Tibber.""" | ||
await async_init_recorder_component(hass) | ||
|
||
def _get_homes(): | ||
tibber_home = AsyncMock() | ||
tibber_home.home_id = "home_id" | ||
tibber_home.get_historic_data.return_value = _CONSUMPTION_DATA_1 | ||
return [tibber_home] | ||
|
||
tibber_connection = AsyncMock() | ||
tibber_connection.name = "tibber" | ||
tibber_connection.fetch_consumption_data_active_homes.return_value = None | ||
tibber_connection.get_homes = _get_homes | ||
|
||
coordinator = TibberDataCoordinator(hass, tibber_connection) | ||
await coordinator._async_update_data() | ||
await async_wait_recording_done_without_instance(hass) | ||
|
||
statistic_id = "tibber:energy_consumption_home_id" | ||
|
||
stats = await hass.async_add_executor_job( | ||
statistics_during_period, | ||
hass, | ||
dt_util.parse_datetime(_CONSUMPTION_DATA_1[0]["from"]), | ||
None, | ||
[statistic_id], | ||
"hour", | ||
True, | ||
) | ||
|
||
assert len(stats) == 1 | ||
assert len(stats[statistic_id]) == 3 | ||
_sum = 0 | ||
for k, stat in enumerate(stats[statistic_id]): | ||
assert stat["start"] == dt_util.parse_datetime(_CONSUMPTION_DATA_1[k]["from"]) | ||
assert stat["state"] == _CONSUMPTION_DATA_1[k]["consumption"] | ||
assert stat["mean"] is None | ||
assert stat["min"] is None | ||
assert stat["max"] is None | ||
assert stat["last_reset"] is None | ||
|
||
_sum += _CONSUMPTION_DATA_1[k]["consumption"] | ||
assert stat["sum"] == _sum |
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.
Do we need to add the recorder to dependencies of tibber now?
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.
It is added to
after_dependencies
Is that correct?
https://developers.home-assistant.io/docs/creating_integration_manifest/#after-dependencies
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.
If the dependency isn't always required by the integration or if we need to handle a missing dependency gracefully in a config flow we use
after_dependencies
. If the dependency is always required we usedependencies
.