2019-07-14 10:30:23 +00:00
|
|
|
"""Support for Twente Milieu."""
|
2021-03-05 23:33:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-13 14:34:09 +00:00
|
|
|
from datetime import date, timedelta
|
2019-07-14 10:30:23 +00:00
|
|
|
|
2021-11-13 14:34:09 +00:00
|
|
|
from twentemilieu import TwenteMilieu, WasteType
|
2019-07-14 10:30:23 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-04 13:10:01 +00:00
|
|
|
from homeassistant.const import CONF_ID, Platform
|
2021-03-05 23:33:26 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-07-14 10:30:23 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2021-11-13 14:34:09 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2019-07-14 10:30:23 +00:00
|
|
|
|
2021-11-13 14:34:09 +00:00
|
|
|
from .const import CONF_HOUSE_LETTER, CONF_HOUSE_NUMBER, CONF_POST_CODE, DOMAIN, LOGGER
|
2021-05-22 08:15:30 +00:00
|
|
|
|
2019-07-14 10:30:23 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=3600)
|
|
|
|
|
|
|
|
SERVICE_UPDATE = "update"
|
|
|
|
SERVICE_SCHEMA = vol.Schema({vol.Optional(CONF_ID): cv.string})
|
|
|
|
|
2021-12-04 13:10:01 +00:00
|
|
|
PLATFORMS = [Platform.SENSOR]
|
2021-04-27 20:19:57 +00:00
|
|
|
|
2019-07-14 10:30:23 +00:00
|
|
|
|
2021-03-05 23:33:26 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2019-07-14 10:30:23 +00:00
|
|
|
"""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,
|
|
|
|
)
|
|
|
|
|
2021-11-13 14:34:09 +00:00
|
|
|
coordinator: DataUpdateCoordinator[
|
2022-03-15 11:05:15 +00:00
|
|
|
dict[WasteType, list[date]]
|
2021-11-13 14:34:09 +00:00
|
|
|
] = DataUpdateCoordinator(
|
|
|
|
hass,
|
|
|
|
LOGGER,
|
|
|
|
name=DOMAIN,
|
|
|
|
update_interval=SCAN_INTERVAL,
|
|
|
|
update_method=twentemilieu.update,
|
|
|
|
)
|
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2019-07-14 10:30:23 +00:00
|
|
|
|
2021-11-13 14:34:09 +00:00
|
|
|
# For backwards compat, set unique ID
|
|
|
|
if entry.unique_id is None:
|
2021-11-14 10:11:29 +00:00
|
|
|
hass.config_entries.async_update_entry(
|
|
|
|
entry, unique_id=str(entry.data[CONF_ID])
|
|
|
|
)
|
2019-07-14 10:30:23 +00:00
|
|
|
|
2021-11-13 14:34:09 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.data[CONF_ID]] = coordinator
|
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
2019-07-14 10:30:23 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-03-05 23:33:26 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
2019-07-14 10:30:23 +00:00
|
|
|
"""Unload Twente Milieu config entry."""
|
2021-04-27 20:19:57 +00:00
|
|
|
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
2021-11-13 14:34:09 +00:00
|
|
|
if unload_ok:
|
2021-11-14 10:11:29 +00:00
|
|
|
del hass.data[DOMAIN][entry.data[CONF_ID]]
|
2021-04-27 20:19:57 +00:00
|
|
|
return unload_ok
|