From 6b68d3d365ac30b422349d4ceb894be4cdcd338b Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Thu, 5 Jan 2023 03:26:59 -0700 Subject: [PATCH] Generalize a base ReCollect Waste entity (#85166) --- .../components/recollect_waste/entity.py | 42 +++++++++++++++++++ .../components/recollect_waste/sensor.py | 42 +++++-------------- 2 files changed, 53 insertions(+), 31 deletions(-) create mode 100644 homeassistant/components/recollect_waste/entity.py diff --git a/homeassistant/components/recollect_waste/entity.py b/homeassistant/components/recollect_waste/entity.py new file mode 100644 index 00000000000..41781b10355 --- /dev/null +++ b/homeassistant/components/recollect_waste/entity.py @@ -0,0 +1,42 @@ +"""Define a base ReCollect Waste entity.""" +from aiorecollect.client import PickupEvent + +from homeassistant.config_entries import ConfigEntry +from homeassistant.helpers.device_registry import DeviceEntryType +from homeassistant.helpers.entity import DeviceInfo +from homeassistant.helpers.update_coordinator import ( + CoordinatorEntity, + DataUpdateCoordinator, +) + +from .const import CONF_PLACE_ID, CONF_SERVICE_ID, DOMAIN + + +class ReCollectWasteEntity(CoordinatorEntity[DataUpdateCoordinator[list[PickupEvent]]]): + """Define a base ReCollect Waste entity.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: DataUpdateCoordinator[list[PickupEvent]], + entry: ConfigEntry, + ) -> None: + """Initialize the sensor.""" + super().__init__(coordinator) + + self._identifier = f"{entry.data[CONF_PLACE_ID]}_{entry.data[CONF_SERVICE_ID]}" + + self._attr_device_info = DeviceInfo( + entry_type=DeviceEntryType.SERVICE, + identifiers={(DOMAIN, self._identifier)}, + manufacturer="ReCollect Waste", + name="ReCollect Waste", + ) + self._attr_extra_state_attributes = {} + self._entry = entry + + async def async_added_to_hass(self) -> None: + """Run when entity about to be added to hass.""" + await super().async_added_to_hass() + self._handle_coordinator_update() diff --git a/homeassistant/components/recollect_waste/sensor.py b/homeassistant/components/recollect_waste/sensor.py index 40e080b3fd3..eff3ce0b9a3 100644 --- a/homeassistant/components/recollect_waste/sensor.py +++ b/homeassistant/components/recollect_waste/sensor.py @@ -12,12 +12,10 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_FRIENDLY_NAME from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback -from homeassistant.helpers.update_coordinator import ( - CoordinatorEntity, - DataUpdateCoordinator, -) +from homeassistant.helpers.update_coordinator import DataUpdateCoordinator -from .const import CONF_PLACE_ID, CONF_SERVICE_ID, DOMAIN, LOGGER +from .const import DOMAIN, LOGGER +from .entity import ReCollectWasteEntity ATTR_PICKUP_TYPES = "pickup_types" ATTR_AREA_NAME = "area_name" @@ -59,20 +57,15 @@ async def async_setup_entry( ] async_add_entities( - [ - ReCollectWasteSensor(coordinator, entry, description) - for description in SENSOR_DESCRIPTIONS - ] + ReCollectWasteSensor(coordinator, entry, description) + for description in SENSOR_DESCRIPTIONS ) -class ReCollectWasteSensor( - CoordinatorEntity[DataUpdateCoordinator[list[PickupEvent]]], SensorEntity -): - """ReCollect Waste Sensor.""" +class ReCollectWasteSensor(ReCollectWasteEntity, SensorEntity): + """Define a ReCollect Waste sensor.""" _attr_device_class = SensorDeviceClass.DATE - _attr_has_entity_name = True def __init__( self, @@ -80,28 +73,15 @@ class ReCollectWasteSensor( entry: ConfigEntry, description: SensorEntityDescription, ) -> None: - """Initialize the sensor.""" - super().__init__(coordinator) + """Initialize.""" + super().__init__(coordinator, entry) - self._attr_extra_state_attributes = {} - self._attr_unique_id = f"{entry.data[CONF_PLACE_ID]}_{entry.data[CONF_SERVICE_ID]}_{description.key}" - self._entry = entry + self._attr_unique_id = f"{self._identifier}_{description.key}" self.entity_description = description @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.""" + """Handle updated data from the coordinator.""" if self.entity_description.key == SENSOR_TYPE_CURRENT_PICKUP: try: event = self.coordinator.data[0]