Skip to content

Commit

Permalink
Cleanup unique_id on onewire integration (home-assistant#43783)
Browse files Browse the repository at this point in the history
* Update construction of unique_id

* Move shared logic into OneWireBaseEntity
  • Loading branch information
epenet authored Dec 2, 2020
1 parent 06626af commit 55edc9f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 51 deletions.
25 changes: 12 additions & 13 deletions homeassistant/components/onewire/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)

Expand Down
21 changes: 14 additions & 7 deletions homeassistant/components/onewire/onewire_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def __init__(
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()}"
Expand All @@ -39,6 +40,7 @@ def __init__(
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]:
Expand All @@ -63,7 +65,7 @@ def device_state_attributes(self) -> Optional[Dict[str, Any]]:
@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]]:
Expand All @@ -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

Expand Down
35 changes: 17 additions & 18 deletions homeassistant/components/onewire/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
)
)

Expand Down
25 changes: 12 additions & 13 deletions homeassistant/components/onewire/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)

Expand Down

0 comments on commit 55edc9f

Please sign in to comment.