Cleanup unique_id on onewire integration (#43783)

* Update construction of unique_id

* Move shared logic into OneWireBaseEntity
pull/43858/head
epenet 2020-12-02 15:11:20 +01:00 committed by GitHub
parent 06626af337
commit 55edc9f858
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 51 deletions

View File

@ -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,
)
)

View File

@ -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

View File

@ -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,
)
)

View File

@ -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,
)
)