mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Tibber diagnostics (#64472)
Co-authored-by: Philip Allgaier <mail@spacegaier.de> Co-authored-by: Paulus Schoutsen <balloob@gmail.com> Co-authored-by: Franck Nijhof <git@frenck.dev> Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
b8ecb98fd6
commit
549afff307
31
homeassistant/components/tibber/diagnostics.py
Normal file
31
homeassistant/components/tibber/diagnostics.py
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
"""Diagnostics support for Tibber."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import tibber
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
|
|
||||||
|
async def async_get_config_entry_diagnostics(
|
||||||
|
hass: HomeAssistant, config_entry: ConfigEntry
|
||||||
|
) -> dict:
|
||||||
|
"""Return diagnostics for a config entry."""
|
||||||
|
tibber_connection: tibber.Tibber = hass.data[DOMAIN]
|
||||||
|
|
||||||
|
diagnostics_data = {}
|
||||||
|
|
||||||
|
homes = {}
|
||||||
|
for home in tibber_connection.get_homes(only_active=False):
|
||||||
|
homes[home.home_id] = {
|
||||||
|
"last_data_timestamp": home.last_data_timestamp,
|
||||||
|
"has_active_subscription": home.has_active_subscription,
|
||||||
|
"has_real_time_consumption": home.has_real_time_consumption,
|
||||||
|
"last_cons_data_timestamp": home.last_cons_data_timestamp,
|
||||||
|
"country": home.country,
|
||||||
|
}
|
||||||
|
diagnostics_data["homes"] = homes
|
||||||
|
|
||||||
|
return diagnostics_data
|
19
tests/components/tibber/conftest.py
Normal file
19
tests/components/tibber/conftest.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
"""Test helpers for Tibber."""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from homeassistant.components.tibber.const import DOMAIN
|
||||||
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
|
|
||||||
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def config_entry(hass):
|
||||||
|
"""Tibber config entry."""
|
||||||
|
config_entry = MockConfigEntry(
|
||||||
|
domain=DOMAIN,
|
||||||
|
data={CONF_ACCESS_TOKEN: "token"},
|
||||||
|
unique_id="tibber",
|
||||||
|
)
|
||||||
|
config_entry.add_to_hass(hass)
|
||||||
|
return config_entry
|
36
tests/components/tibber/test_common.py
Normal file
36
tests/components/tibber/test_common.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
"""Test common."""
|
||||||
|
import datetime as dt
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def mock_get_homes(only_active=True):
|
||||||
|
"""Return a list of mocked Tibber homes."""
|
||||||
|
tibber_home = AsyncMock()
|
||||||
|
tibber_home.name = "Name"
|
||||||
|
tibber_home.home_id = "home_id"
|
||||||
|
tibber_home.currency = "NOK"
|
||||||
|
tibber_home.has_active_subscription = True
|
||||||
|
tibber_home.has_real_time_consumption = False
|
||||||
|
tibber_home.country = "NO"
|
||||||
|
tibber_home.last_cons_data_timestamp = dt.datetime(2016, 1, 1, 12, 44, 57)
|
||||||
|
tibber_home.last_data_timestamp = dt.datetime(2016, 1, 1, 12, 48, 57)
|
||||||
|
tibber_home.get_historic_data.return_value = CONSUMPTION_DATA_1
|
||||||
|
return [tibber_home]
|
@ -7,7 +7,7 @@ from homeassistant import config_entries
|
|||||||
from homeassistant.components.tibber.const import DOMAIN
|
from homeassistant.components.tibber.const import DOMAIN
|
||||||
from homeassistant.const import CONF_ACCESS_TOKEN
|
from homeassistant.const import CONF_ACCESS_TOKEN
|
||||||
|
|
||||||
from tests.common import MockConfigEntry, async_init_recorder_component
|
from tests.common import async_init_recorder_component
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(name="tibber_setup", autouse=True)
|
@pytest.fixture(name="tibber_setup", autouse=True)
|
||||||
@ -55,17 +55,10 @@ async def test_create_entry(hass):
|
|||||||
assert result["data"] == test_data
|
assert result["data"] == test_data
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_entry_already_exists(hass):
|
async def test_flow_entry_already_exists(hass, config_entry):
|
||||||
"""Test user input for config_entry that already exists."""
|
"""Test user input for config_entry that already exists."""
|
||||||
await async_init_recorder_component(hass)
|
await async_init_recorder_component(hass)
|
||||||
|
|
||||||
first_entry = MockConfigEntry(
|
|
||||||
domain="tibber",
|
|
||||||
data={CONF_ACCESS_TOKEN: "valid"},
|
|
||||||
unique_id="tibber",
|
|
||||||
)
|
|
||||||
first_entry.add_to_hass(hass)
|
|
||||||
|
|
||||||
test_data = {
|
test_data = {
|
||||||
CONF_ACCESS_TOKEN: "valid",
|
CONF_ACCESS_TOKEN: "valid",
|
||||||
}
|
}
|
||||||
|
50
tests/components/tibber/test_diagnostics.py
Normal file
50
tests/components/tibber/test_diagnostics.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
"""Test the Netatmo diagnostics."""
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
|
from .test_common import mock_get_homes
|
||||||
|
|
||||||
|
from tests.common import async_init_recorder_component
|
||||||
|
from tests.components.diagnostics import get_diagnostics_for_config_entry
|
||||||
|
|
||||||
|
|
||||||
|
async def test_entry_diagnostics(hass, hass_client, config_entry):
|
||||||
|
"""Test config entry diagnostics."""
|
||||||
|
await async_init_recorder_component(hass)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"tibber.Tibber.update_info",
|
||||||
|
return_value=None,
|
||||||
|
), patch("homeassistant.components.tibber.discovery.async_load_platform"):
|
||||||
|
assert await async_setup_component(hass, "tibber", {})
|
||||||
|
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"tibber.Tibber.get_homes",
|
||||||
|
return_value=[],
|
||||||
|
):
|
||||||
|
result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||||
|
|
||||||
|
assert result == {
|
||||||
|
"homes": {},
|
||||||
|
}
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"tibber.Tibber.get_homes",
|
||||||
|
side_effect=mock_get_homes,
|
||||||
|
):
|
||||||
|
result = await get_diagnostics_for_config_entry(hass, hass_client, config_entry)
|
||||||
|
|
||||||
|
assert result == {
|
||||||
|
"homes": {
|
||||||
|
"home_id": {
|
||||||
|
"last_data_timestamp": "2016-01-01T12:48:57",
|
||||||
|
"has_active_subscription": True,
|
||||||
|
"has_real_time_consumption": False,
|
||||||
|
"last_cons_data_timestamp": "2016-01-01T12:44:57",
|
||||||
|
"country": "NO",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
@ -5,44 +5,20 @@ from homeassistant.components.recorder.statistics import statistics_during_perio
|
|||||||
from homeassistant.components.tibber.sensor import TibberDataCoordinator
|
from homeassistant.components.tibber.sensor import TibberDataCoordinator
|
||||||
from homeassistant.util import dt as dt_util
|
from homeassistant.util import dt as dt_util
|
||||||
|
|
||||||
|
from .test_common import CONSUMPTION_DATA_1, mock_get_homes
|
||||||
|
|
||||||
from tests.common import async_init_recorder_component
|
from tests.common import async_init_recorder_component
|
||||||
from tests.components.recorder.common import async_wait_recording_done_without_instance
|
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):
|
async def test_async_setup_entry(hass):
|
||||||
"""Test setup Tibber."""
|
"""Test setup Tibber."""
|
||||||
await async_init_recorder_component(hass)
|
await async_init_recorder_component(hass)
|
||||||
|
|
||||||
def _get_homes():
|
|
||||||
tibber_home = AsyncMock()
|
|
||||||
tibber_home.name = "Name"
|
|
||||||
tibber_home.home_id = "home_id"
|
|
||||||
tibber_home.currency = "NOK"
|
|
||||||
tibber_home.get_historic_data.return_value = _CONSUMPTION_DATA_1
|
|
||||||
return [tibber_home]
|
|
||||||
|
|
||||||
tibber_connection = AsyncMock()
|
tibber_connection = AsyncMock()
|
||||||
tibber_connection.name = "tibber"
|
tibber_connection.name = "tibber"
|
||||||
tibber_connection.fetch_consumption_data_active_homes.return_value = None
|
tibber_connection.fetch_consumption_data_active_homes.return_value = None
|
||||||
tibber_connection.get_homes = _get_homes
|
tibber_connection.get_homes = mock_get_homes
|
||||||
|
|
||||||
coordinator = TibberDataCoordinator(hass, tibber_connection)
|
coordinator = TibberDataCoordinator(hass, tibber_connection)
|
||||||
await coordinator._async_update_data()
|
await coordinator._async_update_data()
|
||||||
@ -54,7 +30,7 @@ async def test_async_setup_entry(hass):
|
|||||||
stats = await hass.async_add_executor_job(
|
stats = await hass.async_add_executor_job(
|
||||||
statistics_during_period,
|
statistics_during_period,
|
||||||
hass,
|
hass,
|
||||||
dt_util.parse_datetime(_CONSUMPTION_DATA_1[0]["from"]),
|
dt_util.parse_datetime(CONSUMPTION_DATA_1[0]["from"]),
|
||||||
None,
|
None,
|
||||||
[statistic_id],
|
[statistic_id],
|
||||||
"hour",
|
"hour",
|
||||||
@ -65,14 +41,14 @@ async def test_async_setup_entry(hass):
|
|||||||
assert len(stats[statistic_id]) == 3
|
assert len(stats[statistic_id]) == 3
|
||||||
_sum = 0
|
_sum = 0
|
||||||
for k, stat in enumerate(stats[statistic_id]):
|
for k, stat in enumerate(stats[statistic_id]):
|
||||||
assert stat["start"] == dt_util.parse_datetime(_CONSUMPTION_DATA_1[k]["from"])
|
assert stat["start"] == dt_util.parse_datetime(CONSUMPTION_DATA_1[k]["from"])
|
||||||
assert stat["state"] == _CONSUMPTION_DATA_1[k]["consumption"]
|
assert stat["state"] == CONSUMPTION_DATA_1[k]["consumption"]
|
||||||
assert stat["mean"] is None
|
assert stat["mean"] is None
|
||||||
assert stat["min"] is None
|
assert stat["min"] is None
|
||||||
assert stat["max"] is None
|
assert stat["max"] is None
|
||||||
assert stat["last_reset"] is None
|
assert stat["last_reset"] is None
|
||||||
|
|
||||||
_sum += _CONSUMPTION_DATA_1[k]["consumption"]
|
_sum += CONSUMPTION_DATA_1[k]["consumption"]
|
||||||
assert stat["sum"] == _sum
|
assert stat["sum"] == _sum
|
||||||
|
|
||||||
# Validate cost
|
# Validate cost
|
||||||
@ -81,7 +57,7 @@ async def test_async_setup_entry(hass):
|
|||||||
stats = await hass.async_add_executor_job(
|
stats = await hass.async_add_executor_job(
|
||||||
statistics_during_period,
|
statistics_during_period,
|
||||||
hass,
|
hass,
|
||||||
dt_util.parse_datetime(_CONSUMPTION_DATA_1[0]["from"]),
|
dt_util.parse_datetime(CONSUMPTION_DATA_1[0]["from"]),
|
||||||
None,
|
None,
|
||||||
[statistic_id],
|
[statistic_id],
|
||||||
"hour",
|
"hour",
|
||||||
@ -92,12 +68,12 @@ async def test_async_setup_entry(hass):
|
|||||||
assert len(stats[statistic_id]) == 3
|
assert len(stats[statistic_id]) == 3
|
||||||
_sum = 0
|
_sum = 0
|
||||||
for k, stat in enumerate(stats[statistic_id]):
|
for k, stat in enumerate(stats[statistic_id]):
|
||||||
assert stat["start"] == dt_util.parse_datetime(_CONSUMPTION_DATA_1[k]["from"])
|
assert stat["start"] == dt_util.parse_datetime(CONSUMPTION_DATA_1[k]["from"])
|
||||||
assert stat["state"] == _CONSUMPTION_DATA_1[k]["totalCost"]
|
assert stat["state"] == CONSUMPTION_DATA_1[k]["totalCost"]
|
||||||
assert stat["mean"] is None
|
assert stat["mean"] is None
|
||||||
assert stat["min"] is None
|
assert stat["min"] is None
|
||||||
assert stat["max"] is None
|
assert stat["max"] is None
|
||||||
assert stat["last_reset"] is None
|
assert stat["last_reset"] is None
|
||||||
|
|
||||||
_sum += _CONSUMPTION_DATA_1[k]["totalCost"]
|
_sum += CONSUMPTION_DATA_1[k]["totalCost"]
|
||||||
assert stat["sum"] == _sum
|
assert stat["sum"] == _sum
|
||||||
|
Loading…
x
Reference in New Issue
Block a user