deconz: Use partition instead of split where possible (#81804)
* deconz: Use partition instead of split where possible With a smattering of code deduplication Split out of #81493 * Update homeassistant/components/deconz/util.py Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com> Co-authored-by: Robert Svensson <Kane610@users.noreply.github.com>pull/81807/head
parent
b364ef98a0
commit
6021cedb09
|
@ -33,6 +33,7 @@ import homeassistant.helpers.entity_registry as er
|
|||
from .const import ATTR_DARK, ATTR_ON, DOMAIN as DECONZ_DOMAIN
|
||||
from .deconz_device import DeconzDevice
|
||||
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
||||
from .util import serial_from_unique_id
|
||||
|
||||
_SensorDeviceT = TypeVar("_SensorDeviceT", bound=PydeconzSensorBase)
|
||||
|
||||
|
@ -187,7 +188,9 @@ def async_update_unique_id(
|
|||
return
|
||||
|
||||
if description.old_unique_id_suffix:
|
||||
unique_id = f'{unique_id.split("-", 1)[0]}-{description.old_unique_id_suffix}'
|
||||
unique_id = (
|
||||
f"{serial_from_unique_id(unique_id)}-{description.old_unique_id_suffix}"
|
||||
)
|
||||
|
||||
if entity_id := ent_reg.async_get_entity_id(DOMAIN, DECONZ_DOMAIN, unique_id):
|
||||
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
|
||||
|
|
|
@ -17,6 +17,7 @@ from homeassistant.helpers.entity import DeviceInfo, Entity
|
|||
|
||||
from .const import DOMAIN as DECONZ_DOMAIN
|
||||
from .gateway import DeconzGateway
|
||||
from .util import serial_from_unique_id
|
||||
|
||||
_DeviceT = TypeVar(
|
||||
"_DeviceT",
|
||||
|
@ -55,9 +56,7 @@ class DeconzBase(Generic[_DeviceT]):
|
|||
def serial(self) -> str | None:
|
||||
"""Return a serial number for this device."""
|
||||
assert isinstance(self._device, PydeconzDevice)
|
||||
if not self._device.unique_id or self._device.unique_id.count(":") != 7:
|
||||
return None
|
||||
return self._device.unique_id.split("-", 1)[0]
|
||||
return serial_from_unique_id(self._device.unique_id)
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo | None:
|
||||
|
|
|
@ -26,6 +26,7 @@ import homeassistant.helpers.entity_registry as er
|
|||
from .const import DOMAIN as DECONZ_DOMAIN
|
||||
from .deconz_device import DeconzDevice
|
||||
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
||||
from .util import serial_from_unique_id
|
||||
|
||||
T = TypeVar("T", Presence, PydeconzSensorBase)
|
||||
|
||||
|
@ -88,7 +89,7 @@ def async_update_unique_id(
|
|||
if ent_reg.async_get_entity_id(DOMAIN, DECONZ_DOMAIN, new_unique_id):
|
||||
return
|
||||
|
||||
unique_id = f'{unique_id.split("-", 1)[0]}-{description.key}'
|
||||
unique_id = f"{serial_from_unique_id(unique_id)}-{description.key}"
|
||||
if entity_id := ent_reg.async_get_entity_id(DOMAIN, DECONZ_DOMAIN, unique_id):
|
||||
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
|
||||
|
||||
|
|
|
@ -50,6 +50,7 @@ import homeassistant.util.dt as dt_util
|
|||
from .const import ATTR_DARK, ATTR_ON, DOMAIN as DECONZ_DOMAIN
|
||||
from .deconz_device import DeconzDevice
|
||||
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
||||
from .util import serial_from_unique_id
|
||||
|
||||
PROVIDES_EXTRA_ATTRIBUTES = (
|
||||
"battery",
|
||||
|
@ -248,7 +249,9 @@ def async_update_unique_id(
|
|||
return
|
||||
|
||||
if description.old_unique_id_suffix:
|
||||
unique_id = f'{unique_id.split("-", 1)[0]}-{description.old_unique_id_suffix}'
|
||||
unique_id = (
|
||||
f"{serial_from_unique_id(unique_id)}-{description.old_unique_id_suffix}"
|
||||
)
|
||||
|
||||
if entity_id := ent_reg.async_get_entity_id(DOMAIN, DECONZ_DOMAIN, unique_id):
|
||||
ent_reg.async_update_entity(entity_id, new_unique_id=new_unique_id)
|
||||
|
@ -290,7 +293,7 @@ async def async_setup_entry(
|
|||
sensor.type.startswith("CLIP")
|
||||
or (no_sensor_data and description.key != "battery")
|
||||
or (
|
||||
(unique_id := sensor.unique_id.rsplit("-", 1)[0])
|
||||
(unique_id := sensor.unique_id.rpartition("-")[0])
|
||||
in known_device_entities[description.key]
|
||||
)
|
||||
):
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
"""Utilities for deCONZ integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def serial_from_unique_id(unique_id: str | None) -> str | None:
|
||||
"""Get a device serial number from a unique ID, if possible."""
|
||||
if not unique_id or unique_id.count(":") != 7:
|
||||
return None
|
||||
return unique_id.partition("-")[0]
|
|
@ -7,6 +7,7 @@ from homeassistant.components.deconz.deconz_event import (
|
|||
CONF_DECONZ_ALARM_EVENT,
|
||||
CONF_DECONZ_EVENT,
|
||||
)
|
||||
from homeassistant.components.deconz.util import serial_from_unique_id
|
||||
from homeassistant.const import (
|
||||
CONF_CODE,
|
||||
CONF_DEVICE_ID,
|
||||
|
@ -60,7 +61,7 @@ async def test_humanifying_deconz_alarm_event(hass, aioclient_mock):
|
|||
device_registry = dr.async_get(hass)
|
||||
|
||||
keypad_event_id = slugify(data["sensors"]["1"]["name"])
|
||||
keypad_serial = data["sensors"]["1"]["uniqueid"].split("-", 1)[0]
|
||||
keypad_serial = serial_from_unique_id(data["sensors"]["1"]["uniqueid"])
|
||||
keypad_entry = device_registry.async_get_device(
|
||||
identifiers={(DECONZ_DOMAIN, keypad_serial)}
|
||||
)
|
||||
|
@ -131,25 +132,25 @@ async def test_humanifying_deconz_event(hass, aioclient_mock):
|
|||
device_registry = dr.async_get(hass)
|
||||
|
||||
switch_event_id = slugify(data["sensors"]["1"]["name"])
|
||||
switch_serial = data["sensors"]["1"]["uniqueid"].split("-", 1)[0]
|
||||
switch_serial = serial_from_unique_id(data["sensors"]["1"]["uniqueid"])
|
||||
switch_entry = device_registry.async_get_device(
|
||||
identifiers={(DECONZ_DOMAIN, switch_serial)}
|
||||
)
|
||||
|
||||
hue_remote_event_id = slugify(data["sensors"]["2"]["name"])
|
||||
hue_remote_serial = data["sensors"]["2"]["uniqueid"].split("-", 1)[0]
|
||||
hue_remote_serial = serial_from_unique_id(data["sensors"]["2"]["uniqueid"])
|
||||
hue_remote_entry = device_registry.async_get_device(
|
||||
identifiers={(DECONZ_DOMAIN, hue_remote_serial)}
|
||||
)
|
||||
|
||||
xiaomi_cube_event_id = slugify(data["sensors"]["3"]["name"])
|
||||
xiaomi_cube_serial = data["sensors"]["3"]["uniqueid"].split("-", 1)[0]
|
||||
xiaomi_cube_serial = serial_from_unique_id(data["sensors"]["3"]["uniqueid"])
|
||||
xiaomi_cube_entry = device_registry.async_get_device(
|
||||
identifiers={(DECONZ_DOMAIN, xiaomi_cube_serial)}
|
||||
)
|
||||
|
||||
faulty_event_id = slugify(data["sensors"]["4"]["name"])
|
||||
faulty_serial = data["sensors"]["4"]["uniqueid"].split("-", 1)[0]
|
||||
faulty_serial = serial_from_unique_id(data["sensors"]["4"]["uniqueid"])
|
||||
faulty_entry = device_registry.async_get_device(
|
||||
identifiers={(DECONZ_DOMAIN, faulty_serial)}
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue