Add unique_id property to ARWN sensors (#42570)

* Add unique_id property

After HA 0.111 the ARWN platform's lack of a unique_id causes the
sensors to fail to register correctly. The unique_id for this is just
a slug which is the mqtt topic that it's inbound on.

Also change the logging to INFO for data received to make it fit
better in the logging levels and show up during '-v' runs.

* use topic as unique_id

fix logging

* fix final topic location

* fix black

* address the fact that some topics have more than one sensor

This splits up rain and wind sensors to unique topics

* address review comments
This commit is contained in:
Sean Dague 2020-11-05 08:55:53 -05:00 committed by GitHub
parent 50761eb4e4
commit 2a576e979b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -30,26 +30,30 @@ def discover_sensors(topic, payload):
unit = TEMP_FAHRENHEIT unit = TEMP_FAHRENHEIT
else: else:
unit = TEMP_CELSIUS unit = TEMP_CELSIUS
return ArwnSensor(name, "temp", unit) return ArwnSensor(topic, name, "temp", unit)
if domain == "moisture": if domain == "moisture":
name = f"{parts[2]} Moisture" name = f"{parts[2]} Moisture"
return ArwnSensor(name, "moisture", unit, "mdi:water-percent") return ArwnSensor(topic, name, "moisture", unit, "mdi:water-percent")
if domain == "rain": if domain == "rain":
if len(parts) >= 3 and parts[2] == "today": if len(parts) >= 3 and parts[2] == "today":
return ArwnSensor( return ArwnSensor(
"Rain Since Midnight", "since_midnight", "in", "mdi:water" topic, "Rain Since Midnight", "since_midnight", "in", "mdi:water"
) )
return ( return (
ArwnSensor("Total Rainfall", "total", unit, "mdi:water"), ArwnSensor(topic + "/total", "Total Rainfall", "total", unit, "mdi:water"),
ArwnSensor("Rainfall Rate", "rate", unit, "mdi:water"), ArwnSensor(topic + "/rate", "Rainfall Rate", "rate", unit, "mdi:water"),
) )
if domain == "barometer": if domain == "barometer":
return ArwnSensor("Barometer", "pressure", unit, "mdi:thermometer-lines") return ArwnSensor(topic, "Barometer", "pressure", unit, "mdi:thermometer-lines")
if domain == "wind": if domain == "wind":
return ( return (
ArwnSensor("Wind Speed", "speed", unit, "mdi:speedometer"), ArwnSensor(
ArwnSensor("Wind Gust", "gust", unit, "mdi:speedometer"), topic + "/speed", "Wind Speed", "speed", unit, "mdi:speedometer"
ArwnSensor("Wind Direction", "direction", DEGREE, "mdi:compass"), ),
ArwnSensor(topic + "/gust", "Wind Gust", "gust", unit, "mdi:speedometer"),
ArwnSensor(
topic + "/dir", "Wind Direction", "direction", DEGREE, "mdi:compass"
),
) )
@ -95,11 +99,15 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
sensor.set_event(event) sensor.set_event(event)
store[sensor.name] = sensor store[sensor.name] = sensor
_LOGGER.debug( _LOGGER.debug(
"Registering new sensor %(name)s => %(event)s", "Registering sensor %(name)s => %(event)s",
{"name": sensor.name, "event": event}, {"name": sensor.name, "event": event},
) )
async_add_entities((sensor,), True) async_add_entities((sensor,), True)
else: else:
_LOGGER.debug(
"Recording sensor %(name)s => %(event)s",
{"name": sensor.name, "event": event},
)
store[sensor.name].set_event(event) store[sensor.name].set_event(event)
await mqtt.async_subscribe(hass, TOPIC, async_sensor_event_received, 0) await mqtt.async_subscribe(hass, TOPIC, async_sensor_event_received, 0)
@ -109,11 +117,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class ArwnSensor(Entity): class ArwnSensor(Entity):
"""Representation of an ARWN sensor.""" """Representation of an ARWN sensor."""
def __init__(self, name, state_key, units, icon=None): def __init__(self, topic, name, state_key, units, icon=None):
"""Initialize the sensor.""" """Initialize the sensor."""
self.hass = None self.hass = None
self.entity_id = _slug(name) self.entity_id = _slug(name)
self._name = name self._name = name
# This mqtt topic for the sensor which is its uid
self._uid = topic
self._state_key = state_key self._state_key = state_key
self.event = {} self.event = {}
self._unit_of_measurement = units self._unit_of_measurement = units
@ -135,6 +145,14 @@ class ArwnSensor(Entity):
"""Get the name of the sensor.""" """Get the name of the sensor."""
return self._name return self._name
@property
def unique_id(self):
"""Return a unique ID.
This is based on the topic that comes from mqtt
"""
return self._uid
@property @property
def state_attributes(self): def state_attributes(self):
"""Return all the state attributes.""" """Return all the state attributes."""