zha: Simplify unique ID (#12495)

Also fixes entity IDs generated for devices on the same endpoint.
This commit is contained in:
Russell Cloran 2018-02-18 15:02:34 -08:00 committed by Paulus Schoutsen
parent 1143499301
commit 17b57099ae

View File

@ -183,7 +183,7 @@ class ApplicationListener:
component = None component = None
profile_clusters = ([], []) profile_clusters = ([], [])
device_key = "{}-{}".format(str(device.ieee), endpoint_id) device_key = "{}-{}".format(device.ieee, endpoint_id)
node_config = self._config[DOMAIN][CONF_DEVICE_CONFIG].get( node_config = self._config[DOMAIN][CONF_DEVICE_CONFIG].get(
device_key, {}) device_key, {})
@ -213,7 +213,7 @@ class ApplicationListener:
'in_clusters': {c.cluster_id: c for c in in_clusters}, 'in_clusters': {c.cluster_id: c for c in in_clusters},
'out_clusters': {c.cluster_id: c for c in out_clusters}, 'out_clusters': {c.cluster_id: c for c in out_clusters},
'new_join': join, 'new_join': join,
'unique_id': "{}-{}".format(device.ieee, endpoint_id), 'unique_id': device_key,
} }
discovery_info.update(discovered_info) discovery_info.update(discovered_info)
self._hass.data[DISCOVERY_KEY][device_key] = discovery_info self._hass.data[DISCOVERY_KEY][device_key] = discovery_info
@ -234,17 +234,17 @@ class ApplicationListener:
continue continue
component = zha_const.SINGLE_CLUSTER_DEVICE_CLASS[cluster_type] component = zha_const.SINGLE_CLUSTER_DEVICE_CLASS[cluster_type]
cluster_key = "{}-{}".format(device_key, cluster_id)
discovery_info = { discovery_info = {
'application_listener': self, 'application_listener': self,
'endpoint': endpoint, 'endpoint': endpoint,
'in_clusters': {cluster.cluster_id: cluster}, 'in_clusters': {cluster.cluster_id: cluster},
'out_clusters': {}, 'out_clusters': {},
'new_join': join, 'new_join': join,
'unique_id': "{}-{}-{}".format( 'unique_id': cluster_key,
device.ieee, endpoint_id, cluster_id), 'entity_suffix': '_{}'.format(cluster_id),
} }
discovery_info.update(discovered_info) discovery_info.update(discovered_info)
cluster_key = "{}-{}".format(device_key, cluster_id)
self._hass.data[DISCOVERY_KEY][cluster_key] = discovery_info self._hass.data[DISCOVERY_KEY][cluster_key] = discovery_info
yield from discovery.async_load_platform( yield from discovery.async_load_platform(
@ -272,23 +272,26 @@ class Entity(entity.Entity):
ieee = endpoint.device.ieee ieee = endpoint.device.ieee
ieeetail = ''.join(['%02x' % (o, ) for o in ieee[-4:]]) ieeetail = ''.join(['%02x' % (o, ) for o in ieee[-4:]])
if manufacturer and model is not None: if manufacturer and model is not None:
self.entity_id = "{}.{}_{}_{}_{}".format( self.entity_id = "{}.{}_{}_{}_{}{}".format(
self._domain, self._domain,
slugify(manufacturer), slugify(manufacturer),
slugify(model), slugify(model),
ieeetail, ieeetail,
endpoint.endpoint_id, endpoint.endpoint_id,
kwargs.get('entity_suffix', ''),
) )
self._device_state_attributes['friendly_name'] = "{} {}".format( self._device_state_attributes['friendly_name'] = "{} {}".format(
manufacturer, manufacturer,
model, model,
) )
else: else:
self.entity_id = "{}.zha_{}_{}".format( self.entity_id = "{}.zha_{}_{}{}".format(
self._domain, self._domain,
ieeetail, ieeetail,
endpoint.endpoint_id, endpoint.endpoint_id,
kwargs.get('entity_suffix', ''),
) )
for cluster in in_clusters.values(): for cluster in in_clusters.values():
cluster.add_listener(self) cluster.add_listener(self)
for cluster in out_clusters.values(): for cluster in out_clusters.values():
@ -370,8 +373,7 @@ def get_discovery_info(hass, discovery_info):
discovery_key = discovery_info.get('discovery_key', None) discovery_key = discovery_info.get('discovery_key', None)
all_discovery_info = hass.data.get(DISCOVERY_KEY, {}) all_discovery_info = hass.data.get(DISCOVERY_KEY, {})
discovery_info = all_discovery_info.get(discovery_key, None) return all_discovery_info.get(discovery_key, None)
return discovery_info
@asyncio.coroutine @asyncio.coroutine