From c1572d9600ae921647c900338a6f1ed73edd9f46 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 27 Apr 2024 09:26:26 +0200 Subject: [PATCH] Handle invalid device type in onewire (#116153) * Make device type optional in onewire * Add comment --- homeassistant/components/onewire/binary_sensor.py | 2 +- homeassistant/components/onewire/model.py | 2 +- homeassistant/components/onewire/onewirehub.py | 10 +++++++--- homeassistant/components/onewire/sensor.py | 4 ++-- homeassistant/components/onewire/switch.py | 2 +- tests/components/onewire/const.py | 8 +++++++- .../onewire/snapshots/test_binary_sensor.ambr | 12 ++++++++++++ tests/components/onewire/snapshots/test_sensor.ambr | 12 ++++++++++++ tests/components/onewire/snapshots/test_switch.ambr | 12 ++++++++++++ 9 files changed, 55 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/onewire/binary_sensor.py b/homeassistant/components/onewire/binary_sensor.py index d2e66609103..3c2ca3529cc 100644 --- a/homeassistant/components/onewire/binary_sensor.py +++ b/homeassistant/components/onewire/binary_sensor.py @@ -117,7 +117,7 @@ def get_entities(onewire_hub: OneWireHub) -> list[OneWireBinarySensor]: device_type = device.type device_info = device.device_info device_sub_type = "std" - if "EF" in family: + if device_type and "EF" in family: device_sub_type = "HobbyBoard" family = device_type diff --git a/homeassistant/components/onewire/model.py b/homeassistant/components/onewire/model.py index 9deaca2d121..a59953dcd25 100644 --- a/homeassistant/components/onewire/model.py +++ b/homeassistant/components/onewire/model.py @@ -16,4 +16,4 @@ class OWDeviceDescription: family: str id: str path: str - type: str + type: str | None diff --git a/homeassistant/components/onewire/onewirehub.py b/homeassistant/components/onewire/onewirehub.py index b01cc6ba3d6..2dc617ba039 100644 --- a/homeassistant/components/onewire/onewirehub.py +++ b/homeassistant/components/onewire/onewirehub.py @@ -45,7 +45,7 @@ DEVICE_MANUFACTURER = { _LOGGER = logging.getLogger(__name__) -def _is_known_device(device_family: str, device_type: str) -> bool: +def _is_known_device(device_family: str, device_type: str | None) -> bool: """Check if device family/type is known to the library.""" if device_family in ("7E", "EF"): # EDS or HobbyBoard return device_type in DEVICE_SUPPORT[device_family] @@ -144,11 +144,15 @@ class OneWireHub: return devices - def _get_device_type(self, device_path: str) -> str: + def _get_device_type(self, device_path: str) -> str | None: """Get device model.""" if TYPE_CHECKING: assert self.owproxy - device_type = self.owproxy.read(f"{device_path}type").decode() + try: + device_type = self.owproxy.read(f"{device_path}type").decode() + except protocol.ProtocolError as exc: + _LOGGER.debug("Unable to read `%stype`: %s", device_path, exc) + return None _LOGGER.debug("read `%stype`: %s", device_path, device_type) if device_type == "EDS": device_type = self.owproxy.read(f"{device_path}device_type").decode() diff --git a/homeassistant/components/onewire/sensor.py b/homeassistant/components/onewire/sensor.py index 46f18842d51..3e43df4dddd 100644 --- a/homeassistant/components/onewire/sensor.py +++ b/homeassistant/components/onewire/sensor.py @@ -377,10 +377,10 @@ def get_entities( device_info = device.device_info device_sub_type = "std" device_path = device.path - if "EF" in family: + if device_type and "EF" in family: device_sub_type = "HobbyBoard" family = device_type - elif "7E" in family: + elif device_type and "7E" in family: device_sub_type = "EDS" family = device_type elif "A6" in family: diff --git a/homeassistant/components/onewire/switch.py b/homeassistant/components/onewire/switch.py index 41276218540..94a7d41ab85 100644 --- a/homeassistant/components/onewire/switch.py +++ b/homeassistant/components/onewire/switch.py @@ -178,7 +178,7 @@ def get_entities(onewire_hub: OneWireHub) -> list[OneWireSwitch]: device_id = device.id device_info = device.device_info device_sub_type = "std" - if "EF" in family: + if device_type and "EF" in family: device_sub_type = "HobbyBoard" family = device_type elif "A6" in family: diff --git a/tests/components/onewire/const.py b/tests/components/onewire/const.py index e5f8ac575e9..a1bab9807d5 100644 --- a/tests/components/onewire/const.py +++ b/tests/components/onewire/const.py @@ -1,6 +1,6 @@ """Constants for 1-Wire integration.""" -from pyownet.protocol import Error as ProtocolError +from pyownet.protocol import ProtocolError from homeassistant.components.onewire.const import Platform @@ -58,6 +58,12 @@ MOCK_OWPROXY_DEVICES = { {ATTR_INJECT_READS: b" 248125"}, ], }, + "16.111111111111": { + # Test case for issue #115984, where the device type cannot be read + ATTR_INJECT_READS: [ + ProtocolError(), # read device type + ], + }, "1F.111111111111": { ATTR_INJECT_READS: [ b"DS2409", # read device type diff --git a/tests/components/onewire/snapshots/test_binary_sensor.ambr b/tests/components/onewire/snapshots/test_binary_sensor.ambr index 3123dfb6a5e..999794ec20d 100644 --- a/tests/components/onewire/snapshots/test_binary_sensor.ambr +++ b/tests/components/onewire/snapshots/test_binary_sensor.ambr @@ -219,6 +219,18 @@ }), ]) # --- +# name: test_binary_sensors[16.111111111111] + list([ + ]) +# --- +# name: test_binary_sensors[16.111111111111].1 + list([ + ]) +# --- +# name: test_binary_sensors[16.111111111111].2 + list([ + ]) +# --- # name: test_binary_sensors[1D.111111111111] list([ DeviceRegistryEntrySnapshot({ diff --git a/tests/components/onewire/snapshots/test_sensor.ambr b/tests/components/onewire/snapshots/test_sensor.ambr index aa8c914ece5..59ed167197d 100644 --- a/tests/components/onewire/snapshots/test_sensor.ambr +++ b/tests/components/onewire/snapshots/test_sensor.ambr @@ -278,6 +278,18 @@ }), ]) # --- +# name: test_sensors[16.111111111111] + list([ + ]) +# --- +# name: test_sensors[16.111111111111].1 + list([ + ]) +# --- +# name: test_sensors[16.111111111111].2 + list([ + ]) +# --- # name: test_sensors[1D.111111111111] list([ DeviceRegistryEntrySnapshot({ diff --git a/tests/components/onewire/snapshots/test_switch.ambr b/tests/components/onewire/snapshots/test_switch.ambr index 2ac542d203c..8fd1e2aeef6 100644 --- a/tests/components/onewire/snapshots/test_switch.ambr +++ b/tests/components/onewire/snapshots/test_switch.ambr @@ -351,6 +351,18 @@ }), ]) # --- +# name: test_switches[16.111111111111] + list([ + ]) +# --- +# name: test_switches[16.111111111111].1 + list([ + ]) +# --- +# name: test_switches[16.111111111111].2 + list([ + ]) +# --- # name: test_switches[1D.111111111111] list([ DeviceRegistryEntrySnapshot({