feat: add func slugify_did

This commit is contained in:
topsworld 2025-01-16 19:23:06 +08:00
parent 8a1b38ef56
commit e07b90dd7a
4 changed files with 12 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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