Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Summary data from Fyta."""
2 
3 from __future__ import annotations
4 
5 from collections.abc import Callable
6 from dataclasses import dataclass
7 from datetime import datetime
8 from typing import Final
9 
10 from fyta_cli.fyta_models import Plant
11 
13  SensorDeviceClass,
14  SensorEntity,
15  SensorEntityDescription,
16  SensorStateClass,
17 )
18 from homeassistant.const import (
19  PERCENTAGE,
20  EntityCategory,
21  UnitOfConductivity,
22  UnitOfTemperature,
23 )
24 from homeassistant.core import HomeAssistant
25 from homeassistant.helpers.entity_platform import AddEntitiesCallback
26 from homeassistant.helpers.typing import StateType
27 
28 from . import FytaConfigEntry
29 from .coordinator import FytaCoordinator
30 from .entity import FytaPlantEntity
31 
32 
33 @dataclass(frozen=True, kw_only=True)
35  """Describes Fyta sensor entity."""
36 
37  value_fn: Callable[[Plant], StateType | datetime]
38 
39 
40 PLANT_STATUS_LIST: list[str] = ["deleted", "doing_great", "need_attention", "no_sensor"]
41 PLANT_MEASUREMENT_STATUS_LIST: list[str] = [
42  "no_data",
43  "too_low",
44  "low",
45  "perfect",
46  "high",
47  "too_high",
48 ]
49 
50 
51 SENSORS: Final[list[FytaSensorEntityDescription]] = [
53  key="scientific_name",
54  translation_key="scientific_name",
55  value_fn=lambda plant: plant.scientific_name,
56  ),
58  key="status",
59  translation_key="plant_status",
60  device_class=SensorDeviceClass.ENUM,
61  options=PLANT_STATUS_LIST,
62  value_fn=lambda plant: plant.status.name.lower(),
63  ),
65  key="temperature_status",
66  translation_key="temperature_status",
67  device_class=SensorDeviceClass.ENUM,
68  options=PLANT_MEASUREMENT_STATUS_LIST,
69  value_fn=lambda plant: plant.temperature_status.name.lower(),
70  ),
72  key="light_status",
73  translation_key="light_status",
74  device_class=SensorDeviceClass.ENUM,
75  options=PLANT_MEASUREMENT_STATUS_LIST,
76  value_fn=lambda plant: plant.light_status.name.lower(),
77  ),
79  key="moisture_status",
80  translation_key="moisture_status",
81  device_class=SensorDeviceClass.ENUM,
82  options=PLANT_MEASUREMENT_STATUS_LIST,
83  value_fn=lambda plant: plant.moisture_status.name.lower(),
84  ),
86  key="salinity_status",
87  translation_key="salinity_status",
88  device_class=SensorDeviceClass.ENUM,
89  options=PLANT_MEASUREMENT_STATUS_LIST,
90  value_fn=lambda plant: plant.salinity_status.name.lower(),
91  ),
93  key="temperature",
94  native_unit_of_measurement=UnitOfTemperature.CELSIUS,
95  device_class=SensorDeviceClass.TEMPERATURE,
96  state_class=SensorStateClass.MEASUREMENT,
97  value_fn=lambda plant: plant.temperature,
98  ),
100  key="light",
101  translation_key="light",
102  native_unit_of_measurement="μmol/s⋅m²",
103  state_class=SensorStateClass.MEASUREMENT,
104  value_fn=lambda plant: plant.light,
105  ),
107  key="moisture",
108  native_unit_of_measurement=PERCENTAGE,
109  device_class=SensorDeviceClass.MOISTURE,
110  state_class=SensorStateClass.MEASUREMENT,
111  value_fn=lambda plant: plant.moisture,
112  ),
114  key="salinity",
115  translation_key="salinity",
116  native_unit_of_measurement=UnitOfConductivity.MILLISIEMENS_PER_CM,
117  device_class=SensorDeviceClass.CONDUCTIVITY,
118  state_class=SensorStateClass.MEASUREMENT,
119  value_fn=lambda plant: plant.salinity,
120  ),
122  key="ph",
123  device_class=SensorDeviceClass.PH,
124  state_class=SensorStateClass.MEASUREMENT,
125  value_fn=lambda plant: plant.ph,
126  ),
128  key="battery_level",
129  native_unit_of_measurement=PERCENTAGE,
130  device_class=SensorDeviceClass.BATTERY,
131  state_class=SensorStateClass.MEASUREMENT,
132  entity_category=EntityCategory.DIAGNOSTIC,
133  value_fn=lambda plant: plant.battery_level,
134  ),
135 ]
136 
137 
139  hass: HomeAssistant, entry: FytaConfigEntry, async_add_entities: AddEntitiesCallback
140 ) -> None:
141  """Set up the FYTA sensors."""
142  coordinator: FytaCoordinator = entry.runtime_data
143 
144  plant_entities = [
145  FytaPlantSensor(coordinator, entry, sensor, plant_id)
146  for plant_id in coordinator.fyta.plant_list
147  for sensor in SENSORS
148  if sensor.key in dir(coordinator.data.get(plant_id))
149  ]
150 
151  async_add_entities(plant_entities)
152 
153  def _async_add_new_device(plant_id: int) -> None:
155  FytaPlantSensor(coordinator, entry, sensor, plant_id)
156  for sensor in SENSORS
157  if sensor.key in dir(coordinator.data.get(plant_id))
158  )
159 
160  coordinator.new_device_callbacks.append(_async_add_new_device)
161 
162 
164  """Represents a Fyta sensor."""
165 
166  entity_description: FytaSensorEntityDescription
167 
168  @property
169  def native_value(self) -> StateType | datetime:
170  """Return the state for this sensor."""
171 
172  return self.entity_descriptionentity_description.value_fn(self.plantplant)
None async_setup_entry(HomeAssistant hass, FytaConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:140