mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Move coordinator for TwenteMilieu into own module (#133000)
This commit is contained in:
parent
bb610acb86
commit
0377dc5b5a
@ -2,53 +2,25 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import date, timedelta
|
|
||||||
|
|
||||||
from twentemilieu import TwenteMilieu, WasteType
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import CONF_ID, Platform
|
from homeassistant.const import CONF_ID, Platform
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
||||||
|
|
||||||
from .const import CONF_HOUSE_LETTER, CONF_HOUSE_NUMBER, CONF_POST_CODE, DOMAIN, LOGGER
|
from .coordinator import TwenteMilieuConfigEntry, TwenteMilieuDataUpdateCoordinator
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(seconds=3600)
|
|
||||||
|
|
||||||
SERVICE_UPDATE = "update"
|
SERVICE_UPDATE = "update"
|
||||||
SERVICE_SCHEMA = vol.Schema({vol.Optional(CONF_ID): cv.string})
|
SERVICE_SCHEMA = vol.Schema({vol.Optional(CONF_ID): cv.string})
|
||||||
|
|
||||||
PLATFORMS = [Platform.CALENDAR, Platform.SENSOR]
|
PLATFORMS = [Platform.CALENDAR, Platform.SENSOR]
|
||||||
|
|
||||||
type TwenteMilieuDataUpdateCoordinator = DataUpdateCoordinator[
|
|
||||||
dict[WasteType, list[date]]
|
|
||||||
]
|
|
||||||
type TwenteMilieuConfigEntry = ConfigEntry[TwenteMilieuDataUpdateCoordinator]
|
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
hass: HomeAssistant, entry: TwenteMilieuConfigEntry
|
hass: HomeAssistant, entry: TwenteMilieuConfigEntry
|
||||||
) -> bool:
|
) -> bool:
|
||||||
"""Set up Twente Milieu from a config entry."""
|
"""Set up Twente Milieu from a config entry."""
|
||||||
session = async_get_clientsession(hass)
|
coordinator = TwenteMilieuDataUpdateCoordinator(hass, entry)
|
||||||
twentemilieu = TwenteMilieu(
|
|
||||||
post_code=entry.data[CONF_POST_CODE],
|
|
||||||
house_number=entry.data[CONF_HOUSE_NUMBER],
|
|
||||||
house_letter=entry.data[CONF_HOUSE_LETTER],
|
|
||||||
session=session,
|
|
||||||
)
|
|
||||||
|
|
||||||
coordinator: TwenteMilieuDataUpdateCoordinator = DataUpdateCoordinator(
|
|
||||||
hass,
|
|
||||||
LOGGER,
|
|
||||||
config_entry=entry,
|
|
||||||
name=DOMAIN,
|
|
||||||
update_interval=SCAN_INTERVAL,
|
|
||||||
update_method=twentemilieu.update,
|
|
||||||
)
|
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
|
|
||||||
entry.runtime_data = coordinator
|
entry.runtime_data = coordinator
|
||||||
|
@ -10,8 +10,8 @@ from homeassistant.core import HomeAssistant, callback
|
|||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
|
||||||
from . import TwenteMilieuConfigEntry
|
|
||||||
from .const import WASTE_TYPE_TO_DESCRIPTION
|
from .const import WASTE_TYPE_TO_DESCRIPTION
|
||||||
|
from .coordinator import TwenteMilieuConfigEntry
|
||||||
from .entity import TwenteMilieuEntity
|
from .entity import TwenteMilieuEntity
|
||||||
|
|
||||||
|
|
||||||
|
49
homeassistant/components/twentemilieu/coordinator.py
Normal file
49
homeassistant/components/twentemilieu/coordinator.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
"""Data update coordinator for Twente Milieu."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import date
|
||||||
|
|
||||||
|
from twentemilieu import TwenteMilieu, WasteType
|
||||||
|
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||||
|
|
||||||
|
from .const import (
|
||||||
|
CONF_HOUSE_LETTER,
|
||||||
|
CONF_HOUSE_NUMBER,
|
||||||
|
CONF_POST_CODE,
|
||||||
|
DOMAIN,
|
||||||
|
LOGGER,
|
||||||
|
SCAN_INTERVAL,
|
||||||
|
)
|
||||||
|
|
||||||
|
type TwenteMilieuConfigEntry = ConfigEntry[TwenteMilieuDataUpdateCoordinator]
|
||||||
|
|
||||||
|
|
||||||
|
class TwenteMilieuDataUpdateCoordinator(
|
||||||
|
DataUpdateCoordinator[dict[WasteType, list[date]]]
|
||||||
|
):
|
||||||
|
"""Class to manage fetching Twente Milieu data."""
|
||||||
|
|
||||||
|
def __init__(self, hass: HomeAssistant, entry: TwenteMilieuConfigEntry) -> None:
|
||||||
|
"""Initialize Twente Milieu data update coordinator."""
|
||||||
|
self.twentemilieu = TwenteMilieu(
|
||||||
|
post_code=entry.data[CONF_POST_CODE],
|
||||||
|
house_number=entry.data[CONF_HOUSE_NUMBER],
|
||||||
|
house_letter=entry.data[CONF_HOUSE_LETTER],
|
||||||
|
session=async_get_clientsession(hass),
|
||||||
|
)
|
||||||
|
super().__init__(
|
||||||
|
hass,
|
||||||
|
LOGGER,
|
||||||
|
name=DOMAIN,
|
||||||
|
update_interval=SCAN_INTERVAL,
|
||||||
|
config_entry=entry,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _async_update_data(self) -> dict[WasteType, list[date]]:
|
||||||
|
"""Fetch Twente Milieu data."""
|
||||||
|
return await self.twentemilieu.update()
|
@ -7,8 +7,8 @@ from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
|||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from . import TwenteMilieuConfigEntry, TwenteMilieuDataUpdateCoordinator
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import TwenteMilieuConfigEntry, TwenteMilieuDataUpdateCoordinator
|
||||||
|
|
||||||
|
|
||||||
class TwenteMilieuEntity(CoordinatorEntity[TwenteMilieuDataUpdateCoordinator], Entity):
|
class TwenteMilieuEntity(CoordinatorEntity[TwenteMilieuDataUpdateCoordinator], Entity):
|
||||||
|
@ -6,10 +6,7 @@ rules:
|
|||||||
This integration does not provide additional actions.
|
This integration does not provide additional actions.
|
||||||
appropriate-polling: done
|
appropriate-polling: done
|
||||||
brands: done
|
brands: done
|
||||||
common-modules:
|
common-modules: done
|
||||||
status: todo
|
|
||||||
comment: |
|
|
||||||
The coordinator isn't in the common module yet.
|
|
||||||
config-flow-test-coverage: done
|
config-flow-test-coverage: done
|
||||||
config-flow:
|
config-flow:
|
||||||
status: todo
|
status: todo
|
||||||
|
@ -16,8 +16,8 @@ from homeassistant.const import CONF_ID
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
from . import TwenteMilieuConfigEntry
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
from .coordinator import TwenteMilieuConfigEntry
|
||||||
from .entity import TwenteMilieuEntity
|
from .entity import TwenteMilieuEntity
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,7 +51,8 @@ def mock_twentemilieu() -> Generator[MagicMock]:
|
|||||||
"""Return a mocked Twente Milieu client."""
|
"""Return a mocked Twente Milieu client."""
|
||||||
with (
|
with (
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.twentemilieu.TwenteMilieu", autospec=True
|
"homeassistant.components.twentemilieu.coordinator.TwenteMilieu",
|
||||||
|
autospec=True,
|
||||||
) as twentemilieu_mock,
|
) as twentemilieu_mock,
|
||||||
patch(
|
patch(
|
||||||
"homeassistant.components.twentemilieu.config_flow.TwenteMilieu",
|
"homeassistant.components.twentemilieu.config_flow.TwenteMilieu",
|
||||||
|
@ -29,7 +29,7 @@ async def test_load_unload_config_entry(
|
|||||||
|
|
||||||
|
|
||||||
@patch(
|
@patch(
|
||||||
"homeassistant.components.twentemilieu.TwenteMilieu.update",
|
"homeassistant.components.twentemilieu.coordinator.TwenteMilieu.update",
|
||||||
side_effect=RuntimeError,
|
side_effect=RuntimeError,
|
||||||
)
|
)
|
||||||
async def test_config_entry_not_ready(
|
async def test_config_entry_not_ready(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user