Added unique ID's based on coordinates, sensor type and client name.

This commit is contained in:
corneyl 2018-03-15 23:18:28 +01:00
parent 50ec73df86
commit 3345e67a15

View file

@ -161,7 +161,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
dev = []
for sensor_type in config[CONF_MONITORED_CONDITIONS]:
dev.append(BrSensor(sensor_type, config.get(CONF_NAME, 'br')))
dev.append(BrSensor(sensor_type, config.get(CONF_NAME, 'br'),
coordinates))
async_add_devices(dev)
data = BrData(hass, coordinates, timeframe, dev)
@ -172,7 +173,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class BrSensor(Entity):
"""Representation of an Buienradar sensor."""
def __init__(self, sensor_type, client_name):
def __init__(self, sensor_type, client_name, coordinates):
"""Initialize the sensor."""
from buienradar.buienradar import (PRECIPITATION_FORECAST, CONDITION)
@ -185,6 +186,7 @@ class BrSensor(Entity):
self._attribution = None
self._measured = None
self._stationname = None
self._unique_id = self.uuid(coordinates)
# All continuous sensors should be forced to be updated
self._force_update = self.type != SYMBOL and \
@ -193,6 +195,21 @@ class BrSensor(Entity):
if self.type.startswith(PRECIPITATION_FORECAST):
self._timeframe = None
def uuid(self, coordinates):
"""Generate a UUID using coordinates, client name and sensor type"""
from hashlib import sha1
from uuid import UUID
# The combination of the location, name an sensor type is unique
unique_str = "%2.6f%2.6f%s%s" % (coordinates[CONF_LATITUDE],
coordinates[CONF_LONGITUDE],
self.type,
self.client_name)
# Create UUID based on the hash of the unique string
return str(UUID(sha1(unique_str.encode()).hexdigest()[:32]))
def load_data(self, data):
"""Load the sensor with relevant data."""
# Find sensor
@ -304,6 +321,11 @@ class BrSensor(Entity):
"""Return the attribution."""
return self._attribution
@property
def unique_id(self):
"""Return the unique id."""
return self._unique_id
@property
def name(self):
"""Return the name of the sensor."""