From 55edc9f8580aea07618523e3aa72c327be175627 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 2 Dec 2020 15:11:20 +0100 Subject: [PATCH] Cleanup unique_id on onewire integration (#43783) * Update construction of unique_id * Move shared logic into OneWireBaseEntity --- .../components/onewire/binary_sensor.py | 25 +++++++------ .../components/onewire/onewire_entities.py | 21 +++++++---- homeassistant/components/onewire/sensor.py | 35 +++++++++---------- homeassistant/components/onewire/switch.py | 25 +++++++------ 4 files changed, 55 insertions(+), 51 deletions(-) diff --git a/homeassistant/components/onewire/binary_sensor.py b/homeassistant/components/onewire/binary_sensor.py index 031dd984702..86b584c998c 100644 --- a/homeassistant/components/onewire/binary_sensor.py +++ b/homeassistant/components/onewire/binary_sensor.py @@ -94,29 +94,28 @@ def get_entities(onewirehub: OneWireHub): for device in onewirehub.devices: family = device["family"] device_type = device["type"] - sensor_id = os.path.split(os.path.split(device["path"])[0])[1] + device_id = os.path.split(os.path.split(device["path"])[0])[1] if family not in DEVICE_BINARY_SENSORS: continue device_info = { - "identifiers": {(DOMAIN, sensor_id)}, + "identifiers": {(DOMAIN, device_id)}, "manufacturer": "Maxim Integrated", "model": device_type, - "name": sensor_id, + "name": device_id, } - for device_sensor in DEVICE_BINARY_SENSORS[family]: - device_file = os.path.join( - os.path.split(device["path"])[0], device_sensor["path"] + for entity_specs in DEVICE_BINARY_SENSORS[family]: + entity_path = os.path.join( + os.path.split(device["path"])[0], entity_specs["path"] ) entities.append( OneWireProxyBinarySensor( - sensor_id, - device_file, - device_sensor["type"], - device_sensor["name"], - device_info, - device_sensor.get("default_disabled", False), - onewirehub.owproxy, + device_id=device_id, + device_name=device_id, + device_info=device_info, + entity_path=entity_path, + entity_specs=entity_specs, + owproxy=onewirehub.owproxy, ) ) diff --git a/homeassistant/components/onewire/onewire_entities.py b/homeassistant/components/onewire/onewire_entities.py index d16a2e9b493..9238bb5d32c 100644 --- a/homeassistant/components/onewire/onewire_entities.py +++ b/homeassistant/components/onewire/onewire_entities.py @@ -28,6 +28,7 @@ class OneWireBaseEntity(Entity): entity_name: str = None, device_info=None, default_disabled: bool = False, + unique_id: str = None, ): """Initialize the entity.""" self._name = f"{name} {entity_name or entity_type.capitalize()}" @@ -39,6 +40,7 @@ class OneWireBaseEntity(Entity): self._state = None self._value_raw = None self._default_disabled = default_disabled + self._unique_id = unique_id or device_file @property def name(self) -> Optional[str]: @@ -63,7 +65,7 @@ class OneWireBaseEntity(Entity): @property def unique_id(self) -> Optional[str]: """Return a unique ID.""" - return self._device_file + return self._unique_id @property def device_info(self) -> Optional[Dict[str, Any]]: @@ -81,17 +83,22 @@ class OneWireProxyEntity(OneWireBaseEntity): def __init__( self, - name: str, - device_file: str, - entity_type: str, - entity_name: str, + device_id: str, + device_name: str, device_info: Dict[str, Any], - default_disabled: bool, + entity_path: str, + entity_specs: Dict[str, Any], owproxy: protocol._Proxy, ): """Initialize the sensor.""" super().__init__( - name, device_file, entity_type, entity_name, device_info, default_disabled + name=device_name, + device_file=entity_path, + entity_type=entity_specs["type"], + entity_name=entity_specs["name"], + device_info=device_info, + default_disabled=entity_specs.get("default_disabled", False), + unique_id=f"/{device_id}/{entity_specs['path']}", ) self._owproxy = owproxy diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index f908c1ada2d..98090dc949f 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -244,7 +244,7 @@ def get_entities(onewirehub: OneWireHub, config): for device in onewirehub.devices: family = device["family"] device_type = device["type"] - sensor_id = os.path.split(os.path.split(device["path"])[0])[1] + device_id = os.path.split(os.path.split(device["path"])[0])[1] dev_type = "std" if "EF" in family: dev_type = "HobbyBoard" @@ -254,38 +254,37 @@ def get_entities(onewirehub: OneWireHub, config): _LOGGER.warning( "Ignoring unknown family (%s) of sensor found for device: %s", family, - sensor_id, + device_id, ) continue device_info = { - "identifiers": {(DOMAIN, sensor_id)}, + "identifiers": {(DOMAIN, device_id)}, "manufacturer": "Maxim Integrated", "model": device_type, - "name": sensor_id, + "name": device_id, } - for device_sensor in hb_info_from_type(dev_type)[family]: - if device_sensor["type"] == SENSOR_TYPE_MOISTURE: - s_id = device_sensor["path"].split(".")[1] + for entity_specs in hb_info_from_type(dev_type)[family]: + if entity_specs["type"] == SENSOR_TYPE_MOISTURE: + s_id = entity_specs["path"].split(".")[1] is_leaf = int( onewirehub.owproxy.read( f"{device['path']}moisture/is_leaf.{s_id}" ).decode() ) if is_leaf: - device_sensor["type"] = SENSOR_TYPE_WETNESS - device_sensor["name"] = f"Wetness {s_id}" - device_file = os.path.join( - os.path.split(device["path"])[0], device_sensor["path"] + entity_specs["type"] = SENSOR_TYPE_WETNESS + entity_specs["name"] = f"Wetness {s_id}" + entity_path = os.path.join( + os.path.split(device["path"])[0], entity_specs["path"] ) entities.append( OneWireProxySensor( - device_names.get(sensor_id, sensor_id), - device_file, - device_sensor["type"], - device_sensor["name"], - device_info, - device_sensor.get("default_disabled", False), - onewirehub.owproxy, + device_id=device_id, + device_name=device_names.get(device_id, device_id), + device_info=device_info, + entity_path=entity_path, + entity_specs=entity_specs, + owproxy=onewirehub.owproxy, ) ) diff --git a/homeassistant/components/onewire/switch.py b/homeassistant/components/onewire/switch.py index f1b588690ae..da1ed01a980 100644 --- a/homeassistant/components/onewire/switch.py +++ b/homeassistant/components/onewire/switch.py @@ -157,30 +157,29 @@ def get_entities(onewirehub: OneWireHub): for device in onewirehub.devices: family = device["family"] device_type = device["type"] - sensor_id = os.path.split(os.path.split(device["path"])[0])[1] + device_id = os.path.split(os.path.split(device["path"])[0])[1] if family not in DEVICE_SWITCHES: continue device_info = { - "identifiers": {(DOMAIN, sensor_id)}, + "identifiers": {(DOMAIN, device_id)}, "manufacturer": "Maxim Integrated", "model": device_type, - "name": sensor_id, + "name": device_id, } - for device_switch in DEVICE_SWITCHES[family]: - device_file = os.path.join( - os.path.split(device["path"])[0], device_switch["path"] + for entity_specs in DEVICE_SWITCHES[family]: + entity_path = os.path.join( + os.path.split(device["path"])[0], entity_specs["path"] ) entities.append( OneWireProxySwitch( - sensor_id, - device_file, - device_switch["type"], - device_switch["name"], - device_info, - device_switch.get("default_disabled", False), - onewirehub.owproxy, + device_id=device_id, + device_name=device_id, + device_info=device_info, + entity_path=entity_path, + entity_specs=entity_specs, + owproxy=onewirehub.owproxy, ) )