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

Fixes #58719
pull/58690/head
J. Nick Koston 2021-10-29 19:57:01 -05:00 committed by GitHub
parent e97133613a
commit 061b1abd1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 5 deletions

View File

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

View File

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

View File

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