mirror of
https://github.com/home-assistant/core.git
synced 2025-08-07 06:35:10 +02:00
Added unique ID's based on coordinates, sensor type and client name.
This commit is contained in:
@@ -161,7 +161,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
|||||||
|
|
||||||
dev = []
|
dev = []
|
||||||
for sensor_type in config[CONF_MONITORED_CONDITIONS]:
|
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)
|
async_add_devices(dev)
|
||||||
|
|
||||||
data = BrData(hass, coordinates, timeframe, 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):
|
class BrSensor(Entity):
|
||||||
"""Representation of an Buienradar sensor."""
|
"""Representation of an Buienradar sensor."""
|
||||||
|
|
||||||
def __init__(self, sensor_type, client_name):
|
def __init__(self, sensor_type, client_name, coordinates):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
from buienradar.buienradar import (PRECIPITATION_FORECAST, CONDITION)
|
from buienradar.buienradar import (PRECIPITATION_FORECAST, CONDITION)
|
||||||
|
|
||||||
@@ -185,6 +186,7 @@ class BrSensor(Entity):
|
|||||||
self._attribution = None
|
self._attribution = None
|
||||||
self._measured = None
|
self._measured = None
|
||||||
self._stationname = None
|
self._stationname = None
|
||||||
|
self._unique_id = self.uuid(coordinates)
|
||||||
|
|
||||||
# All continuous sensors should be forced to be updated
|
# All continuous sensors should be forced to be updated
|
||||||
self._force_update = self.type != SYMBOL and \
|
self._force_update = self.type != SYMBOL and \
|
||||||
@@ -193,6 +195,21 @@ class BrSensor(Entity):
|
|||||||
if self.type.startswith(PRECIPITATION_FORECAST):
|
if self.type.startswith(PRECIPITATION_FORECAST):
|
||||||
self._timeframe = None
|
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):
|
def load_data(self, data):
|
||||||
"""Load the sensor with relevant data."""
|
"""Load the sensor with relevant data."""
|
||||||
# Find sensor
|
# Find sensor
|
||||||
@@ -304,6 +321,11 @@ class BrSensor(Entity):
|
|||||||
"""Return the attribution."""
|
"""Return the attribution."""
|
||||||
return self._attribution
|
return self._attribution
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return the unique id."""
|
||||||
|
return self._unique_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
|
Reference in New Issue
Block a user