diff --git a/custom_components/plejd/light.py b/custom_components/plejd/light.py index 64b5678..6a9be51 100644 --- a/custom_components/plejd/light.py +++ b/custom_components/plejd/light.py @@ -16,9 +16,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): dev = devices[d] if dev.type == "light": coordinator = Coordinator(hass, dev) - async def updateCallback(data): - coordinator.async_set_updated_data(data) - dev.updateCallback = updateCallback + dev.updateCallback = coordinator.async_set_updated_data light = PlejdLight(coordinator, dev) entities.append(light) async_add_entities(entities, False) @@ -38,10 +36,6 @@ class PlejdLight(LightEntity, CoordinatorEntity): def _data(self): return self.coordinator.data or {} - @property - def available(self): - return self.device.available - @property def device_info(self): return { diff --git a/custom_components/plejd/pyplejd/api.py b/custom_components/plejd/pyplejd/api.py index 8d2e2da..f97e936 100644 --- a/custom_components/plejd/pyplejd/api.py +++ b/custom_components/plejd/pyplejd/api.py @@ -43,16 +43,16 @@ async def _get_site_details(session, siteId): # fp.write(json.dumps(data)) return data -site_data = None +site_data = {} async def get_site_data(username, password, siteId): global site_data - if site_data is not None: - return site_data + if site_data.get(siteId) is not None: + return site_data.get(siteId) 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 details = await _get_site_details(session, siteId) - site_data = details + site_data[siteId] = details return details async def get_sites(username, password): @@ -74,13 +74,11 @@ async def get_devices(**credentials): for device in site_data["devices"]: BLE_address = device["deviceId"] - def find_deviceId(d): - return next((s for s in d if s["deviceId"] == BLE_address), None) - address = site_data["deviceAddress"][BLE_address] dimmable = None - settings = find_deviceId(site_data["outputSettings"]) + settings = next((s for s in site_data["outputSettings"] + if s["deviceParseId"] == device["objectId"])) if settings is not None: outputs = site_data["outputAddress"][BLE_address] address = outputs[str(settings["output"])] @@ -88,9 +86,10 @@ async def get_devices(**credentials): if settings is not None and settings["dimCurve"] == "nonDimmable": dimmable = False - plejdDevice = find_deviceId(site_data["plejdDevices"]) + plejdDevice = next((s for s in site_data["plejdDevices"] + if s["deviceId"] == BLE_address)) room = next((r for r in site_data["rooms"] if r["roomId"] == device["roomId"]), {}) - + retval[address] = { "address": address, "BLE_address": BLE_address, diff --git a/custom_components/plejd/pyplejd/mesh.py b/custom_components/plejd/pyplejd/mesh.py index a206525..264b32a 100644 --- a/custom_components/plejd/pyplejd/mesh.py +++ b/custom_components/plejd/pyplejd/mesh.py @@ -71,8 +71,9 @@ class PlejdMesh(): self.client = client _LOGGER.debug("Connected to Plejd mesh") await asyncio.sleep(2) - if not await self._authenticate(): - await self.disconnect() + if not await self._authenticate() or not await self.ping(): + await self.client.disconnect() + self._connected = False continue break except (BleakError, asyncio.TimeoutError) as e: @@ -186,6 +187,12 @@ def decode_state(data): dim = int.from_bytes(data[6:8], "little") elif cmd == b"\x00\x97": state = bool(data[5]) + elif cmd == b"\x00\x16": + _LOGGER.debug("A button was pressed") + return None + else: + _LOGGER.debug("Unknown command %s", cmd) + return None return { "address": address, diff --git a/custom_components/plejd/pyplejd/plejd_device.py b/custom_components/plejd/pyplejd/plejd_device.py index aa2a867..55b54b8 100644 --- a/custom_components/plejd/pyplejd/plejd_device.py +++ b/custom_components/plejd/pyplejd/plejd_device.py @@ -1,6 +1,9 @@ from builtins import property from collections import namedtuple +import logging + +_LOGGER = logging.getLogger(__name__) Device = namedtuple("Device", ["model", "type", "dimmable"]) @@ -101,7 +104,7 @@ class PlejdDevice: self._dim = dim if update: if self.updateCallback: - await self.updateCallback({"state": self._state, "dim": self._dim}) + self.updateCallback({"state": self._state, "dim": self._dim}) async def turn_on(self, dim=0): await self.manager.mesh.set_state(self.address, True, dim) diff --git a/custom_components/plejd/switch.py b/custom_components/plejd/switch.py index 632b5f2..cf074d4 100644 --- a/custom_components/plejd/switch.py +++ b/custom_components/plejd/switch.py @@ -16,9 +16,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities): dev = devices[d] if dev.type == "switch": coordinator = Coordinator(hass, dev) - async def updateCallback(data): - coordinator.async_set_updated_data(data) - dev.updateCallback = updateCallback + dev.updateCallback = coordinator.async_set_updated_data switch = PlejdSwitch(coordinator, dev) entities.append(switch) async_add_entities(entities, False) @@ -38,10 +36,6 @@ class PlejdSwitch(SwitchEntity, CoordinatorEntity): def _data(self): return self.coordinator.data or {} - @property - def available(self): - return self._data.get("state", None) is not None - @property def device_info(self): return {