From 8937981929927305d6c9bc6870bec01afa71f882 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Fri, 13 Aug 2021 17:03:21 +0300 Subject: [PATCH] Fix dublicates --- custom_components/miwifi/__init__.py | 8 ++++++-- custom_components/miwifi/device_tracker.py | 15 ++++++++++++--- custom_components/miwifi/services.yaml | 4 ++++ 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/custom_components/miwifi/__init__.py b/custom_components/miwifi/__init__.py index 9f6d695..077e8d1 100644 --- a/custom_components/miwifi/__init__.py +++ b/custom_components/miwifi/__init__.py @@ -82,14 +82,17 @@ async def async_close(event): hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_close) - await _init_services(hass) + await _init_services(hass, store) if not await client.async_setup(): return False return True -async def _init_services(hass: HomeAssistant): +async def _init_services(hass: HomeAssistant, store: Store): + async def clear_store(call: ServiceCall): + await store.async_save({}) + async def remove_devices(call: ServiceCall): data = dict(call.data) @@ -141,3 +144,4 @@ async def remove_devices(call: ServiceCall): hass.data[DOMAIN][entry_id].remove_device(mac) hass.services.async_register(DOMAIN, 'remove_devices', remove_devices) + hass.services.async_register(DOMAIN, 'clear_store', clear_store) diff --git a/custom_components/miwifi/device_tracker.py b/custom_components/miwifi/device_tracker.py index 6e8615b..86ed6cb 100644 --- a/custom_components/miwifi/device_tracker.py +++ b/custom_components/miwifi/device_tracker.py @@ -18,6 +18,7 @@ from homeassistant.helpers.entity import async_generate_entity_id from homeassistant.helpers.typing import UNDEFINED from homeassistant.components.zone import ENTITY_ID_HOME +from homeassistant.util import slugify from .core.const import DEVICES_UPDATED, DOMAIN, DEVICE_TRACKER_ENTITY_ID_FORMAT, LEGACY_YAML_DEVICES from .core.luci_data import LuciData @@ -90,13 +91,21 @@ def _get_new_device(hass: HomeAssistant, device: dict, legacy_device: dict) -> d "icon": legacy_device["icon"] if "icon" in legacy_device else None, "signal": device["signal"], "online": device["ip"][0]["online"] if "ip" in device else "0", - "unique_id": async_generate_entity_id( + "unique_id": _generate_entity_id( DEVICE_TRACKER_ENTITY_ID_FORMAT, - legacy_device["dev_id"] if "dev_id" in legacy_device else device["name"], - hass = hass + legacy_device["dev_id"] if "dev_id" in legacy_device else device["name"] ) } +def _generate_entity_id( + entity_id_format: str, + name: Optional[str] +) -> str: + name = (name or DEVICE_DEFAULT_NAME).lower() + preferred_string = entity_id_format.format(slugify(name)) + + return preferred_string + async def _get_legacy_devices(hass: HomeAssistant) -> dict: legacy_devices = {} diff --git a/custom_components/miwifi/services.yaml b/custom_components/miwifi/services.yaml index 2b3c6d6..c634e99 100644 --- a/custom_components/miwifi/services.yaml +++ b/custom_components/miwifi/services.yaml @@ -5,3 +5,7 @@ remove_devices: entity: integration: miwifi domain: device_tracker + +clear_store: + name: Clear devices store + description: Clear devices store. \ No newline at end of file