From 2a576e979b7cca7be0604a59cb182e414ce20490 Mon Sep 17 00:00:00 2001 From: Sean Dague Date: Thu, 5 Nov 2020 08:55:53 -0500 Subject: [PATCH] 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 --- homeassistant/components/arwn/sensor.py | 40 ++++++++++++++++++------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/arwn/sensor.py b/homeassistant/components/arwn/sensor.py index 3db1283279e..18186e8b871 100644 --- a/homeassistant/components/arwn/sensor.py +++ b/homeassistant/components/arwn/sensor.py @@ -30,26 +30,30 @@ def discover_sensors(topic, payload): unit = TEMP_FAHRENHEIT else: unit = TEMP_CELSIUS - return ArwnSensor(name, "temp", unit) + return ArwnSensor(topic, name, "temp", unit) if domain == "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 len(parts) >= 3 and parts[2] == "today": return ArwnSensor( - "Rain Since Midnight", "since_midnight", "in", "mdi:water" + topic, "Rain Since Midnight", "since_midnight", "in", "mdi:water" ) return ( - ArwnSensor("Total Rainfall", "total", unit, "mdi:water"), - ArwnSensor("Rainfall Rate", "rate", unit, "mdi:water"), + ArwnSensor(topic + "/total", "Total Rainfall", "total", unit, "mdi:water"), + ArwnSensor(topic + "/rate", "Rainfall Rate", "rate", unit, "mdi:water"), ) if domain == "barometer": - return ArwnSensor("Barometer", "pressure", unit, "mdi:thermometer-lines") + return ArwnSensor(topic, "Barometer", "pressure", unit, "mdi:thermometer-lines") if domain == "wind": return ( - ArwnSensor("Wind Speed", "speed", unit, "mdi:speedometer"), - ArwnSensor("Wind Gust", "gust", unit, "mdi:speedometer"), - ArwnSensor("Wind Direction", "direction", DEGREE, "mdi:compass"), + ArwnSensor( + topic + "/speed", "Wind Speed", "speed", unit, "mdi:speedometer" + ), + 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) store[sensor.name] = sensor _LOGGER.debug( - "Registering new sensor %(name)s => %(event)s", + "Registering sensor %(name)s => %(event)s", {"name": sensor.name, "event": event}, ) async_add_entities((sensor,), True) else: + _LOGGER.debug( + "Recording sensor %(name)s => %(event)s", + {"name": sensor.name, "event": event}, + ) store[sensor.name].set_event(event) 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): """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.""" self.hass = None self.entity_id = _slug(name) self._name = name + # This mqtt topic for the sensor which is its uid + self._uid = topic self._state_key = state_key self.event = {} self._unit_of_measurement = units @@ -135,6 +145,14 @@ class ArwnSensor(Entity): """Get the name of the sensor.""" 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 def state_attributes(self): """Return all the state attributes."""