Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for Ridwell sensors."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Mapping
6 from datetime import date
7 from typing import Any
8 
9 from aioridwell.model import RidwellAccount
10 
12  SensorDeviceClass,
13  SensorEntity,
14  SensorEntityDescription,
15 )
16 from homeassistant.config_entries import ConfigEntry
17 from homeassistant.core import HomeAssistant
18 from homeassistant.helpers.entity_platform import AddEntitiesCallback
19 
20 from .const import DOMAIN, SENSOR_TYPE_NEXT_PICKUP
21 from .coordinator import RidwellDataUpdateCoordinator
22 from .entity import RidwellEntity
23 
24 ATTR_CATEGORY = "category"
25 ATTR_PICKUP_STATE = "pickup_state"
26 ATTR_PICKUP_TYPES = "pickup_types"
27 ATTR_QUANTITY = "quantity"
28 
29 SENSOR_DESCRIPTION = SensorEntityDescription(
30  key=SENSOR_TYPE_NEXT_PICKUP,
31  translation_key="next_pickup",
32  device_class=SensorDeviceClass.DATE,
33 )
34 
35 
37  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
38 ) -> None:
39  """Set up Ridwell sensors based on a config entry."""
40  coordinator: RidwellDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
41 
43  RidwellSensor(coordinator, account, SENSOR_DESCRIPTION)
44  for account in coordinator.accounts.values()
45  )
46 
47 
49  """Define a Ridwell pickup sensor."""
50 
51  def __init__(
52  self,
53  coordinator: RidwellDataUpdateCoordinator,
54  account: RidwellAccount,
55  description: SensorEntityDescription,
56  ) -> None:
57  """Initialize."""
58  super().__init__(coordinator, account)
59 
60  self._attr_unique_id_attr_unique_id = f"{account.account_id}_{description.key}"
61  self.entity_descriptionentity_description = description
62 
63  @property
64  def extra_state_attributes(self) -> Mapping[str, Any]:
65  """Return entity specific state attributes."""
66  attrs: dict[str, Any] = {
67  ATTR_PICKUP_TYPES: {},
68  ATTR_PICKUP_STATE: self.next_pickup_eventnext_pickup_event.state.value,
69  }
70 
71  for pickup in self.next_pickup_eventnext_pickup_event.pickups:
72  if pickup.name not in attrs[ATTR_PICKUP_TYPES]:
73  attrs[ATTR_PICKUP_TYPES][pickup.name] = {
74  ATTR_CATEGORY: pickup.category.value,
75  ATTR_QUANTITY: pickup.quantity,
76  }
77  else:
78  # Ridwell's API will return distinct objects, even if they have the
79  # same name (e.g. two pickups of Latex Paint will show up as two
80  # objects) – so, we sum the quantities:
81  attrs[ATTR_PICKUP_TYPES][pickup.name][ATTR_QUANTITY] += pickup.quantity
82 
83  return attrs
84 
85  @property
86  def native_value(self) -> date:
87  """Return the value reported by the sensor."""
88  return self.next_pickup_eventnext_pickup_event.pickup_date
None __init__(self, RidwellDataUpdateCoordinator coordinator, RidwellAccount account, SensorEntityDescription description)
Definition: sensor.py:56
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:38