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 datetime import date, timedelta
|
||||
|
||||
from twentemilieu import TwenteMilieu, WasteType
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_ID, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
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
|
||||
|
||||
SCAN_INTERVAL = timedelta(seconds=3600)
|
||||
from .coordinator import TwenteMilieuConfigEntry, TwenteMilieuDataUpdateCoordinator
|
||||
|
||||
SERVICE_UPDATE = "update"
|
||||
SERVICE_SCHEMA = vol.Schema({vol.Optional(CONF_ID): cv.string})
|
||||
|
||||
PLATFORMS = [Platform.CALENDAR, Platform.SENSOR]
|
||||
|
||||
type TwenteMilieuDataUpdateCoordinator = DataUpdateCoordinator[
|
||||
dict[WasteType, list[date]]
|
||||
]
|
||||
type TwenteMilieuConfigEntry = ConfigEntry[TwenteMilieuDataUpdateCoordinator]
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant, entry: TwenteMilieuConfigEntry
|
||||
) -> bool:
|
||||
"""Set up Twente Milieu from a config entry."""
|
||||
session = async_get_clientsession(hass)
|
||||
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,
|
||||
)
|
||||
coordinator = TwenteMilieuDataUpdateCoordinator(hass, entry)
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
entry.runtime_data = coordinator
|
||||
|
@ -10,8 +10,8 @@ from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from . import TwenteMilieuConfigEntry
|
||||
from .const import WASTE_TYPE_TO_DESCRIPTION
|
||||
from .coordinator import TwenteMilieuConfigEntry
|
||||
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.update_coordinator import CoordinatorEntity
|
||||
|
||||
from . import TwenteMilieuConfigEntry, TwenteMilieuDataUpdateCoordinator
|
||||
from .const import DOMAIN
|
||||
from .coordinator import TwenteMilieuConfigEntry, TwenteMilieuDataUpdateCoordinator
|
||||
|
||||
|
||||
class TwenteMilieuEntity(CoordinatorEntity[TwenteMilieuDataUpdateCoordinator], Entity):
|
||||
|
@ -6,10 +6,7 @@ rules:
|
||||
This integration does not provide additional actions.
|
||||
appropriate-polling: done
|
||||
brands: done
|
||||
common-modules:
|
||||
status: todo
|
||||
comment: |
|
||||
The coordinator isn't in the common module yet.
|
||||
common-modules: done
|
||||
config-flow-test-coverage: done
|
||||
config-flow:
|
||||
status: todo
|
||||
|
@ -16,8 +16,8 @@ from homeassistant.const import CONF_ID
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import TwenteMilieuConfigEntry
|
||||
from .const import DOMAIN
|
||||
from .coordinator import TwenteMilieuConfigEntry
|
||||
from .entity import TwenteMilieuEntity
|
||||
|
||||
|
||||
|
@ -51,7 +51,8 @@ def mock_twentemilieu() -> Generator[MagicMock]:
|
||||
"""Return a mocked Twente Milieu client."""
|
||||
with (
|
||||
patch(
|
||||
"homeassistant.components.twentemilieu.TwenteMilieu", autospec=True
|
||||
"homeassistant.components.twentemilieu.coordinator.TwenteMilieu",
|
||||
autospec=True,
|
||||
) as twentemilieu_mock,
|
||||
patch(
|
||||
"homeassistant.components.twentemilieu.config_flow.TwenteMilieu",
|
||||
|
@ -29,7 +29,7 @@ async def test_load_unload_config_entry(
|
||||
|
||||
|
||||
@patch(
|
||||
"homeassistant.components.twentemilieu.TwenteMilieu.update",
|
||||
"homeassistant.components.twentemilieu.coordinator.TwenteMilieu.update",
|
||||
side_effect=RuntimeError,
|
||||
)
|
||||
async def test_config_entry_not_ready(
|
||||
|
Loading…
x
Reference in New Issue
Block a user