Fix multiple output devices. Also allow connection to multiple meshes?

This commit is contained in:
Thomas Lovén 2022-10-19 20:13:13 +02:00
parent 87ed481f80
commit 8797b119f2
5 changed files with 24 additions and 27 deletions

View File

@ -16,9 +16,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
dev = devices[d] dev = devices[d]
if dev.type == "light": if dev.type == "light":
coordinator = Coordinator(hass, dev) coordinator = Coordinator(hass, dev)
async def updateCallback(data): dev.updateCallback = coordinator.async_set_updated_data
coordinator.async_set_updated_data(data)
dev.updateCallback = updateCallback
light = PlejdLight(coordinator, dev) light = PlejdLight(coordinator, dev)
entities.append(light) entities.append(light)
async_add_entities(entities, False) async_add_entities(entities, False)
@ -38,10 +36,6 @@ class PlejdLight(LightEntity, CoordinatorEntity):
def _data(self): def _data(self):
return self.coordinator.data or {} return self.coordinator.data or {}
@property
def available(self):
return self.device.available
@property @property
def device_info(self): def device_info(self):
return { return {

View File

@ -43,16 +43,16 @@ async def _get_site_details(session, siteId):
# fp.write(json.dumps(data)) # fp.write(json.dumps(data))
return data return data
site_data = None site_data = {}
async def get_site_data(username, password, siteId): async def get_site_data(username, password, siteId):
global site_data global site_data
if site_data is not None: if site_data.get(siteId) is not None:
return site_data return site_data.get(siteId)
async with ClientSession(base_url=API_BASE_URL, headers=headers) as session: async with ClientSession(base_url=API_BASE_URL, headers=headers) as session:
session_token = await _login(session, username, password) session_token = await _login(session, username, password)
session.headers["X-Parse-Session-Token"] = session_token session.headers["X-Parse-Session-Token"] = session_token
details = await _get_site_details(session, siteId) details = await _get_site_details(session, siteId)
site_data = details site_data[siteId] = details
return details return details
async def get_sites(username, password): async def get_sites(username, password):
@ -74,13 +74,11 @@ async def get_devices(**credentials):
for device in site_data["devices"]: for device in site_data["devices"]:
BLE_address = device["deviceId"] 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] address = site_data["deviceAddress"][BLE_address]
dimmable = None 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: if settings is not None:
outputs = site_data["outputAddress"][BLE_address] outputs = site_data["outputAddress"][BLE_address]
address = outputs[str(settings["output"])] address = outputs[str(settings["output"])]
@ -88,7 +86,8 @@ async def get_devices(**credentials):
if settings is not None and settings["dimCurve"] == "nonDimmable": if settings is not None and settings["dimCurve"] == "nonDimmable":
dimmable = False 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"]), {}) room = next((r for r in site_data["rooms"] if r["roomId"] == device["roomId"]), {})
retval[address] = { retval[address] = {

View File

@ -71,8 +71,9 @@ class PlejdMesh():
self.client = client self.client = client
_LOGGER.debug("Connected to Plejd mesh") _LOGGER.debug("Connected to Plejd mesh")
await asyncio.sleep(2) await asyncio.sleep(2)
if not await self._authenticate(): if not await self._authenticate() or not await self.ping():
await self.disconnect() await self.client.disconnect()
self._connected = False
continue continue
break break
except (BleakError, asyncio.TimeoutError) as e: except (BleakError, asyncio.TimeoutError) as e:
@ -186,6 +187,12 @@ def decode_state(data):
dim = int.from_bytes(data[6:8], "little") dim = int.from_bytes(data[6:8], "little")
elif cmd == b"\x00\x97": elif cmd == b"\x00\x97":
state = bool(data[5]) 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 { return {
"address": address, "address": address,

View File

@ -1,6 +1,9 @@
from builtins import property from builtins import property
from collections import namedtuple from collections import namedtuple
import logging
_LOGGER = logging.getLogger(__name__)
Device = namedtuple("Device", ["model", "type", "dimmable"]) Device = namedtuple("Device", ["model", "type", "dimmable"])
@ -101,7 +104,7 @@ class PlejdDevice:
self._dim = dim self._dim = dim
if update: if update:
if self.updateCallback: 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): async def turn_on(self, dim=0):
await self.manager.mesh.set_state(self.address, True, dim) await self.manager.mesh.set_state(self.address, True, dim)

View File

@ -16,9 +16,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
dev = devices[d] dev = devices[d]
if dev.type == "switch": if dev.type == "switch":
coordinator = Coordinator(hass, dev) coordinator = Coordinator(hass, dev)
async def updateCallback(data): dev.updateCallback = coordinator.async_set_updated_data
coordinator.async_set_updated_data(data)
dev.updateCallback = updateCallback
switch = PlejdSwitch(coordinator, dev) switch = PlejdSwitch(coordinator, dev)
entities.append(switch) entities.append(switch)
async_add_entities(entities, False) async_add_entities(entities, False)
@ -38,10 +36,6 @@ class PlejdSwitch(SwitchEntity, CoordinatorEntity):
def _data(self): def _data(self):
return self.coordinator.data or {} return self.coordinator.data or {}
@property
def available(self):
return self._data.get("state", None) is not None
@property @property
def device_info(self): def device_info(self):
return { return {