Handle invalid device type in onewire (#116153)

* Make device type optional in onewire

* Add comment
This commit is contained in:
epenet 2024-04-27 09:26:26 +02:00 committed by GitHub
parent 97d151d1c6
commit c1572d9600
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 55 additions and 9 deletions

View File

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

View File

@ -16,4 +16,4 @@ class OWDeviceDescription:
family: str
id: str
path: str
type: str
type: str | None

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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