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
|
|
|
import voluptuous as vol
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2020-11-12 10:00:42 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2021-04-08 05:34:47 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
CONF_FRIENDLY_NAME,
|
|
|
|
CONF_NAME,
|
|
|
|
DEVICE_CLASS_TIMESTAMP,
|
|
|
|
)
|
2020-11-12 10:00:42 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers import config_validation as cv
|
2021-04-29 10:28:14 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-07-07 22:40:03 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2020-11-12 10:00:42 +00:00
|
|
|
from homeassistant.helpers.update_coordinator import (
|
|
|
|
CoordinatorEntity,
|
|
|
|
DataUpdateCoordinator,
|
|
|
|
)
|
2021-04-08 05:34:47 +00:00
|
|
|
from homeassistant.util.dt import as_utc
|
2020-11-12 10:00:42 +00:00
|
|
|
|
|
|
|
from .const import CONF_PLACE_ID, CONF_SERVICE_ID, DATA_COORDINATOR, 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-10-28 21:37:37 +00:00
|
|
|
ATTR_NEXT_PICKUP_TYPES = "next_pickup_types"
|
|
|
|
ATTR_NEXT_PICKUP_DATE = "next_pickup_date"
|
2020-11-12 10:00:42 +00:00
|
|
|
|
2020-12-17 19:59:45 +00:00
|
|
|
DEFAULT_ATTRIBUTION = "Pickup data provided by ReCollect Waste"
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "recollect_waste"
|
2019-01-29 08:38:01 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PLACE_ID): cv.string,
|
|
|
|
vol.Required(CONF_SERVICE_ID): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
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_platform(
|
|
|
|
hass: HomeAssistant,
|
2021-07-07 22:40:03 +00:00
|
|
|
config: ConfigType,
|
2021-04-29 10:28:14 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-07-07 22:40:03 +00:00
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2020-12-19 17:29:37 +00:00
|
|
|
"""Import Recollect Waste configuration from YAML."""
|
2020-11-12 10:00:42 +00:00
|
|
|
LOGGER.warning(
|
2021-03-19 14:26:36 +00:00
|
|
|
"Loading ReCollect Waste via platform setup is deprecated; "
|
|
|
|
"Please remove it from your configuration"
|
2020-11-12 10:00:42 +00:00
|
|
|
)
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": SOURCE_IMPORT},
|
|
|
|
data=config,
|
|
|
|
)
|
|
|
|
)
|
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."""
|
2020-11-12 10:00:42 +00:00
|
|
|
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
2020-12-17 19:59:45 +00:00
|
|
|
async_add_entities([ReCollectWasteSensor(coordinator, entry)])
|
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-07-02 16:14:29 +00:00
|
|
|
_attr_device_class = DEVICE_CLASS_TIMESTAMP
|
|
|
|
|
2020-11-12 10:00:42 +00:00
|
|
|
def __init__(self, coordinator: DataUpdateCoordinator, entry: ConfigEntry) -> 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-07-02 16:14:29 +00:00
|
|
|
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
|
|
|
self._attr_name = DEFAULT_NAME
|
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{entry.data[CONF_PLACE_ID]}{entry.data[CONF_SERVICE_ID]}"
|
|
|
|
)
|
|
|
|
self._entry = entry
|
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."""
|
|
|
|
pickup_event = self.coordinator.data[0]
|
|
|
|
next_pickup_event = self.coordinator.data[1]
|
|
|
|
|
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(
|
|
|
|
self._entry, pickup_event.pickup_types
|
|
|
|
),
|
2020-11-09 22:31:48 +00:00
|
|
|
ATTR_AREA_NAME: pickup_event.area_name,
|
2020-12-19 17:29:37 +00:00
|
|
|
ATTR_NEXT_PICKUP_TYPES: async_get_pickup_type_names(
|
|
|
|
self._entry, next_pickup_event.pickup_types
|
|
|
|
),
|
2021-04-08 05:34:47 +00:00
|
|
|
ATTR_NEXT_PICKUP_DATE: as_utc(next_pickup_event.date).isoformat(),
|
2020-11-09 22:31:48 +00:00
|
|
|
}
|
|
|
|
)
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = as_utc(pickup_event.date).isoformat()
|