2020-12-17 19:59:45 +00:00
|
|
|
"""Support for ReCollect Waste sensors."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-01-09 16:50:42 +00:00
|
|
|
from datetime import date
|
|
|
|
|
|
|
|
from aiorecollect.client import PickupEvent
|
2019-01-29 08:38:01 +00:00
|
|
|
|
2022-01-06 15:05:48 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2021-08-29 20:03:44 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-11-12 10:00:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2023-01-05 10:26:59 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2020-11-12 10:00:42 +00:00
|
|
|
|
2023-01-05 10:26:59 +00:00
|
|
|
from .const import DOMAIN, LOGGER
|
|
|
|
from .entity import ReCollectWasteEntity
|
2023-01-09 16:50:42 +00:00
|
|
|
from .util import async_get_pickup_type_names
|
2019-01-29 08:38:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_PICKUP_TYPES = "pickup_types"
|
|
|
|
ATTR_AREA_NAME = "area_name"
|
2020-11-12 10:00:42 +00:00
|
|
|
|
2022-01-06 15:05:48 +00:00
|
|
|
SENSOR_TYPE_CURRENT_PICKUP = "current_pickup"
|
|
|
|
SENSOR_TYPE_NEXT_PICKUP = "next_pickup"
|
|
|
|
|
|
|
|
SENSOR_DESCRIPTIONS = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_CURRENT_PICKUP,
|
2022-07-10 20:11:02 +00:00
|
|
|
name="Current pickup",
|
2022-01-06 15:05:48 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_NEXT_PICKUP,
|
2022-07-10 20:11:02 +00:00
|
|
|
name="Next pickup",
|
2022-01-06 15:05:48 +00:00
|
|
|
),
|
|
|
|
)
|
2019-01-29 08:38:01 +00:00
|
|
|
|
|
|
|
|
2020-11-12 10:00:42 +00:00
|
|
|
async def async_setup_entry(
|
2021-04-29 10:28:14 +00:00
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
2020-11-12 10:00:42 +00:00
|
|
|
) -> None:
|
2020-12-17 19:59:45 +00:00
|
|
|
"""Set up ReCollect Waste sensors based on a config entry."""
|
2022-12-29 08:07:23 +00:00
|
|
|
coordinator: DataUpdateCoordinator[list[PickupEvent]] = hass.data[DOMAIN][
|
|
|
|
entry.entry_id
|
|
|
|
]
|
2022-01-06 15:05:48 +00:00
|
|
|
|
|
|
|
async_add_entities(
|
2023-01-05 10:26:59 +00:00
|
|
|
ReCollectWasteSensor(coordinator, entry, description)
|
|
|
|
for description in SENSOR_DESCRIPTIONS
|
2022-01-06 15:05:48 +00:00
|
|
|
)
|
2019-01-29 08:38:01 +00:00
|
|
|
|
|
|
|
|
2023-01-05 10:26:59 +00:00
|
|
|
class ReCollectWasteSensor(ReCollectWasteEntity, SensorEntity):
|
|
|
|
"""Define a ReCollect Waste sensor."""
|
2019-01-29 08:38:01 +00:00
|
|
|
|
2021-12-16 15:35:18 +00:00
|
|
|
_attr_device_class = SensorDeviceClass.DATE
|
2021-07-02 16:14:29 +00:00
|
|
|
|
2023-01-09 16:50:42 +00:00
|
|
|
PICKUP_INDEX_MAP = {
|
|
|
|
SENSOR_TYPE_CURRENT_PICKUP: 1,
|
|
|
|
SENSOR_TYPE_NEXT_PICKUP: 2,
|
|
|
|
}
|
|
|
|
|
2022-01-06 15:05:48 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
2022-12-29 08:07:23 +00:00
|
|
|
coordinator: DataUpdateCoordinator[list[PickupEvent]],
|
2022-01-06 15:05:48 +00:00
|
|
|
entry: ConfigEntry,
|
|
|
|
description: SensorEntityDescription,
|
|
|
|
) -> None:
|
2023-01-05 10:26:59 +00:00
|
|
|
"""Initialize."""
|
|
|
|
super().__init__(coordinator, entry)
|
2019-01-29 08:38:01 +00:00
|
|
|
|
2023-01-05 10:26:59 +00:00
|
|
|
self._attr_unique_id = f"{self._identifier}_{description.key}"
|
2022-01-06 15:05:48 +00:00
|
|
|
self.entity_description = description
|
2020-11-12 10:00:42 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
2023-01-05 10:26:59 +00:00
|
|
|
"""Handle updated data from the coordinator."""
|
2023-01-09 16:50:42 +00:00
|
|
|
relevant_events = (e for e in self.coordinator.data if e.date >= date.today())
|
|
|
|
pickup_index = self.PICKUP_INDEX_MAP[self.entity_description.key]
|
|
|
|
|
|
|
|
try:
|
|
|
|
for _ in range(pickup_index):
|
|
|
|
event = next(relevant_events)
|
|
|
|
except StopIteration:
|
2023-01-12 18:56:06 +00:00
|
|
|
LOGGER.debug("No pickup event found for %s", self.entity_description.key)
|
2023-01-09 16:50:42 +00:00
|
|
|
self._attr_extra_state_attributes = {}
|
|
|
|
self._attr_native_value = None
|
2022-01-06 15:05:48 +00:00
|
|
|
else:
|
2023-01-09 16:50:42 +00:00
|
|
|
self._attr_extra_state_attributes[ATTR_AREA_NAME] = event.area_name
|
|
|
|
self._attr_extra_state_attributes[
|
|
|
|
ATTR_PICKUP_TYPES
|
|
|
|
] = async_get_pickup_type_names(self._entry, event.pickup_types)
|
|
|
|
self._attr_native_value = event.date
|