Add unique_id to Xiaomi Aqara (#12372)

* Add unique_id to Xiaomi Aqara

* Slugify the unique ID

* Use the domain instead of a separate string

* Add underscore

* Remove unique ID from attributes

* Re-add removed attributes

* Remove domain from the unique ID

* Use type or data key

* Also make sure that the data key is not None

* Yes, it does have that member, if that check passes. Thanks pylint.
This commit is contained in:
Lukas Barth 2018-02-22 08:36:39 +01:00 committed by Paulus Schoutsen
parent 4d7fb2c7de
commit 2a4971dec7

View File

@ -21,6 +21,7 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.util.dt import utcnow
from homeassistant.util import slugify
REQUIREMENTS = ['PyXiaomiGateway==0.8.1']
@ -203,12 +204,13 @@ def setup(hass, config):
class XiaomiDevice(Entity):
"""Representation a base Xiaomi device."""
def __init__(self, device, name, xiaomi_hub):
def __init__(self, device, device_type, xiaomi_hub):
"""Initialize the Xiaomi device."""
self._state = None
self._is_available = True
self._sid = device['sid']
self._name = '{}_{}'.format(name, self._sid)
self._name = '{}_{}'.format(device_type, self._sid)
self._type = device_type
self._write_to_hub = xiaomi_hub.write_to_hub
self._get_from_hub = xiaomi_hub.get_from_hub
self._device_state_attributes = {}
@ -217,6 +219,14 @@ class XiaomiDevice(Entity):
self.parse_data(device['data'], device['raw_data'])
self.parse_voltage(device['data'])
if hasattr(self, '_data_key') \
and self._data_key: # pylint: disable=no-member
self._unique_id = slugify("{}-{}".format(
self._data_key, # pylint: disable=no-member
self._sid))
else:
self._unique_id = slugify("{}-{}".format(self._type, self._sid))
def _add_push_data_job(self, *args):
self.hass.add_job(self.push_data, *args)
@ -230,6 +240,11 @@ class XiaomiDevice(Entity):
"""Return the name of the device."""
return self._name
@property
def unique_id(self) -> str:
"""Return an unique ID."""
return self._unique_id
@property
def available(self):
"""Return True if entity is available."""