mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-16 14:40:43 +08:00
feat: add func slugify_did
This commit is contained in:
parent
8a1b38ef56
commit
e07b90dd7a
@ -54,6 +54,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.components import persistent_notification
|
from homeassistant.components import persistent_notification
|
||||||
from homeassistant.helpers import device_registry, entity_registry
|
from homeassistant.helpers import device_registry, entity_registry
|
||||||
|
|
||||||
|
from .miot.common import slugify_did
|
||||||
from .miot.miot_storage import (
|
from .miot.miot_storage import (
|
||||||
DeviceManufacturer, MIoTStorage, MIoTCert)
|
DeviceManufacturer, MIoTStorage, MIoTCert)
|
||||||
from .miot.miot_spec import (
|
from .miot.miot_spec import (
|
||||||
@ -237,7 +238,7 @@ async def async_setup_entry(
|
|||||||
device_entry = dr.async_get_device(
|
device_entry = dr.async_get_device(
|
||||||
identifiers={(
|
identifiers={(
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
MIoTDevice.gen_did_tag(
|
slugify_did(
|
||||||
cloud_server=config_entry.data['cloud_server'],
|
cloud_server=config_entry.data['cloud_server'],
|
||||||
did=did))},
|
did=did))},
|
||||||
connections=None)
|
connections=None)
|
||||||
|
|||||||
@ -98,6 +98,11 @@ def slugify_name(name: str, separator: str = '_') -> str:
|
|||||||
return slugify(name, separator=separator)
|
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):
|
class MIoTMatcher(MQTTMatcher):
|
||||||
"""MIoT Pub/Sub topic matcher."""
|
"""MIoT Pub/Sub topic matcher."""
|
||||||
|
|
||||||
|
|||||||
@ -59,7 +59,7 @@ from homeassistant.core import HomeAssistant
|
|||||||
from homeassistant.components import zeroconf
|
from homeassistant.components import zeroconf
|
||||||
|
|
||||||
# pylint: disable=relative-beyond-top-level
|
# pylint: disable=relative-beyond-top-level
|
||||||
from .common import MIoTMatcher, slugify_name
|
from .common import MIoTMatcher, slugify_did
|
||||||
from .const import (
|
from .const import (
|
||||||
DEFAULT_CTRL_MODE, DEFAULT_INTEGRATION_LANGUAGE, DEFAULT_NICK_NAME, DOMAIN,
|
DEFAULT_CTRL_MODE, DEFAULT_INTEGRATION_LANGUAGE, DEFAULT_NICK_NAME, DOMAIN,
|
||||||
MIHOME_CERT_EXPIRE_MARGIN, NETWORK_REFRESH_INTERVAL,
|
MIHOME_CERT_EXPIRE_MARGIN, NETWORK_REFRESH_INTERVAL,
|
||||||
@ -874,7 +874,7 @@ class MIoTClient:
|
|||||||
|
|
||||||
async def remove_device2_async(self, did_tag: str) -> None:
|
async def remove_device2_async(self, did_tag: str) -> None:
|
||||||
for did in self._device_list_cache:
|
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:
|
if did_tag == d_tag:
|
||||||
await self.remove_device_async(did)
|
await self.remove_device_async(did)
|
||||||
break
|
break
|
||||||
|
|||||||
@ -85,7 +85,7 @@ from .specs.specv2entity import (
|
|||||||
SPEC_PROP_TRANS_MAP,
|
SPEC_PROP_TRANS_MAP,
|
||||||
SPEC_SERVICE_TRANS_MAP
|
SPEC_SERVICE_TRANS_MAP
|
||||||
)
|
)
|
||||||
from .common import slugify_name
|
from .common import slugify_name, slugify_did
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
from .miot_client import MIoTClient
|
from .miot_client import MIoTClient
|
||||||
from .miot_error import MIoTClientError, MIoTDeviceError
|
from .miot_error import MIoTClientError, MIoTDeviceError
|
||||||
@ -335,11 +335,8 @@ class MIoTDevice:
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def did_tag(self) -> str:
|
def did_tag(self) -> str:
|
||||||
return slugify_name(f'{self.miot_client.cloud_server}_{self._did}')
|
return slugify_did(
|
||||||
|
cloud_server=self.miot_client.cloud_server, did=self._did)
|
||||||
@staticmethod
|
|
||||||
def gen_did_tag(cloud_server: str, did: str) -> str:
|
|
||||||
return slugify_name(f'{cloud_server}_{did}')
|
|
||||||
|
|
||||||
def gen_device_entity_id(self, ha_domain: str) -> str:
|
def gen_device_entity_id(self, ha_domain: str) -> str:
|
||||||
return (
|
return (
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user