Skip to content

Commit

Permalink
Fix dublicates
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed Aug 13, 2021
1 parent 6572b28 commit 8937981
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
8 changes: 6 additions & 2 deletions custom_components/miwifi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
15 changes: 12 additions & 3 deletions custom_components/miwifi/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}

Expand Down
4 changes: 4 additions & 0 deletions custom_components/miwifi/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ remove_devices:
entity:
integration: miwifi
domain: device_tracker

clear_store:
name: Clear devices store
description: Clear devices store.

0 comments on commit 8937981

Please sign in to comment.