1 """Sensor support for Melnor Bluetooth water timer."""
3 from __future__
import annotations
5 from collections.abc
import Callable
6 from dataclasses
import dataclass
7 from datetime
import datetime
10 from melnor_bluetooth.device
import Device, Valve
15 SensorEntityDescription,
21 SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
29 from .const
import DOMAIN
30 from .coordinator
import MelnorDataUpdateCoordinator
31 from .entity
import MelnorBluetoothEntity, MelnorZoneEntity, get_entities_for_valves
35 """Calculate the number of minutes left in the current watering cycle."""
37 if valve.is_watering
is not True or dt_util.now() > dt_util.utc_from_timestamp(
38 valve.watering_end_time
42 return dt_util.utc_from_timestamp(valve.watering_end_time)
46 """Return the value of the next_cycle date, only if the cycle is enabled."""
48 if valve.schedule_enabled
is True:
49 return valve.next_cycle
54 @dataclass(frozen=True, kw_only=True)
56 """Describes Melnor sensor entity."""
58 state_fn: Callable[[Valve], Any]
61 @dataclass(frozen=True, kw_only=True)
63 """Describes Melnor sensor entity."""
65 state_fn: Callable[[Device], Any]
68 DEVICE_ENTITY_DESCRIPTIONS: list[MelnorSensorEntityDescription] = [
70 device_class=SensorDeviceClass.BATTERY,
71 entity_category=EntityCategory.DIAGNOSTIC,
73 native_unit_of_measurement=PERCENTAGE,
74 state_class=SensorStateClass.MEASUREMENT,
75 state_fn=
lambda device: device.battery_level,
78 device_class=SensorDeviceClass.SIGNAL_STRENGTH,
79 entity_category=EntityCategory.DIAGNOSTIC,
80 entity_registry_enabled_default=
False,
82 translation_key=
"rssi",
83 native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
84 state_class=SensorStateClass.MEASUREMENT,
85 state_fn=
lambda device: device.rssi,
89 ZONE_ENTITY_DESCRIPTIONS: list[MelnorZoneSensorEntityDescription] = [
91 device_class=SensorDeviceClass.TIMESTAMP,
92 key=
"manual_cycle_end",
93 translation_key=
"manual_cycle_end",
94 state_fn=watering_seconds_left,
97 device_class=SensorDeviceClass.TIMESTAMP,
99 translation_key=
"next_cycle",
107 config_entry: ConfigEntry,
108 async_add_entities: AddEntitiesCallback,
110 """Set up the sensor platform."""
112 coordinator: MelnorDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
120 for description
in DEVICE_ENTITY_DESCRIPTIONS
125 get_entities_for_valves(
127 ZONE_ENTITY_DESCRIPTIONS,
129 coordinator, description, valve
136 """Representation of a Melnor sensor."""
138 entity_description: MelnorSensorEntityDescription
142 coordinator: MelnorDataUpdateCoordinator,
143 entity_description: MelnorSensorEntityDescription,
145 """Initialize a sensor for a Melnor device."""
154 """Return the sensor value."""
159 """Representation of a Melnor sensor."""
161 entity_description: MelnorZoneSensorEntityDescription
165 coordinator: MelnorDataUpdateCoordinator,
166 entity_description: MelnorZoneSensorEntityDescription,
169 """Initialize a sensor for a Melnor device."""
170 super().
__init__(coordinator, entity_description, valve)
174 """Return the sensor value."""
StateType native_value(self)
None __init__(self, MelnorDataUpdateCoordinator coordinator, MelnorSensorEntityDescription entity_description)
None __init__(self, MelnorDataUpdateCoordinator coordinator, MelnorZoneSensorEntityDescription entity_description, Valve valve)
StateType native_value(self)
datetime|None watering_seconds_left(Valve valve)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
datetime|None next_cycle(Valve valve)