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
|
|
|
|
|
2020-12-19 17:29:37 +00:00
|
|
|
from aiorecollect.client import PickupType
|
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
|
2021-12-16 15:35:18 +00:00
|
|
|
from homeassistant.const import CONF_FRIENDLY_NAME
|
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
|
2020-11-12 10:00:42 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
|
|
|
|
2022-01-06 15:05:48 +00:00
|
|
|
from .const import CONF_PLACE_ID, CONF_SERVICE_ID, DOMAIN, LOGGER
|
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,
|
|
|
|
name="Current Pickup",
|
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=SENSOR_TYPE_NEXT_PICKUP,
|
|
|
|
name="Next Pickup",
|
|
|
|
),
|
|
|
|
)
|
2019-01-29 08:38:01 +00:00
|
|
|
|
|
|
|
|
2020-12-19 17:29:37 +00:00
|
|
|
@callback
|
|
|
|
def async_get_pickup_type_names(
|
2021-03-18 13:31:38 +00:00
|
|
|
entry: ConfigEntry, pickup_types: list[PickupType]
|
|
|
|
) -> list[str]:
|
2020-12-19 17:29:37 +00:00
|
|
|
"""Return proper pickup type names from their associated objects."""
|
|
|
|
return [
|
|
|
|
t.friendly_name
|
|
|
|
if entry.options.get(CONF_FRIENDLY_NAME) and t.friendly_name
|
|
|
|
else t.name
|
|
|
|
for t in pickup_types
|
|
|
|
]
|
|
|
|
|
|
|
|
|
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."""
|
2021-11-14 18:12:34 +00:00
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
2022-01-06 15:05:48 +00:00
|
|
|
|
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
ReCollectWasteSensor(coordinator, entry, description)
|
|
|
|
for description in SENSOR_DESCRIPTIONS
|
|
|
|
]
|
|
|
|
)
|
2019-01-29 08:38:01 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class ReCollectWasteSensor(CoordinatorEntity, SensorEntity):
|
2020-12-17 19:59:45 +00:00
|
|
|
"""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
|
|
|
|
2022-01-06 15:05:48 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
description: SensorEntityDescription,
|
|
|
|
) -> None:
|
2019-01-29 08:38:01 +00:00
|
|
|
"""Initialize the sensor."""
|
2020-11-12 10:00:42 +00:00
|
|
|
super().__init__(coordinator)
|
2019-01-29 08:38:01 +00:00
|
|
|
|
2021-11-14 18:12:34 +00:00
|
|
|
self._attr_extra_state_attributes = {}
|
2022-01-06 15:05:48 +00:00
|
|
|
self._attr_unique_id = f"{entry.data[CONF_PLACE_ID]}_{entry.data[CONF_SERVICE_ID]}_{description.key}"
|
2021-07-02 16:14:29 +00:00
|
|
|
self._entry = entry
|
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:
|
|
|
|
"""Respond to a DataUpdateCoordinator update."""
|
|
|
|
self.update_from_latest_data()
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Handle entity which will be added."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
self.update_from_latest_data()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self) -> None:
|
|
|
|
"""Update the state."""
|
2022-01-06 15:05:48 +00:00
|
|
|
if self.entity_description.key == SENSOR_TYPE_CURRENT_PICKUP:
|
|
|
|
try:
|
|
|
|
event = self.coordinator.data[0]
|
|
|
|
except IndexError:
|
|
|
|
LOGGER.error("No current pickup found")
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
event = self.coordinator.data[1]
|
|
|
|
except IndexError:
|
|
|
|
LOGGER.info("No next pickup found")
|
|
|
|
return
|
2020-11-12 10:00:42 +00:00
|
|
|
|
2021-07-02 16:14:29 +00:00
|
|
|
self._attr_extra_state_attributes.update(
|
2020-11-09 22:31:48 +00:00
|
|
|
{
|
2020-12-19 17:29:37 +00:00
|
|
|
ATTR_PICKUP_TYPES: async_get_pickup_type_names(
|
2022-01-06 15:05:48 +00:00
|
|
|
self._entry, event.pickup_types
|
2020-12-19 17:29:37 +00:00
|
|
|
),
|
2022-01-06 15:05:48 +00:00
|
|
|
ATTR_AREA_NAME: event.area_name,
|
2020-11-09 22:31:48 +00:00
|
|
|
}
|
|
|
|
)
|
2022-01-06 15:05:48 +00:00
|
|
|
self._attr_native_value = event.date
|