Skip to content

Commit

Permalink
fix uniq
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Mamontov committed Aug 16, 2021
1 parent 8937981 commit 0bfae93
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions custom_components/miwifi/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
from homeassistant.components.binary_sensor import ENTITY_ID_FORMAT, BinarySensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import async_generate_entity_id

from .core.util import _generate_entity_id
from .core.const import DATA_UPDATED, DOMAIN, BINARY_SENSORS
from .core.luci_data import LuciData

Expand All @@ -35,7 +35,7 @@ def __init__(self, hass: HomeAssistant, luci: LuciData, code: str, data: dict) -
self._data = data
self._state = False

self.entity_id = async_generate_entity_id(
self.entity_id = _generate_entity_id(
ENTITY_ID_FORMAT,
"{}_{}".format(luci.api.device_data["name"], code),
hass = hass
Expand Down
10 changes: 10 additions & 0 deletions custom_components/miwifi/core/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from homeassistant.util import slugify

def _generate_entity_id(
entity_id_format: str,
name: str
) -> str:
name = name.lower()
preferred_string = entity_id_format.format(slugify(name))

return preferred_string
12 changes: 1 addition & 11 deletions custom_components/miwifi/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
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
from .core.util import _generate_entity_id

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,15 +96,6 @@ def _get_new_device(hass: HomeAssistant, device: dict, legacy_device: dict) -> d
)
}

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: 2 additions & 2 deletions custom_components/miwifi/light.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from homeassistant.components.light import ENTITY_ID_FORMAT, LightEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import async_generate_entity_id

from .core.util import _generate_entity_id
from .core.const import DATA_UPDATED, DOMAIN, LIGHTS
from .core.luci_data import LuciData

Expand All @@ -33,7 +33,7 @@ def __init__(self, hass: HomeAssistant, luci: LuciData, code: str, data: dict) -
self._state = False
self._is_block = False

self.entity_id = async_generate_entity_id(
self.entity_id = _generate_entity_id(
ENTITY_ID_FORMAT,
"{}_{}".format(luci.api.device_data["name"], code),
hass = hass
Expand Down
5 changes: 3 additions & 2 deletions custom_components/miwifi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from homeassistant.components.sensor import ENTITY_ID_FORMAT
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import async_generate_entity_id, Entity
from homeassistant.helpers.entity import Entity

from .core.util import _generate_entity_id
from .core.const import DATA_UPDATED, DOMAIN, SENSORS
from .core.luci_data import LuciData

Expand All @@ -35,7 +36,7 @@ def __init__(self, hass: HomeAssistant, luci: LuciData, code: str, data: dict) -
self._data = data
self._state = None

self.entity_id = async_generate_entity_id(
self.entity_id = _generate_entity_id(
ENTITY_ID_FORMAT,
"{}_{}".format(luci.api.device_data["name"], code),
hass = hass
Expand Down
4 changes: 2 additions & 2 deletions custom_components/miwifi/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from homeassistant.components.switch import ENTITY_ID_FORMAT, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import async_generate_entity_id

from .core.util import _generate_entity_id
from .core.const import DATA_UPDATED, DOMAIN, SWITCHS
from .core.luci_data import LuciData

Expand All @@ -33,7 +33,7 @@ def __init__(self, hass: HomeAssistant, luci: LuciData, code: str, data: dict) -
self._state = False
self._is_block = False

self.entity_id = async_generate_entity_id(
self.entity_id = _generate_entity_id(
ENTITY_ID_FORMAT,
"{}_{}".format(luci.api.device_data["name"], code),
hass = hass
Expand Down

0 comments on commit 0bfae93

Please sign in to comment.