1 """Support for collecting data from the ARWN project."""
3 from __future__
import annotations
17 _LOGGER = logging.getLogger(__name__)
26 """Given a topic, dynamically create the right sensor type.
30 parts = topic.split(
"/")
31 unit = payload.get(
"units",
"")
33 if domain ==
"temperature":
36 unit = UnitOfTemperature.FAHRENHEIT
38 unit = UnitOfTemperature.CELSIUS
41 topic, name,
"temp", unit, device_class=SensorDeviceClass.TEMPERATURE
44 if domain ==
"moisture":
45 name = f
"{parts[2]} Moisture"
46 return [
ArwnSensor(topic, name,
"moisture", unit,
"mdi:water-percent")]
48 if len(parts) >= 3
and parts[2] ==
"today":
52 "Rain Since Midnight",
54 UnitOfPrecipitationDepth.INCHES,
55 device_class=SensorDeviceClass.PRECIPITATION,
64 device_class=SensorDeviceClass.PRECIPITATION,
71 device_class=SensorDeviceClass.PRECIPITATION,
74 if domain ==
"barometer":
76 ArwnSensor(topic,
"Barometer",
"pressure", unit,
"mdi:thermometer-lines")
85 device_class=SensorDeviceClass.WIND_SPEED,
92 device_class=SensorDeviceClass.WIND_SPEED,
95 topic +
"/dir",
"Wind Direction",
"direction", DEGREE,
"mdi:compass"
102 return f
"sensor.arwn_{slugify(name)}"
108 async_add_entities: AddEntitiesCallback,
109 discovery_info: DiscoveryInfoType |
None =
None,
111 """Set up the ARWN platform."""
114 if not await mqtt.async_wait_for_mqtt_client(hass):
115 _LOGGER.error(
"MQTT integration is not available")
119 def async_sensor_event_received(msg: mqtt.ReceiveMessage) ->
None:
120 """Process events as sensors.
122 When a new event on our topic (arwn/#) is received we map it
123 into a known kind of sensor based on topic name. If we've
124 never seen this before, we keep this sensor around in a global
125 cache. If we have seen it before, we update the values of the
126 existing sensor. Either way, we push an ha state update at the
127 end for the new event we've seen.
129 This lets us dynamically incorporate sensors without any
130 configuration on our side.
137 if (store := hass.data.get(DATA_ARWN))
is None:
138 store = hass.data[DATA_ARWN] = {}
140 if "timestamp" in event:
141 del event[
"timestamp"]
143 for sensor
in sensors:
144 if sensor.name
not in store:
146 sensor.set_event(event)
147 store[sensor.name] = sensor
149 "Registering sensor %(name)s => %(event)s",
150 {
"name": sensor.name,
"event": event},
155 "Recording sensor %(name)s => %(event)s",
156 {
"name": sensor.name,
"event": event},
158 store[sensor.name].set_event(event)
160 await mqtt.async_subscribe(hass, TOPIC, async_sensor_event_received, 0)
164 """Representation of an ARWN sensor."""
166 _attr_should_poll =
False
174 icon: str |
None =
None,
175 device_class: SensorDeviceClass |
None =
None,
177 """Initialize the sensor."""
188 """Update the sensor with the most recent event."""
189 ev: dict[str, Any] = {}
_attr_extra_state_attributes
_attr_native_unit_of_measurement
None set_event(self, dict[str, Any] event)
None __init__(self, str topic, str name, str state_key, str units, str|None icon=None, SensorDeviceClass|None device_class=None)
None async_write_ha_state(self)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
list[ArwnSensor]|None discover_sensors(str topic, dict[str, Any] payload)
JsonObjectType json_loads_object(bytes|bytearray|memoryview|str obj)