From b241bf10b0f562f8f61751894b5fe4e44bb6dc06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Wed, 19 Oct 2022 20:35:11 +0200 Subject: [PATCH] Nicer config entry names --- custom_components/plejd/__init__.py | 8 ++++++-- custom_components/plejd/config_flow.py | 15 +++++++-------- custom_components/plejd/light.py | 2 +- custom_components/plejd/pyplejd/api.py | 4 ++-- custom_components/plejd/pyplejd/mesh.py | 10 ++++++++-- custom_components/plejd/switch.py | 2 +- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/custom_components/plejd/__init__.py b/custom_components/plejd/__init__.py index 4f61d70..54ba3f0 100644 --- a/custom_components/plejd/__init__.py +++ b/custom_components/plejd/__init__.py @@ -47,10 +47,14 @@ async def async_setup_entry(hass, config_entry): hass.data.setdefault(DOMAIN, {}).update( { "stopping": False, - "manager": plejdManager, - "devices": devices, } ) + hass.data[DOMAIN].setdefault("devices", {}).update({ + config_entry.entry_id: devices + }) + hass.data[DOMAIN].setdefault("manager", {}).update({ + config_entry.entry_id: plejdManager, + }) # Close any stale connections that may be open for dev in devices.values(): diff --git a/custom_components/plejd/config_flow.py b/custom_components/plejd/config_flow.py index b4df788..6a64955 100644 --- a/custom_components/plejd/config_flow.py +++ b/custom_components/plejd/config_flow.py @@ -28,22 +28,21 @@ class PlejdConfigFlow(ConfigFlow, domain="plejd"): async def async_step_picksite(self, info=None): if info is None: sites = await api.get_sites(self.credentials["username"], self.credentials["password"]) + self.sites = {site["site"]["siteId"]: site["site"]["title"] for site in sites} return self.async_show_form( step_id="picksite", data_schema=vol.Schema( { - vol.Required("site"): vol.In( - { - site["site"]["siteId"]: site["site"]["title"] - for site in sites - } - ) + vol.Required("site"): vol.In(self.sites) } ) ) + + siteTitle = self.sites[info["site"]] data={ "username": self.credentials["username"], "password": self.credentials["password"], - "siteId": info["site"] + "siteId": info["site"], + "siteTitle": siteTitle, } - return self.async_create_entry(title="Plejd", data=data) + return self.async_create_entry(title=siteTitle, data=data) diff --git a/custom_components/plejd/light.py b/custom_components/plejd/light.py index 6a9be51..8ecf6f5 100644 --- a/custom_components/plejd/light.py +++ b/custom_components/plejd/light.py @@ -9,7 +9,7 @@ _LOGGER = logging.getLogger(__name__) DOMAIN = "plejd" async def async_setup_entry(hass, config_entry, async_add_entities): - devices = hass.data[DOMAIN]["devices"] + devices = hass.data[DOMAIN]["devices"].get(config_entry.entry_id, []) entities = [] for d in devices: diff --git a/custom_components/plejd/pyplejd/api.py b/custom_components/plejd/pyplejd/api.py index f97e936..ef49689 100644 --- a/custom_components/plejd/pyplejd/api.py +++ b/custom_components/plejd/pyplejd/api.py @@ -44,7 +44,7 @@ async def _get_site_details(session, siteId): return data site_data = {} -async def get_site_data(username, password, siteId): +async def get_site_data(username, password, siteId, **_): global site_data if site_data.get(siteId) is not None: return site_data.get(siteId) @@ -55,7 +55,7 @@ async def get_site_data(username, password, siteId): site_data[siteId] = details return details -async def get_sites(username, password): +async def get_sites(username, password, **_): async with ClientSession(base_url=API_BASE_URL, headers=headers) as session: session_token = await _login(session, username, password) session.headers["X-Parse-Session-Token"] = session_token diff --git a/custom_components/plejd/pyplejd/mesh.py b/custom_components/plejd/pyplejd/mesh.py index 264b32a..7043a18 100644 --- a/custom_components/plejd/pyplejd/mesh.py +++ b/custom_components/plejd/pyplejd/mesh.py @@ -25,7 +25,10 @@ class PlejdMesh(): self.statecallback = None def add_mesh_node(self, device): - self.mesh_nodes.append(device) + if device not in self.mesh_nodes: + self.mesh_nodes.append(device) + else: + _LOGGER.debug("Plejd already added") def set_crypto_key(self, key): self.crypto_key = binascii.a2b_hex(key.replace("-", "")) @@ -71,7 +74,7 @@ class PlejdMesh(): self.client = client _LOGGER.debug("Connected to Plejd mesh") await asyncio.sleep(2) - if not await self._authenticate() or not await self.ping(): + if not await self._authenticate(): await self.client.disconnect() self._connected = False continue @@ -164,6 +167,9 @@ class PlejdMesh(): challenge = await self.client.read_gatt_char(PLEJD_AUTH) response = auth_response(self.crypto_key, challenge) await self.client.write_gatt_char(PLEJD_AUTH, response, response=True) + if not await self.ping(): + _LOGGER.debug("Authenticion failed") + return False _LOGGER.debug("Authenticated successfully") return True except (BleakError, asyncio.TimeoutError) as e: diff --git a/custom_components/plejd/switch.py b/custom_components/plejd/switch.py index cf074d4..0db3eac 100644 --- a/custom_components/plejd/switch.py +++ b/custom_components/plejd/switch.py @@ -9,7 +9,7 @@ _LOGGER = logging.getLogger(__name__) DOMAIN = "plejd" async def async_setup_entry(hass, config_entry, async_add_entities): - devices = hass.data[DOMAIN]["devices"] + devices = hass.data[DOMAIN]["devices"].get(config_entry.entry_id, []) entities = [] for d in devices: