Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reintroduce unique_id for Netatmo sensor #18774

Merged
merged 3 commits into from
Dec 1, 2018
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions homeassistant/components/sensor/netatmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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():
Expand All @@ -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))
Expand All @@ -112,9 +117,11 @@ 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.\
self._module_type = self.netatmo_data. \
station_data.moduleByName(module=module_name)['type']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you use this variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is needed in the update function to distinguish the module type to calculate the correct battery level.

module_id = self.netatmo_data. \
station_data.moduleByName(module=module_name)['_id']
self.module_id = module_id[1]
self._unique_id = '{}-{}'.format(module_id, self.type)

@property
def name(self):
Expand All @@ -141,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()
Expand Down Expand Up @@ -169,7 +181,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:
heinemml marked this conversation as resolved.
Show resolved Hide resolved
if data['battery_vp'] >= 5590:
self._state = "Full"
elif data['battery_vp'] >= 5180:
Expand All @@ -180,7 +192,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:
heinemml marked this conversation as resolved.
Show resolved Hide resolved
if data['battery_vp'] >= 5500:
self._state = "Full"
elif data['battery_vp'] >= 5000:
Expand All @@ -191,7 +203,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:
heinemml marked this conversation as resolved.
Show resolved Hide resolved
if data['battery_vp'] >= 5640:
self._state = "Full"
elif data['battery_vp'] >= 5280:
Expand All @@ -202,7 +214,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:
heinemml marked this conversation as resolved.
Show resolved Hide resolved
if data['battery_vp'] >= 5500:
self._state = "Full"
elif data['battery_vp'] >= 5000:
Expand Down