1 """Sensoterra devices."""
3 from __future__
import annotations
5 from datetime
import UTC, datetime, timedelta
6 from enum
import StrEnum, auto
8 from sensoterra.probe
import Probe, Sensor
13 SensorEntityDescription,
18 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
28 from .
import SensoterraConfigEntry
29 from .const
import CONFIGURATION_URL, DOMAIN, SENSOR_EXPIRATION_DAYS
30 from .coordinator
import SensoterraCoordinator
34 """Generic sensors within a Sensoterra probe."""
43 SENSORS: dict[ProbeSensorType, SensorEntityDescription] = {
45 key=ProbeSensorType.MOISTURE,
46 state_class=SensorStateClass.MEASUREMENT,
47 suggested_display_precision=0,
48 device_class=SensorDeviceClass.MOISTURE,
49 native_unit_of_measurement=PERCENTAGE,
50 translation_key=
"soil_moisture_at_cm",
53 key=ProbeSensorType.SI,
54 state_class=SensorStateClass.MEASUREMENT,
55 suggested_display_precision=1,
56 translation_key=
"si_at_cm",
59 key=ProbeSensorType.TEMPERATURE,
60 state_class=SensorStateClass.MEASUREMENT,
61 suggested_display_precision=0,
62 device_class=SensorDeviceClass.TEMPERATURE,
63 native_unit_of_measurement=UnitOfTemperature.CELSIUS,
66 key=ProbeSensorType.BATTERY,
67 state_class=SensorStateClass.MEASUREMENT,
68 suggested_display_precision=0,
69 device_class=SensorDeviceClass.BATTERY,
70 native_unit_of_measurement=PERCENTAGE,
71 entity_category=EntityCategory.DIAGNOSTIC,
74 key=ProbeSensorType.RSSI,
75 state_class=SensorStateClass.MEASUREMENT,
76 suggested_display_precision=0,
77 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
78 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
79 entity_category=EntityCategory.DIAGNOSTIC,
80 entity_registry_enabled_default=
False,
87 entry: SensoterraConfigEntry,
88 async_add_devices: AddEntitiesCallback,
90 """Set up Sensoterra sensor."""
92 coordinator = entry.runtime_data
95 def _async_add_devices(probes: list[Probe]) ->
None:
96 aha = coordinator.async_contexts()
97 current_sensors = set(aha)
103 SENSORS[ProbeSensorType[sensor.type]],
106 for sensor
in probe.sensors()
107 if sensor.type
is not None
108 and sensor.type.lower()
in SENSORS
109 and sensor.id
not in current_sensors
112 coordinator.add_devices_callback = _async_add_devices
114 _async_add_devices(coordinator.data)
118 """Sensoterra sensor like a soil moisture or temperature sensor."""
120 _attr_has_entity_name =
True
124 coordinator: SensoterraCoordinator,
127 entity_description: SensorEntityDescription,
129 """Initialize entity."""
130 super().
__init__(coordinator, context=sensor.id)
135 "depth":
"?" if sensor.depth
is None else str(sensor.depth)
141 identifiers={(DOMAIN, probe.serial)},
144 manufacturer=
"Sensoterra",
145 serial_number=probe.serial,
146 suggested_area=probe.location,
147 configuration_url=CONFIGURATION_URL,
152 """Return the sensor, or None if it doesn't exist."""
157 """Return the value reported by the sensor."""
159 return self.
sensorsensor.value
163 """Return True if entity is available."""
164 if not super().available
or (sensor := self.
sensorsensor)
is None:
167 if sensor.timestamp
is None:
171 expiration = datetime.now(UTC) -
timedelta(days=SENSOR_EXPIRATION_DAYS)
172 return sensor.timestamp >= expiration
Sensor|None get_sensor(self, str|None id)
StateType native_value(self)
_attr_translation_placeholders
None __init__(self, SensoterraCoordinator coordinator, Probe probe, Sensor sensor, SensorEntityDescription entity_description)
def async_add_devices(address, multiple)
None async_setup_entry(HomeAssistant hass, SensoterraConfigEntry entry, AddEntitiesCallback async_add_devices)