Skip to content

Commit

Permalink
Generalize a base ReCollect Waste entity (home-assistant#85166)
Browse files Browse the repository at this point in the history
  • Loading branch information
bachya authored Jan 5, 2023
1 parent 280f6e4 commit 6b68d3d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
42 changes: 42 additions & 0 deletions homeassistant/components/recollect_waste/entity.py
Original file line number Diff line number Diff line change
@@ -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()
42 changes: 11 additions & 31 deletions homeassistant/components/recollect_waste/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@
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"
Expand Down Expand Up @@ -59,49 +57,31 @@ 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,
coordinator: DataUpdateCoordinator[list[PickupEvent]],
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]
Expand Down

0 comments on commit 6b68d3d

Please sign in to comment.