mirror of
https://github.com/home-assistant/core.git
synced 2025-07-22 12:47:08 +00:00
Mill
Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
parent
9e9241744c
commit
524e468afa
@ -14,7 +14,7 @@ from homeassistant.exceptions import ConfigEntryNotReady
|
|||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
|
||||||
from .const import CLOUD, CONNECTION_TYPE, DOMAIN, LOCAL
|
from .const import CLOUD, CONNECTION_TYPE, DOMAIN, LOCAL
|
||||||
from .coordinator import MillDataUpdateCoordinator
|
from .coordinator import MillDataUpdateCoordinator, MillHistoricDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR]
|
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR]
|
||||||
|
|
||||||
@ -40,6 +40,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
update_interval = timedelta(seconds=30)
|
update_interval = timedelta(seconds=30)
|
||||||
key = entry.data[CONF_USERNAME]
|
key = entry.data[CONF_USERNAME]
|
||||||
conn_type = CLOUD
|
conn_type = CLOUD
|
||||||
|
historic_data_coordinator = MillHistoricDataUpdateCoordinator(
|
||||||
|
hass,
|
||||||
|
mill_data_connection=mill_data_connection,
|
||||||
|
update_interval=update_interval,
|
||||||
|
)
|
||||||
|
await historic_data_coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not await mill_data_connection.connect():
|
if not await mill_data_connection.connect():
|
||||||
|
@ -25,6 +25,8 @@ from .const import DOMAIN
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
ONE_YEAR = 1 * 365 * 24
|
||||||
|
|
||||||
|
|
||||||
class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
"""Class to manage fetching Mill data."""
|
"""Class to manage fetching Mill data."""
|
||||||
@ -38,6 +40,28 @@ class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize global Mill data updater."""
|
"""Initialize global Mill data updater."""
|
||||||
self.mill_data_connection = mill_data_connection
|
self.mill_data_connection = mill_data_connection
|
||||||
|
|
||||||
|
super().__init__(
|
||||||
|
hass,
|
||||||
|
_LOGGER,
|
||||||
|
name=DOMAIN,
|
||||||
|
update_method=mill_data_connection.fetch_heater_and_sensor_data,
|
||||||
|
update_interval=update_interval,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class MillHistoricDataUpdateCoordinator(DataUpdateCoordinator):
|
||||||
|
"""Class to manage fetching Mill historic data."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
update_interval: timedelta | None = None,
|
||||||
|
*,
|
||||||
|
mill_data_connection: Mill,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize global Mill data updater."""
|
||||||
|
self.mill_data_connection = mill_data_connection
|
||||||
self._last_stats_time = dt_util.utcnow() - timedelta(days=1)
|
self._last_stats_time = dt_util.utcnow() - timedelta(days=1)
|
||||||
|
|
||||||
super().__init__(
|
super().__init__(
|
||||||
@ -47,15 +71,8 @@ class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
update_interval=update_interval,
|
update_interval=update_interval,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_update_data(self) -> dict:
|
async def _async_update_data(self):
|
||||||
"""Update data via API."""
|
"""Update data via API."""
|
||||||
data = await self.mill_data_connection.fetch_heater_and_sensor_data()
|
|
||||||
if isinstance(self.mill_data_connection, Mill):
|
|
||||||
await self._insert_statistics()
|
|
||||||
return data
|
|
||||||
|
|
||||||
async def _insert_statistics(self) -> None:
|
|
||||||
"""Insert Mill statistics."""
|
|
||||||
now = dt_util.utcnow()
|
now = dt_util.utcnow()
|
||||||
if self._last_stats_time > now - timedelta(hours=1):
|
if self._last_stats_time > now - timedelta(hours=1):
|
||||||
return
|
return
|
||||||
@ -70,7 +87,9 @@ class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
|
|
||||||
if not last_stats or not last_stats.get(statistic_id):
|
if not last_stats or not last_stats.get(statistic_id):
|
||||||
hourly_data = (
|
hourly_data = (
|
||||||
await self.mill_data_connection.fetch_historic_energy_usage(dev_id)
|
await self.mill_data_connection.fetch_historic_energy_usage(
|
||||||
|
dev_id, n_days=ONE_YEAR
|
||||||
|
)
|
||||||
)
|
)
|
||||||
hourly_data = dict(sorted(hourly_data.items(), key=lambda x: x[0]))
|
hourly_data = dict(sorted(hourly_data.items(), key=lambda x: x[0]))
|
||||||
_sum = 0.0
|
_sum = 0.0
|
||||||
@ -133,3 +152,4 @@ class MillDataUpdateCoordinator(DataUpdateCoordinator):
|
|||||||
)
|
)
|
||||||
async_add_external_statistics(self.hass, metadata, statistics)
|
async_add_external_statistics(self.hass, metadata, statistics)
|
||||||
self._last_stats_time = now.replace(minute=0, second=0)
|
self._last_stats_time = now.replace(minute=0, second=0)
|
||||||
|
return
|
||||||
|
@ -5,7 +5,7 @@ from unittest.mock import AsyncMock
|
|||||||
from mill import Heater, Mill
|
from mill import Heater, Mill
|
||||||
|
|
||||||
from homeassistant.components.mill.const import DOMAIN
|
from homeassistant.components.mill.const import DOMAIN
|
||||||
from homeassistant.components.mill.coordinator import MillDataUpdateCoordinator
|
from homeassistant.components.mill.coordinator import MillHistoricDataUpdateCoordinator
|
||||||
from homeassistant.components.recorder import Recorder
|
from homeassistant.components.recorder import Recorder
|
||||||
from homeassistant.components.recorder.statistics import statistics_during_period
|
from homeassistant.components.recorder.statistics import statistics_during_period
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
@ -30,7 +30,7 @@ async def test_async_setup_entry(recorder_mock: Recorder, hass: HomeAssistant) -
|
|||||||
|
|
||||||
statistic_id = f"{DOMAIN}:energy_dev_id"
|
statistic_id = f"{DOMAIN}:energy_dev_id"
|
||||||
|
|
||||||
coordinator = MillDataUpdateCoordinator(
|
coordinator = MillHistoricDataUpdateCoordinator(
|
||||||
hass, mill_data_connection=mill_data_connection
|
hass, mill_data_connection=mill_data_connection
|
||||||
)
|
)
|
||||||
await coordinator._async_update_data()
|
await coordinator._async_update_data()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user