1 """Support for MySensors sensors."""
3 from __future__
import annotations
7 from awesomeversion
import AwesomeVersion
8 from mysensors
import BaseAsyncGateway
13 SensorEntityDescription,
24 UnitOfElectricCurrent,
25 UnitOfElectricPotential,
41 from .
import setup_mysensors_platform
48 MYSENSORS_NODE_DISCOVERY,
52 from .entity
import MySensorNodeEntity, MySensorsChildEntity
53 from .helpers
import on_unload
55 SENSORS: dict[str, SensorEntityDescription] = {
58 device_class=SensorDeviceClass.TEMPERATURE,
59 state_class=SensorStateClass.MEASUREMENT,
63 native_unit_of_measurement=PERCENTAGE,
64 device_class=SensorDeviceClass.HUMIDITY,
65 state_class=SensorStateClass.MEASUREMENT,
69 native_unit_of_measurement=PERCENTAGE,
74 native_unit_of_measurement=PERCENTAGE,
83 icon=
"mdi:weather-partly-cloudy",
87 icon=
"mdi:weather-rainy",
91 icon=
"mdi:weather-rainy",
95 icon=
"mdi:weather-windy",
99 icon=
"mdi:weather-windy",
103 native_unit_of_measurement=DEGREE,
108 native_unit_of_measurement=UnitOfMass.KILOGRAMS,
109 device_class=SensorDeviceClass.WEIGHT,
113 native_unit_of_measurement=UnitOfLength.METERS,
114 device_class=SensorDeviceClass.DISTANCE,
118 native_unit_of_measurement=
"ohm",
122 native_unit_of_measurement=UnitOfPower.WATT,
123 device_class=SensorDeviceClass.POWER,
124 state_class=SensorStateClass.MEASUREMENT,
128 native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR,
129 device_class=SensorDeviceClass.ENERGY,
130 state_class=SensorStateClass.TOTAL_INCREASING,
134 native_unit_of_measurement=PERCENTAGE,
135 icon=
"mdi:white-balance-sunny",
141 native_unit_of_measurement=UnitOfLength.METERS,
146 native_unit_of_measurement=UnitOfVolume.CUBIC_METERS,
147 device_class=SensorDeviceClass.VOLUME,
150 key=
"V_LEVEL_S_SOUND",
151 native_unit_of_measurement=UnitOfSoundPressure.DECIBEL,
152 device_class=SensorDeviceClass.SOUND_PRESSURE,
155 key=
"V_LEVEL_S_VIBRATION",
156 native_unit_of_measurement=UnitOfFrequency.HERTZ,
159 key=
"V_LEVEL_S_LIGHT_LEVEL",
160 native_unit_of_measurement=LIGHT_LUX,
161 device_class=SensorDeviceClass.ILLUMINANCE,
162 state_class=SensorStateClass.MEASUREMENT,
165 key=
"V_LEVEL_S_MOISTURE",
166 native_unit_of_measurement=PERCENTAGE,
167 icon=
"mdi:water-percent",
171 native_unit_of_measurement=UnitOfElectricPotential.VOLT,
172 device_class=SensorDeviceClass.VOLTAGE,
173 state_class=SensorStateClass.MEASUREMENT,
177 native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
178 device_class=SensorDeviceClass.CURRENT,
179 state_class=SensorStateClass.MEASUREMENT,
187 native_unit_of_measurement=
"pH",
191 native_unit_of_measurement=UnitOfElectricPotential.MILLIVOLT,
192 device_class=SensorDeviceClass.VOLTAGE,
196 native_unit_of_measurement=UnitOfConductivity.MICROSIEMENS_PER_CM,
200 native_unit_of_measurement=UnitOfReactivePower.VOLT_AMPERE_REACTIVE,
204 native_unit_of_measurement=UnitOfApparentPower.VOLT_AMPERE,
205 device_class=SensorDeviceClass.APPARENT_POWER,
212 config_entry: ConfigEntry,
213 async_add_entities: AddEntitiesCallback,
215 """Set up this platform for a specific ConfigEntry(==Gateway)."""
218 """Discover and add a MySensors sensor."""
224 async_add_entities=async_add_entities,
229 """Add battery sensor for each MySensors node."""
230 gateway_id = discovery_info[ATTR_GATEWAY_ID]
231 node_id = discovery_info[ATTR_NODE_ID]
232 gateway: BaseAsyncGateway = hass.data[DOMAIN][MYSENSORS_GATEWAYS][gateway_id]
237 config_entry.entry_id,
240 MYSENSORS_DISCOVERY.format(config_entry.entry_id, Platform.SENSOR),
247 config_entry.entry_id,
250 MYSENSORS_NODE_DISCOVERY,
257 """Battery sensor of MySensors node."""
259 _attr_device_class = SensorDeviceClass.BATTERY
260 _attr_state_class = SensorStateClass.MEASUREMENT
261 _attr_native_unit_of_measurement = PERCENTAGE
262 _attr_force_update =
True
266 """Return a unique ID for use in home assistant."""
267 return f
"{self.gateway_id}-{self.node_id}-battery"
271 """Return the name of this entity."""
272 return f
"{self.node_name} Battery"
276 """Update the controller with the latest battery level."""
282 """Representation of a MySensors Sensor child node."""
284 _attr_force_update =
True
286 def __init__(self, *args: Any, **kwargs: Any) ->
None:
287 """Set up the instance."""
294 """Return the state of the sensor."""
295 return self._values.
get(self.value_type)
299 """Return the unit of measurement of this entity."""
300 set_req = self.gateway.const.SetReq
302 AwesomeVersion(self.gateway.protocol_version) >= AwesomeVersion(
"1.5")
303 and set_req.V_UNIT_PREFIX
in self._values
305 custom_unit: str = self._values[set_req.V_UNIT_PREFIX]
308 if set_req(self.value_type) == set_req.V_TEMP:
309 if self.
hasshass.config.units
is METRIC_SYSTEM:
310 return UnitOfTemperature.CELSIUS
311 return UnitOfTemperature.FAHRENHEIT
313 if hasattr(self,
"entity_description"):
318 """Return the sensor entity description."""
319 set_req = self.gateway.const.SetReq
320 entity_description = SENSORS.get(set_req(self.value_type).name)
322 if not entity_description:
323 presentation = self.gateway.const.Presentation
324 entity_description = SENSORS.get(
325 f
"{set_req(self.value_type).name}_{presentation(self.child_type).name}"
328 return entity_description
None _async_update_callback(self)
None async_write_ha_state(self)
web.Response get(self, web.Request request, str config_key)
None on_unload(HomeAssistant hass, GatewayId gateway_id, Callable fnct)
None async_discover(DiscoveryInfo discovery_info)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
None async_node_discover(NodeDiscoveryInfo discovery_info)
list[MySensorsChildEntity]|None setup_mysensors_platform(HomeAssistant hass, Platform domain, DiscoveryInfo discovery_info, type[MySensorsChildEntity]|Mapping[SensorType, type[MySensorsChildEntity]] device_class,(tuple|None) device_args=None, Callable|None async_add_entities=None)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)