mirror of
https://github.com/home-assistant/core.git
synced 2025-07-07 21:37:07 +00:00
Add data coordinator to Stookwijzer (#131574)
This commit is contained in:
parent
cf74532cc6
commit
4ba8db1de4
@ -4,36 +4,37 @@ from __future__ import annotations
|
||||
|
||||
from stookwijzer import Stookwijzer
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LOCATION, CONF_LONGITUDE, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import issue_registry as ir
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
|
||||
from .const import DOMAIN, LOGGER
|
||||
from .coordinator import StookwijzerConfigEntry, StookwijzerCoordinator
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: StookwijzerConfigEntry) -> bool:
|
||||
"""Set up Stookwijzer from a config entry."""
|
||||
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = Stookwijzer(
|
||||
async_get_clientsession(hass),
|
||||
entry.data[CONF_LATITUDE],
|
||||
entry.data[CONF_LONGITUDE],
|
||||
)
|
||||
coordinator = StookwijzerCoordinator(hass, entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
entry.runtime_data = coordinator
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
return True
|
||||
|
||||
|
||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_unload_entry(
|
||||
hass: HomeAssistant, entry: StookwijzerConfigEntry
|
||||
) -> bool:
|
||||
"""Unload Stookwijzer config entry."""
|
||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
||||
del hass.data[DOMAIN][entry.entry_id]
|
||||
return unload_ok
|
||||
return await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
|
||||
|
||||
async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
async def async_migrate_entry(
|
||||
hass: HomeAssistant, entry: StookwijzerConfigEntry
|
||||
) -> bool:
|
||||
"""Migrate old entry."""
|
||||
LOGGER.debug("Migrating from version %s", entry.version)
|
||||
|
||||
|
44
homeassistant/components/stookwijzer/coordinator.py
Normal file
44
homeassistant/components/stookwijzer/coordinator.py
Normal file
@ -0,0 +1,44 @@
|
||||
"""Class representing a Stookwijzer update coordinator."""
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from stookwijzer import Stookwijzer
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_LATITUDE, CONF_LONGITUDE
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DOMAIN, LOGGER
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=60)
|
||||
|
||||
type StookwijzerConfigEntry = ConfigEntry[StookwijzerCoordinator]
|
||||
|
||||
|
||||
class StookwijzerCoordinator(DataUpdateCoordinator[None]):
|
||||
"""Stookwijzer update coordinator."""
|
||||
|
||||
def __init__(self, hass: HomeAssistant, entry: StookwijzerConfigEntry) -> None:
|
||||
"""Initialize the coordinator."""
|
||||
super().__init__(
|
||||
hass,
|
||||
LOGGER,
|
||||
name=DOMAIN,
|
||||
update_interval=SCAN_INTERVAL,
|
||||
)
|
||||
self.client = Stookwijzer(
|
||||
async_get_clientsession(hass),
|
||||
entry.data[CONF_LATITUDE],
|
||||
entry.data[CONF_LONGITUDE],
|
||||
)
|
||||
|
||||
async def _async_update_data(self) -> None:
|
||||
"""Fetch data from API endpoint."""
|
||||
await self.client.async_update()
|
||||
if self.client.advice is None:
|
||||
raise UpdateFailed(
|
||||
translation_domain=DOMAIN,
|
||||
translation_key="no_data_received",
|
||||
)
|
@ -4,19 +4,16 @@ from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from stookwijzer import Stookwijzer
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import StookwijzerConfigEntry
|
||||
|
||||
|
||||
async def async_get_config_entry_diagnostics(
|
||||
hass: HomeAssistant, entry: ConfigEntry
|
||||
hass: HomeAssistant, entry: StookwijzerConfigEntry
|
||||
) -> dict[str, Any]:
|
||||
"""Return diagnostics for a config entry."""
|
||||
client: Stookwijzer = hass.data[DOMAIN][entry.entry_id]
|
||||
client = entry.runtime_data.client
|
||||
return {
|
||||
"advice": client.advice,
|
||||
}
|
||||
|
@ -2,32 +2,26 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import timedelta
|
||||
|
||||
from stookwijzer import Stookwijzer
|
||||
|
||||
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import DOMAIN
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=60)
|
||||
from .coordinator import StookwijzerConfigEntry, StookwijzerCoordinator
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
entry: ConfigEntry,
|
||||
entry: StookwijzerConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up Stookwijzer sensor from a config entry."""
|
||||
client = hass.data[DOMAIN][entry.entry_id]
|
||||
async_add_entities([StookwijzerSensor(client, entry)], update_before_add=True)
|
||||
async_add_entities([StookwijzerSensor(entry)])
|
||||
|
||||
|
||||
class StookwijzerSensor(SensorEntity):
|
||||
class StookwijzerSensor(CoordinatorEntity[StookwijzerCoordinator], SensorEntity):
|
||||
"""Defines a Stookwijzer binary sensor."""
|
||||
|
||||
_attr_attribution = "Data provided by atlasleefomgeving.nl"
|
||||
@ -35,9 +29,10 @@ class StookwijzerSensor(SensorEntity):
|
||||
_attr_has_entity_name = True
|
||||
_attr_translation_key = "advice"
|
||||
|
||||
def __init__(self, client: Stookwijzer, entry: ConfigEntry) -> None:
|
||||
def __init__(self, entry: StookwijzerConfigEntry) -> None:
|
||||
"""Initialize a Stookwijzer device."""
|
||||
self._client = client
|
||||
super().__init__(entry.runtime_data)
|
||||
self._client = entry.runtime_data.client
|
||||
self._attr_options = ["code_yellow", "code_orange", "code_red"]
|
||||
self._attr_unique_id = entry.entry_id
|
||||
self._attr_device_info = DeviceInfo(
|
||||
@ -47,15 +42,6 @@ class StookwijzerSensor(SensorEntity):
|
||||
configuration_url="https://www.atlasleefomgeving.nl/stookwijzer",
|
||||
)
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Update the data from the Stookwijzer handler."""
|
||||
await self._client.async_update()
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return if entity is available."""
|
||||
return self._client.advice is not None
|
||||
|
||||
@property
|
||||
def native_value(self) -> str | None:
|
||||
"""Return the state of the device."""
|
||||
|
@ -29,5 +29,10 @@
|
||||
"description": "The Stookwijzer integration was unable to automatically migrate your location to a new format the updated integrations uses.\n\nMake sure you are connected to the internet and restart Home Assistant to try again.\n\nIf this doesn't resolve the error, remove and re-add the integration.",
|
||||
"title": "Migration of your location failed"
|
||||
}
|
||||
},
|
||||
"exceptions": {
|
||||
"no_data_received": {
|
||||
"message": "No data received from Stookwijzer."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -61,6 +61,10 @@ def mock_stookwijzer() -> Generator[MagicMock]:
|
||||
"homeassistant.components.stookwijzer.Stookwijzer",
|
||||
autospec=True,
|
||||
) as stookwijzer_mock,
|
||||
patch(
|
||||
"homeassistant.components.stookwijzer.coordinator.Stookwijzer",
|
||||
new=stookwijzer_mock,
|
||||
),
|
||||
patch(
|
||||
"homeassistant.components.stookwijzer.config_flow.Stookwijzer",
|
||||
new=stookwijzer_mock,
|
||||
|
Loading…
x
Reference in New Issue
Block a user