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>
This commit is contained in:
parent
b364ef98a0
commit
6021cedb09
6 changed files with 28 additions and 12 deletions
|
@ -33,6 +33,7 @@ import homeassistant.helpers.entity_registry as er
|
||||||
from .const import ATTR_DARK, ATTR_ON, DOMAIN as DECONZ_DOMAIN
|
from .const import ATTR_DARK, ATTR_ON, DOMAIN as DECONZ_DOMAIN
|
||||||
from .deconz_device import DeconzDevice
|
from .deconz_device import DeconzDevice
|
||||||
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
||||||
|
from .util import serial_from_unique_id
|
||||||
|
|
||||||
_SensorDeviceT = TypeVar("_SensorDeviceT", bound=PydeconzSensorBase)
|
_SensorDeviceT = TypeVar("_SensorDeviceT", bound=PydeconzSensorBase)
|
||||||
|
|
||||||
|
@ -187,7 +188,9 @@ def async_update_unique_id(
|
||||||
return
|
return
|
||||||
|
|
||||||
if description.old_unique_id_suffix:
|
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):
|
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)
|
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 .const import DOMAIN as DECONZ_DOMAIN
|
||||||
from .gateway import DeconzGateway
|
from .gateway import DeconzGateway
|
||||||
|
from .util import serial_from_unique_id
|
||||||
|
|
||||||
_DeviceT = TypeVar(
|
_DeviceT = TypeVar(
|
||||||
"_DeviceT",
|
"_DeviceT",
|
||||||
|
@ -55,9 +56,7 @@ class DeconzBase(Generic[_DeviceT]):
|
||||||
def serial(self) -> str | None:
|
def serial(self) -> str | None:
|
||||||
"""Return a serial number for this device."""
|
"""Return a serial number for this device."""
|
||||||
assert isinstance(self._device, PydeconzDevice)
|
assert isinstance(self._device, PydeconzDevice)
|
||||||
if not self._device.unique_id or self._device.unique_id.count(":") != 7:
|
return serial_from_unique_id(self._device.unique_id)
|
||||||
return None
|
|
||||||
return self._device.unique_id.split("-", 1)[0]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self) -> DeviceInfo | None:
|
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 .const import DOMAIN as DECONZ_DOMAIN
|
||||||
from .deconz_device import DeconzDevice
|
from .deconz_device import DeconzDevice
|
||||||
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
||||||
|
from .util import serial_from_unique_id
|
||||||
|
|
||||||
T = TypeVar("T", Presence, PydeconzSensorBase)
|
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):
|
if ent_reg.async_get_entity_id(DOMAIN, DECONZ_DOMAIN, new_unique_id):
|
||||||
return
|
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):
|
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)
|
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 .const import ATTR_DARK, ATTR_ON, DOMAIN as DECONZ_DOMAIN
|
||||||
from .deconz_device import DeconzDevice
|
from .deconz_device import DeconzDevice
|
||||||
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
from .gateway import DeconzGateway, get_gateway_from_config_entry
|
||||||
|
from .util import serial_from_unique_id
|
||||||
|
|
||||||
PROVIDES_EXTRA_ATTRIBUTES = (
|
PROVIDES_EXTRA_ATTRIBUTES = (
|
||||||
"battery",
|
"battery",
|
||||||
|
@ -248,7 +249,9 @@ def async_update_unique_id(
|
||||||
return
|
return
|
||||||
|
|
||||||
if description.old_unique_id_suffix:
|
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):
|
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)
|
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")
|
sensor.type.startswith("CLIP")
|
||||||
or (no_sensor_data and description.key != "battery")
|
or (no_sensor_data and description.key != "battery")
|
||||||
or (
|
or (
|
||||||
(unique_id := sensor.unique_id.rsplit("-", 1)[0])
|
(unique_id := sensor.unique_id.rpartition("-")[0])
|
||||||
in known_device_entities[description.key]
|
in known_device_entities[description.key]
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
|
|
9
homeassistant/components/deconz/util.py
Normal file
9
homeassistant/components/deconz/util.py
Normal file
|
@ -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_ALARM_EVENT,
|
||||||
CONF_DECONZ_EVENT,
|
CONF_DECONZ_EVENT,
|
||||||
)
|
)
|
||||||
|
from homeassistant.components.deconz.util import serial_from_unique_id
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_CODE,
|
CONF_CODE,
|
||||||
CONF_DEVICE_ID,
|
CONF_DEVICE_ID,
|
||||||
|
@ -60,7 +61,7 @@ async def test_humanifying_deconz_alarm_event(hass, aioclient_mock):
|
||||||
device_registry = dr.async_get(hass)
|
device_registry = dr.async_get(hass)
|
||||||
|
|
||||||
keypad_event_id = slugify(data["sensors"]["1"]["name"])
|
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(
|
keypad_entry = device_registry.async_get_device(
|
||||||
identifiers={(DECONZ_DOMAIN, keypad_serial)}
|
identifiers={(DECONZ_DOMAIN, keypad_serial)}
|
||||||
)
|
)
|
||||||
|
@ -131,25 +132,25 @@ async def test_humanifying_deconz_event(hass, aioclient_mock):
|
||||||
device_registry = dr.async_get(hass)
|
device_registry = dr.async_get(hass)
|
||||||
|
|
||||||
switch_event_id = slugify(data["sensors"]["1"]["name"])
|
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(
|
switch_entry = device_registry.async_get_device(
|
||||||
identifiers={(DECONZ_DOMAIN, switch_serial)}
|
identifiers={(DECONZ_DOMAIN, switch_serial)}
|
||||||
)
|
)
|
||||||
|
|
||||||
hue_remote_event_id = slugify(data["sensors"]["2"]["name"])
|
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(
|
hue_remote_entry = device_registry.async_get_device(
|
||||||
identifiers={(DECONZ_DOMAIN, hue_remote_serial)}
|
identifiers={(DECONZ_DOMAIN, hue_remote_serial)}
|
||||||
)
|
)
|
||||||
|
|
||||||
xiaomi_cube_event_id = slugify(data["sensors"]["3"]["name"])
|
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(
|
xiaomi_cube_entry = device_registry.async_get_device(
|
||||||
identifiers={(DECONZ_DOMAIN, xiaomi_cube_serial)}
|
identifiers={(DECONZ_DOMAIN, xiaomi_cube_serial)}
|
||||||
)
|
)
|
||||||
|
|
||||||
faulty_event_id = slugify(data["sensors"]["4"]["name"])
|
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(
|
faulty_entry = device_registry.async_get_device(
|
||||||
identifiers={(DECONZ_DOMAIN, faulty_serial)}
|
identifiers={(DECONZ_DOMAIN, faulty_serial)}
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue