Skip to content
Snippets Groups Projects
Unverified Commit 061b1abd authored by J. Nick Koston's avatar J. Nick Koston Committed by GitHub
Browse files

Improve handling of invalid serial numbers in HomeKit Controller (#58723)

Fixes #58719
parent e9713361
No related branches found
No related tags found
No related merge requests found
......@@ -19,7 +19,7 @@ from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity import DeviceInfo, Entity
from .config_flow import normalize_hkid
from .connection import HKDevice
from .connection import HKDevice, valid_serial_number
from .const import (
CONTROLLER,
DOMAIN,
......@@ -141,7 +141,7 @@ class HomeKitEntity(Entity):
"""Return the ID of this device."""
info = self.accessory_info
serial = info.value(CharacteristicsTypes.SERIAL_NUMBER)
if serial:
if valid_serial_number(serial):
return f"homekit-{serial}-{self._iid}"
# Some accessories do not have a serial number
return f"homekit-{self._accessory.unique_id}-{self._aid}-{self._iid}"
......@@ -161,7 +161,7 @@ class HomeKitEntity(Entity):
"""Return the device info."""
info = self.accessory_info
accessory_serial = info.value(CharacteristicsTypes.SERIAL_NUMBER)
if accessory_serial:
if valid_serial_number(accessory_serial):
# Some accessories do not have a serial number
identifier = (DOMAIN, IDENTIFIER_SERIAL_NUMBER, accessory_serial)
else:
......
......@@ -36,6 +36,16 @@ MAX_POLL_FAILURES_TO_DECLARE_UNAVAILABLE = 3
_LOGGER = logging.getLogger(__name__)
def valid_serial_number(serial):
"""Return if the serial number appears to be valid."""
if not serial:
return False
try:
return float("".join(serial.rsplit(".", 1))) > 1
except ValueError:
return True
def get_accessory_information(accessory):
"""Obtain the accessory information service of a HomeKit device."""
result = {}
......@@ -211,7 +221,7 @@ class HKDevice:
serial_number = info.value(CharacteristicsTypes.SERIAL_NUMBER)
if serial_number:
if valid_serial_number(serial_number):
identifiers = {(DOMAIN, IDENTIFIER_SERIAL_NUMBER, serial_number)}
else:
# Some accessories do not have a serial number
......
......@@ -19,7 +19,7 @@ async def test_ryse_smart_bridge_setup(hass):
# Check that the cover.master_bath_south is correctly found and set up
cover_id = "cover.master_bath_south"
cover = entity_registry.async_get(cover_id)
assert cover.unique_id == "homekit-1.0.0-48"
assert cover.unique_id == "homekit-00:00:00:00:00:00-2-48"
cover_helper = Helper(
hass,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment