From e07b90dd7a0b5d2296231faa5882493741bbb5ef Mon Sep 17 00:00:00 2001 From: topsworld Date: Thu, 16 Jan 2025 19:23:06 +0800 Subject: [PATCH] feat: add func slugify_did --- custom_components/xiaomi_home/__init__.py | 3 ++- custom_components/xiaomi_home/miot/common.py | 5 +++++ custom_components/xiaomi_home/miot/miot_client.py | 4 ++-- custom_components/xiaomi_home/miot/miot_device.py | 9 +++------ 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/custom_components/xiaomi_home/__init__.py b/custom_components/xiaomi_home/__init__.py index 43f78ae..6e6c24a 100644 --- a/custom_components/xiaomi_home/__init__.py +++ b/custom_components/xiaomi_home/__init__.py @@ -54,6 +54,7 @@ from homeassistant.core import HomeAssistant from homeassistant.components import persistent_notification from homeassistant.helpers import device_registry, entity_registry +from .miot.common import slugify_did from .miot.miot_storage import ( DeviceManufacturer, MIoTStorage, MIoTCert) from .miot.miot_spec import ( @@ -237,7 +238,7 @@ async def async_setup_entry( device_entry = dr.async_get_device( identifiers={( DOMAIN, - MIoTDevice.gen_did_tag( + slugify_did( cloud_server=config_entry.data['cloud_server'], did=did))}, connections=None) diff --git a/custom_components/xiaomi_home/miot/common.py b/custom_components/xiaomi_home/miot/common.py index 0d17edf..21c5439 100644 --- a/custom_components/xiaomi_home/miot/common.py +++ b/custom_components/xiaomi_home/miot/common.py @@ -98,6 +98,11 @@ def slugify_name(name: str, separator: str = '_') -> str: return slugify(name, separator=separator) +def slugify_did(cloud_server: str, did: str) -> str: + """Slugify a device id.""" + return slugify(f'{cloud_server}_{did}', separator='_') + + class MIoTMatcher(MQTTMatcher): """MIoT Pub/Sub topic matcher.""" diff --git a/custom_components/xiaomi_home/miot/miot_client.py b/custom_components/xiaomi_home/miot/miot_client.py index ba34d4c..0033e95 100644 --- a/custom_components/xiaomi_home/miot/miot_client.py +++ b/custom_components/xiaomi_home/miot/miot_client.py @@ -59,7 +59,7 @@ from homeassistant.core import HomeAssistant from homeassistant.components import zeroconf # pylint: disable=relative-beyond-top-level -from .common import MIoTMatcher, slugify_name +from .common import MIoTMatcher, slugify_did from .const import ( DEFAULT_CTRL_MODE, DEFAULT_INTEGRATION_LANGUAGE, DEFAULT_NICK_NAME, DOMAIN, MIHOME_CERT_EXPIRE_MARGIN, NETWORK_REFRESH_INTERVAL, @@ -874,7 +874,7 @@ class MIoTClient: async def remove_device2_async(self, did_tag: str) -> None: for did in self._device_list_cache: - d_tag = slugify_name(name=f'{self.cloud_server}_{did}') + d_tag = slugify_did(cloud_server=self._cloud_server, did=did) if did_tag == d_tag: await self.remove_device_async(did) break diff --git a/custom_components/xiaomi_home/miot/miot_device.py b/custom_components/xiaomi_home/miot/miot_device.py index 9dfee3d..5e18944 100644 --- a/custom_components/xiaomi_home/miot/miot_device.py +++ b/custom_components/xiaomi_home/miot/miot_device.py @@ -85,7 +85,7 @@ from .specs.specv2entity import ( SPEC_PROP_TRANS_MAP, SPEC_SERVICE_TRANS_MAP ) -from .common import slugify_name +from .common import slugify_name, slugify_did from .const import DOMAIN from .miot_client import MIoTClient from .miot_error import MIoTClientError, MIoTDeviceError @@ -335,11 +335,8 @@ class MIoTDevice: @property def did_tag(self) -> str: - return slugify_name(f'{self.miot_client.cloud_server}_{self._did}') - - @staticmethod - def gen_did_tag(cloud_server: str, did: str) -> str: - return slugify_name(f'{cloud_server}_{did}') + return slugify_did( + cloud_server=self.miot_client.cloud_server, did=self._did) def gen_device_entity_id(self, ha_domain: str) -> str: return (