Cleanup unique_id on onewire integration (#43783)
* Update construction of unique_id * Move shared logic into OneWireBaseEntity
This commit is contained in:
parent
06626af337
commit
55edc9f858
4 changed files with 55 additions and 51 deletions
|
@ -94,29 +94,28 @@ def get_entities(onewirehub: OneWireHub):
|
||||||
for device in onewirehub.devices:
|
for device in onewirehub.devices:
|
||||||
family = device["family"]
|
family = device["family"]
|
||||||
device_type = device["type"]
|
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:
|
if family not in DEVICE_BINARY_SENSORS:
|
||||||
continue
|
continue
|
||||||
device_info = {
|
device_info = {
|
||||||
"identifiers": {(DOMAIN, sensor_id)},
|
"identifiers": {(DOMAIN, device_id)},
|
||||||
"manufacturer": "Maxim Integrated",
|
"manufacturer": "Maxim Integrated",
|
||||||
"model": device_type,
|
"model": device_type,
|
||||||
"name": sensor_id,
|
"name": device_id,
|
||||||
}
|
}
|
||||||
for device_sensor in DEVICE_BINARY_SENSORS[family]:
|
for entity_specs in DEVICE_BINARY_SENSORS[family]:
|
||||||
device_file = os.path.join(
|
entity_path = os.path.join(
|
||||||
os.path.split(device["path"])[0], device_sensor["path"]
|
os.path.split(device["path"])[0], entity_specs["path"]
|
||||||
)
|
)
|
||||||
entities.append(
|
entities.append(
|
||||||
OneWireProxyBinarySensor(
|
OneWireProxyBinarySensor(
|
||||||
sensor_id,
|
device_id=device_id,
|
||||||
device_file,
|
device_name=device_id,
|
||||||
device_sensor["type"],
|
device_info=device_info,
|
||||||
device_sensor["name"],
|
entity_path=entity_path,
|
||||||
device_info,
|
entity_specs=entity_specs,
|
||||||
device_sensor.get("default_disabled", False),
|
owproxy=onewirehub.owproxy,
|
||||||
onewirehub.owproxy,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ class OneWireBaseEntity(Entity):
|
||||||
entity_name: str = None,
|
entity_name: str = None,
|
||||||
device_info=None,
|
device_info=None,
|
||||||
default_disabled: bool = False,
|
default_disabled: bool = False,
|
||||||
|
unique_id: str = None,
|
||||||
):
|
):
|
||||||
"""Initialize the entity."""
|
"""Initialize the entity."""
|
||||||
self._name = f"{name} {entity_name or entity_type.capitalize()}"
|
self._name = f"{name} {entity_name or entity_type.capitalize()}"
|
||||||
|
@ -39,6 +40,7 @@ class OneWireBaseEntity(Entity):
|
||||||
self._state = None
|
self._state = None
|
||||||
self._value_raw = None
|
self._value_raw = None
|
||||||
self._default_disabled = default_disabled
|
self._default_disabled = default_disabled
|
||||||
|
self._unique_id = unique_id or device_file
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> Optional[str]:
|
def name(self) -> Optional[str]:
|
||||||
|
@ -63,7 +65,7 @@ class OneWireBaseEntity(Entity):
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> Optional[str]:
|
def unique_id(self) -> Optional[str]:
|
||||||
"""Return a unique ID."""
|
"""Return a unique ID."""
|
||||||
return self._device_file
|
return self._unique_id
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self) -> Optional[Dict[str, Any]]:
|
def device_info(self) -> Optional[Dict[str, Any]]:
|
||||||
|
@ -81,17 +83,22 @@ class OneWireProxyEntity(OneWireBaseEntity):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
name: str,
|
device_id: str,
|
||||||
device_file: str,
|
device_name: str,
|
||||||
entity_type: str,
|
|
||||||
entity_name: str,
|
|
||||||
device_info: Dict[str, Any],
|
device_info: Dict[str, Any],
|
||||||
default_disabled: bool,
|
entity_path: str,
|
||||||
|
entity_specs: Dict[str, Any],
|
||||||
owproxy: protocol._Proxy,
|
owproxy: protocol._Proxy,
|
||||||
):
|
):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(
|
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
|
self._owproxy = owproxy
|
||||||
|
|
||||||
|
|
|
@ -244,7 +244,7 @@ def get_entities(onewirehub: OneWireHub, config):
|
||||||
for device in onewirehub.devices:
|
for device in onewirehub.devices:
|
||||||
family = device["family"]
|
family = device["family"]
|
||||||
device_type = device["type"]
|
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"
|
dev_type = "std"
|
||||||
if "EF" in family:
|
if "EF" in family:
|
||||||
dev_type = "HobbyBoard"
|
dev_type = "HobbyBoard"
|
||||||
|
@ -254,38 +254,37 @@ def get_entities(onewirehub: OneWireHub, config):
|
||||||
_LOGGER.warning(
|
_LOGGER.warning(
|
||||||
"Ignoring unknown family (%s) of sensor found for device: %s",
|
"Ignoring unknown family (%s) of sensor found for device: %s",
|
||||||
family,
|
family,
|
||||||
sensor_id,
|
device_id,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
device_info = {
|
device_info = {
|
||||||
"identifiers": {(DOMAIN, sensor_id)},
|
"identifiers": {(DOMAIN, device_id)},
|
||||||
"manufacturer": "Maxim Integrated",
|
"manufacturer": "Maxim Integrated",
|
||||||
"model": device_type,
|
"model": device_type,
|
||||||
"name": sensor_id,
|
"name": device_id,
|
||||||
}
|
}
|
||||||
for device_sensor in hb_info_from_type(dev_type)[family]:
|
for entity_specs in hb_info_from_type(dev_type)[family]:
|
||||||
if device_sensor["type"] == SENSOR_TYPE_MOISTURE:
|
if entity_specs["type"] == SENSOR_TYPE_MOISTURE:
|
||||||
s_id = device_sensor["path"].split(".")[1]
|
s_id = entity_specs["path"].split(".")[1]
|
||||||
is_leaf = int(
|
is_leaf = int(
|
||||||
onewirehub.owproxy.read(
|
onewirehub.owproxy.read(
|
||||||
f"{device['path']}moisture/is_leaf.{s_id}"
|
f"{device['path']}moisture/is_leaf.{s_id}"
|
||||||
).decode()
|
).decode()
|
||||||
)
|
)
|
||||||
if is_leaf:
|
if is_leaf:
|
||||||
device_sensor["type"] = SENSOR_TYPE_WETNESS
|
entity_specs["type"] = SENSOR_TYPE_WETNESS
|
||||||
device_sensor["name"] = f"Wetness {s_id}"
|
entity_specs["name"] = f"Wetness {s_id}"
|
||||||
device_file = os.path.join(
|
entity_path = os.path.join(
|
||||||
os.path.split(device["path"])[0], device_sensor["path"]
|
os.path.split(device["path"])[0], entity_specs["path"]
|
||||||
)
|
)
|
||||||
entities.append(
|
entities.append(
|
||||||
OneWireProxySensor(
|
OneWireProxySensor(
|
||||||
device_names.get(sensor_id, sensor_id),
|
device_id=device_id,
|
||||||
device_file,
|
device_name=device_names.get(device_id, device_id),
|
||||||
device_sensor["type"],
|
device_info=device_info,
|
||||||
device_sensor["name"],
|
entity_path=entity_path,
|
||||||
device_info,
|
entity_specs=entity_specs,
|
||||||
device_sensor.get("default_disabled", False),
|
owproxy=onewirehub.owproxy,
|
||||||
onewirehub.owproxy,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -157,30 +157,29 @@ def get_entities(onewirehub: OneWireHub):
|
||||||
for device in onewirehub.devices:
|
for device in onewirehub.devices:
|
||||||
family = device["family"]
|
family = device["family"]
|
||||||
device_type = device["type"]
|
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:
|
if family not in DEVICE_SWITCHES:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
device_info = {
|
device_info = {
|
||||||
"identifiers": {(DOMAIN, sensor_id)},
|
"identifiers": {(DOMAIN, device_id)},
|
||||||
"manufacturer": "Maxim Integrated",
|
"manufacturer": "Maxim Integrated",
|
||||||
"model": device_type,
|
"model": device_type,
|
||||||
"name": sensor_id,
|
"name": device_id,
|
||||||
}
|
}
|
||||||
for device_switch in DEVICE_SWITCHES[family]:
|
for entity_specs in DEVICE_SWITCHES[family]:
|
||||||
device_file = os.path.join(
|
entity_path = os.path.join(
|
||||||
os.path.split(device["path"])[0], device_switch["path"]
|
os.path.split(device["path"])[0], entity_specs["path"]
|
||||||
)
|
)
|
||||||
entities.append(
|
entities.append(
|
||||||
OneWireProxySwitch(
|
OneWireProxySwitch(
|
||||||
sensor_id,
|
device_id=device_id,
|
||||||
device_file,
|
device_name=device_id,
|
||||||
device_switch["type"],
|
device_info=device_info,
|
||||||
device_switch["name"],
|
entity_path=entity_path,
|
||||||
device_info,
|
entity_specs=entity_specs,
|
||||||
device_switch.get("default_disabled", False),
|
owproxy=onewirehub.owproxy,
|
||||||
onewirehub.owproxy,
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue