From 666689e54392d9adb822d373086105f4777e0392 Mon Sep 17 00:00:00 2001 From: Michael Nosthoff Date: Wed, 28 Nov 2018 21:23:37 +0100 Subject: [PATCH 1/3] netatmo: make module type identification more consistent For the interpretation of voltage values the different types of netatmo modules need to be distinguished. This is currently done by selecting the second character of the modules '_id'. The _id-field actually contains a mac address. This is an undocumented way of identifying the module_type. The netatmo API also delivers a field called 'type' which provides a more consistent way to differentiate the fields. This commit introduces a differentiation which uses this provided type. This should improve readability. Also the field module_id is renamed to module_type which should better resemble what it actually represents. --- homeassistant/components/sensor/netatmo.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/sensor/netatmo.py b/homeassistant/components/sensor/netatmo.py index 2abaa801d68900..05864daa1efeda 100644 --- a/homeassistant/components/sensor/netatmo.py +++ b/homeassistant/components/sensor/netatmo.py @@ -63,6 +63,11 @@ vol.Optional(CONF_MODULES): MODULE_SCHEMA, }) +MODULE_TYPE_OUTDOOR = 'NAModule1' +MODULE_TYPE_WIND = 'NAModule2' +MODULE_TYPE_RAIN = 'NAModule3' +MODULE_TYPE_INDOOR = 'NAModule4' + def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the available Netatmo weather sensors.""" @@ -74,7 +79,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): try: if CONF_MODULES in config: # Iterate each module - for module_name, monitored_conditions in\ + for module_name, monitored_conditions in \ config[CONF_MODULES].items(): # Test if module exists if module_name not in data.get_module_names(): @@ -85,7 +90,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): dev.append(NetAtmoSensor(data, module_name, variable)) else: for module_name in data.get_module_names(): - for variable in\ + for variable in \ data.station_data.monitoredConditions(module_name): if variable in SENSOR_TYPES.keys(): dev.append(NetAtmoSensor(data, module_name, variable)) @@ -112,9 +117,8 @@ def __init__(self, netatmo_data, module_name, sensor_type): self._device_class = SENSOR_TYPES[self.type][3] self._icon = SENSOR_TYPES[self.type][2] self._unit_of_measurement = SENSOR_TYPES[self.type][1] - module_id = self.netatmo_data.\ - station_data.moduleByName(module=module_name)['_id'] - self.module_id = module_id[1] + self._module_type = self.netatmo_data. \ + station_data.moduleByName(module=module_name)['type'] @property def name(self): @@ -169,7 +173,7 @@ def update(self): self._state = round(data['Pressure'], 1) elif self.type == 'battery_lvl': self._state = data['battery_vp'] - elif self.type == 'battery_vp' and self.module_id == '6': + elif self.type == 'battery_vp' and self._module_type == MODULE_TYPE_WIND: if data['battery_vp'] >= 5590: self._state = "Full" elif data['battery_vp'] >= 5180: @@ -180,7 +184,7 @@ def update(self): self._state = "Low" elif data['battery_vp'] < 4360: self._state = "Very Low" - elif self.type == 'battery_vp' and self.module_id == '5': + elif self.type == 'battery_vp' and self._module_type == MODULE_TYPE_RAIN: if data['battery_vp'] >= 5500: self._state = "Full" elif data['battery_vp'] >= 5000: @@ -191,7 +195,7 @@ def update(self): self._state = "Low" elif data['battery_vp'] < 4000: self._state = "Very Low" - elif self.type == 'battery_vp' and self.module_id == '3': + elif self.type == 'battery_vp' and self._module_type == MODULE_TYPE_INDOOR: if data['battery_vp'] >= 5640: self._state = "Full" elif data['battery_vp'] >= 5280: @@ -202,7 +206,7 @@ def update(self): self._state = "Low" elif data['battery_vp'] < 4560: self._state = "Very Low" - elif self.type == 'battery_vp' and self.module_id == '2': + elif self.type == 'battery_vp' and self._module_type == MODULE_TYPE_OUTDOOR: if data['battery_vp'] >= 5500: self._state = "Full" elif data['battery_vp'] >= 5000: From 06fc0b42120cf8ef1150bbe3301937dcfcc1be6a Mon Sep 17 00:00:00 2001 From: Michael Nosthoff Date: Wed, 28 Nov 2018 21:34:26 +0100 Subject: [PATCH 2/3] netatmo: reintroduce unique_id using actual module mac address Each netatmo module features a unique MAC-Address. The base station uses an actual assigned MAC Address it also uses on the Wifi it connects to. All other modules have unique MAC Addresses which are only assigned and used by Netatmo on the internal Wireless-Network. All theses Addresses are exposed via the API. So we could use the combination MAC-Address-Sensor_type as unique_id. In a previous commit this had already been tried but there was a misunderstanding in what the 'module_id' represented. It was actually only a module_type representation so it clashed when two modules of the same type where used. --- homeassistant/components/sensor/netatmo.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/homeassistant/components/sensor/netatmo.py b/homeassistant/components/sensor/netatmo.py index 05864daa1efeda..f800c99af48c23 100644 --- a/homeassistant/components/sensor/netatmo.py +++ b/homeassistant/components/sensor/netatmo.py @@ -119,6 +119,9 @@ def __init__(self, netatmo_data, module_name, sensor_type): self._unit_of_measurement = SENSOR_TYPES[self.type][1] self._module_type = self.netatmo_data. \ station_data.moduleByName(module=module_name)['type'] + module_id = self.netatmo_data. \ + station_data.moduleByName(module=module_name)['_id'] + self._unique_id = '{}-{}'.format(module_id, self.type) @property def name(self): @@ -145,6 +148,11 @@ def unit_of_measurement(self): """Return the unit of measurement of this entity, if any.""" return self._unit_of_measurement + @property + def unique_id(self): + """Return the unique ID for this sensor.""" + return self._unique_id + def update(self): """Get the latest data from NetAtmo API and updates the states.""" self.netatmo_data.update() From c99ce996f7675847de022064222b5aa6245c2a9c Mon Sep 17 00:00:00 2001 From: Michael Nosthoff Date: Thu, 29 Nov 2018 15:02:05 +0100 Subject: [PATCH 3/3] Netatmo: fixed line length --- homeassistant/components/sensor/netatmo.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sensor/netatmo.py b/homeassistant/components/sensor/netatmo.py index f800c99af48c23..7590bccb5431ab 100644 --- a/homeassistant/components/sensor/netatmo.py +++ b/homeassistant/components/sensor/netatmo.py @@ -181,7 +181,8 @@ def update(self): self._state = round(data['Pressure'], 1) elif self.type == 'battery_lvl': self._state = data['battery_vp'] - elif self.type == 'battery_vp' and self._module_type == MODULE_TYPE_WIND: + elif (self.type == 'battery_vp' and + self._module_type == MODULE_TYPE_WIND): if data['battery_vp'] >= 5590: self._state = "Full" elif data['battery_vp'] >= 5180: @@ -192,7 +193,8 @@ def update(self): self._state = "Low" elif data['battery_vp'] < 4360: self._state = "Very Low" - elif self.type == 'battery_vp' and self._module_type == MODULE_TYPE_RAIN: + elif (self.type == 'battery_vp' and + self._module_type == MODULE_TYPE_RAIN): if data['battery_vp'] >= 5500: self._state = "Full" elif data['battery_vp'] >= 5000: @@ -203,7 +205,8 @@ def update(self): self._state = "Low" elif data['battery_vp'] < 4000: self._state = "Very Low" - elif self.type == 'battery_vp' and self._module_type == MODULE_TYPE_INDOOR: + elif (self.type == 'battery_vp' and + self._module_type == MODULE_TYPE_INDOOR): if data['battery_vp'] >= 5640: self._state = "Full" elif data['battery_vp'] >= 5280: @@ -214,7 +217,8 @@ def update(self): self._state = "Low" elif data['battery_vp'] < 4560: self._state = "Very Low" - elif self.type == 'battery_vp' and self._module_type == MODULE_TYPE_OUTDOOR: + elif (self.type == 'battery_vp' and + self._module_type == MODULE_TYPE_OUTDOOR): if data['battery_vp'] >= 5500: self._state = "Full" elif data['battery_vp'] >= 5000: