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

Conversation

heinemml
Copy link
Contributor

@heinemml heinemml commented Nov 29, 2018

Description:

Short:

This commit adds back the unique_id property for Netatmo sensors with a really unique value. Additionally it makes the differentiation of Module Types more intuitive.

Long:

In commit e51427b there was already an attempt to add a unique_id in the new Entity Registry style to the Netatmo sensor. This assumed, that the value of self.module_id was a value unique for each Netatmo Module. (actually it changed from using module_id which was unique to self.module_id which was not unique for the unique_id)

This assumption was wrong as PR #4724 introduced a self.module_id which differed from module_id
as it was defined like this:

module_id = self.netatmo_data.\
            station_data.moduleByName(module=module_name)['_id']
self.module_id = module_id[1]

So self.module_id was actually only the second char of the module_id (which is a MAC Address). This was used in the Battery Code to destinguish between the different types of modules. So it was actually more representing a module_type.

This led to a unique_id like 2-Temperature which was pretty obvious to be clashing with other modules.

What this PR now implements:

  • Use the type field provided by the Netatmo API to distinguish between module types instead of taking an undocumented Byte from the Module address. This is used for the calculation of the Battery Status.
  • Use the full MAC-Address + SensorType to generate the unique_id. So the unique_id now looks like XX:XX:XX:XX:XX:XX-Temperature

Example entry for configuration.yaml (if applicable):

netatmo:
  api_key: YOUR_CLIENT_ID
  secret_key: YOUR_CLIENT_SECRET
  username: YOUR_USERNAME
  password: YOUR_PASSWORD

sensor:
  - platform: netatmo
    station: STATION_NAME
    modules:
      module_name1:
        - temperature
      module_name2:
        - temperature
        - battery_vp

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.

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.
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 Outdated Show resolved Hide resolved
homeassistant/components/sensor/netatmo.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/netatmo.py Outdated Show resolved Hide resolved
homeassistant/components/sensor/netatmo.py Outdated Show resolved Hide resolved
@fabaff fabaff changed the title Sensor/Netatmo: reintroduce unique_id Reintroduce unique_id for Netatmo sensor Nov 29, 2018
@@ -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.

@pvizeli pvizeli self-assigned this Nov 30, 2018
@pvizeli pvizeli merged commit bd09e96 into home-assistant:dev Dec 1, 2018
@ghost ghost removed the in progress label Dec 1, 2018
@balloob balloob mentioned this pull request Dec 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants