Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Sensor platform for Garages Amsterdam."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 
8 from odp_amsterdam import Garage
9 
11  SensorEntity,
12  SensorEntityDescription,
13  SensorStateClass,
14 )
15 from homeassistant.core import HomeAssistant
16 from homeassistant.helpers.entity_platform import AddEntitiesCallback
17 from homeassistant.helpers.typing import StateType
18 
19 from . import GaragesAmsterdamConfigEntry
20 from .coordinator import GaragesAmsterdamDataUpdateCoordinator
21 from .entity import GaragesAmsterdamEntity
22 
23 
24 @dataclass(frozen=True, kw_only=True)
26  """Class describing Garages Amsterdam sensor entity."""
27 
28  value_fn: Callable[[Garage], StateType]
29 
30 
31 SENSORS: tuple[GaragesAmsterdamSensorEntityDescription, ...] = (
33  key="free_space_short",
34  translation_key="free_space_short",
35  state_class=SensorStateClass.MEASUREMENT,
36  value_fn=lambda garage: garage.free_space_short,
37  ),
39  key="free_space_long",
40  translation_key="free_space_long",
41  state_class=SensorStateClass.MEASUREMENT,
42  value_fn=lambda garage: garage.free_space_long,
43  ),
45  key="short_capacity",
46  translation_key="short_capacity",
47  value_fn=lambda garage: garage.short_capacity,
48  ),
50  key="long_capacity",
51  translation_key="long_capacity",
52  value_fn=lambda garage: garage.long_capacity,
53  ),
54 )
55 
56 
58  hass: HomeAssistant,
59  entry: GaragesAmsterdamConfigEntry,
60  async_add_entities: AddEntitiesCallback,
61 ) -> None:
62  """Defer sensor setup to the shared sensor module."""
63  coordinator = entry.runtime_data
64 
67  coordinator=coordinator,
68  garage_name=entry.data["garage_name"],
69  description=description,
70  )
71  for description in SENSORS
72  if description.value_fn(coordinator.data[entry.data["garage_name"]]) is not None
73  )
74 
75 
77  """Sensor representing garages amsterdam data."""
78 
79  entity_description: GaragesAmsterdamSensorEntityDescription
80 
81  def __init__(
82  self,
83  *,
84  coordinator: GaragesAmsterdamDataUpdateCoordinator,
85  garage_name: str,
86  description: GaragesAmsterdamSensorEntityDescription,
87  ) -> None:
88  """Initialize garages amsterdam sensor."""
89  super().__init__(coordinator, garage_name)
90  self.entity_descriptionentity_description = description
91  self._attr_unique_id_attr_unique_id = f"{garage_name}-{description.key}"
92 
93  @property
94  def available(self) -> bool:
95  """Return if sensor is available."""
96  return self.coordinator.last_update_success and (
97  self._garage_name_garage_name in self.coordinator.data
98  )
99 
100  @property
101  def native_value(self) -> StateType:
102  """Return the state of the sensor."""
103  return self.entity_descriptionentity_description.value_fn(
104  self.coordinator.data[self._garage_name_garage_name]
105  )
None __init__(self, *GaragesAmsterdamDataUpdateCoordinator coordinator, str garage_name, GaragesAmsterdamSensorEntityDescription description)
Definition: sensor.py:87
None async_setup_entry(HomeAssistant hass, GaragesAmsterdamConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:61