Cleanup and refactoring
This commit is contained in:
@@ -7,6 +7,8 @@ from .mesh import PlejdMesh
|
||||
from .api import get_cryptokey, get_devices
|
||||
from .plejd_device import PlejdDevice
|
||||
|
||||
from .const import PLEJD_SERVICE
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
class PlejdManager:
|
||||
@@ -58,11 +60,14 @@ class PlejdManager:
|
||||
return await self.mesh.ping()
|
||||
|
||||
async def disconnect(self):
|
||||
_LOGGER.info("DISCONNECT")
|
||||
_LOGGER.debug("DISCONNECT")
|
||||
await self.mesh.disconnect()
|
||||
|
||||
async def poll(self):
|
||||
await self.mesh.poll()
|
||||
|
||||
async def ping(self):
|
||||
return await self.mesh.ping()
|
||||
retval = await self.mesh.ping()
|
||||
if self.mesh.pollonWrite:
|
||||
await self.poll()
|
||||
return retval
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from aiohttp import ClientSession
|
||||
import json
|
||||
from collections import namedtuple
|
||||
import logging
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -11,36 +10,6 @@ API_LOGIN_URL = '/parse/login'
|
||||
API_SITE_LIST_URL = '/parse/functions/getSiteList'
|
||||
API_SITE_DETAILS_URL = '/parse/functions/getSiteById'
|
||||
|
||||
Device = namedtuple("Device", ["model", "type", "dimmable"])
|
||||
|
||||
LIGHT = "light"
|
||||
SENSOR = "sensor"
|
||||
SWITCH = "switch"
|
||||
|
||||
HARDWARE_ID = {
|
||||
"0": Device("-unknown-", LIGHT, False),
|
||||
"1": Device("DIM-01", LIGHT, True),
|
||||
"2": Device("DIM-02", LIGHT, True),
|
||||
"3": Device("CTR-01", LIGHT, False),
|
||||
"4": Device("GWY-01", SENSOR, False),
|
||||
"5": Device("LED-10", LIGHT, True),
|
||||
"6": Device("WPH-01", SWITCH, False),
|
||||
"7": Device("REL-01", SWITCH, False),
|
||||
"8": Device("-unknown-", LIGHT, False),
|
||||
"9": Device("-unknown-", LIGHT, False),
|
||||
"10": Device("-unknown-", LIGHT, False),
|
||||
"11": Device("DIM-01", LIGHT, True),
|
||||
"12": Device("-unknown-", LIGHT, False),
|
||||
"13": Device("Generic", LIGHT, False),
|
||||
"14": Device("-unknown-", LIGHT, False),
|
||||
"15": Device("-unknown-", LIGHT, False),
|
||||
"16": Device("-unknown-", LIGHT, False),
|
||||
"17": Device("REL-01", SWITCH, False),
|
||||
"18": Device("REL-02", SWITCH, False),
|
||||
"19": Device("-unknown-", LIGHT, False),
|
||||
"20": Device("SPR-01", SWITCH, False),
|
||||
}
|
||||
|
||||
|
||||
headers = {
|
||||
"X-Parse-Application-Id": API_APP_ID,
|
||||
@@ -108,31 +77,29 @@ async def get_devices(**credentials):
|
||||
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"])
|
||||
if settings is not None:
|
||||
outputs = site_data["outputAddress"][BLE_address]
|
||||
address = outputs[str(settings["output"])]
|
||||
|
||||
if settings is not None and settings["dimCurve"] == "nonDimmable":
|
||||
dimmable = False
|
||||
|
||||
plejdDevice = find_deviceId(site_data["plejdDevices"])
|
||||
deviceType = HARDWARE_ID.get(plejdDevice["hardwareId"], HARDWARE_ID["0"])
|
||||
firmware = plejdDevice["firmware"]["version"]
|
||||
|
||||
dimmable = deviceType.dimmable
|
||||
if settings is not None:
|
||||
dimmable = settings["dimCurve"] != "NonDimmable"
|
||||
|
||||
room = next((r for r in site_data["rooms"] if r["roomId"] == device["roomId"]), {})
|
||||
|
||||
retval[address] = {
|
||||
"address": address,
|
||||
"BLE_address": BLE_address,
|
||||
"name": device["title"],
|
||||
"type": deviceType.type,
|
||||
"model": deviceType.model,
|
||||
"dimmable": dimmable,
|
||||
"room": room.get("title"),
|
||||
"firmware": firmware,
|
||||
"data": {
|
||||
"name": device["title"],
|
||||
"hardwareId": plejdDevice["hardwareId"],
|
||||
"dimmable": dimmable,
|
||||
"room": room.get("title"),
|
||||
"firmware": plejdDevice["firmware"]["version"],
|
||||
}
|
||||
}
|
||||
|
||||
return retval
|
||||
|
||||
@@ -1,19 +1,44 @@
|
||||
|
||||
from builtins import property
|
||||
from collections import namedtuple
|
||||
|
||||
Device = namedtuple("Device", ["model", "type", "dimmable"])
|
||||
|
||||
LIGHT = "light"
|
||||
SENSOR = "sensor"
|
||||
SWITCH = "switch"
|
||||
|
||||
HARDWARE_TYPES = {
|
||||
"0": Device("-unknown-", LIGHT, False),
|
||||
"1": Device("DIM-01", LIGHT, True),
|
||||
"2": Device("DIM-02", LIGHT, True),
|
||||
"3": Device("CTR-01", LIGHT, False),
|
||||
"4": Device("GWY-01", SENSOR, False),
|
||||
"5": Device("LED-10", LIGHT, True),
|
||||
"6": Device("WPH-01", SWITCH, False),
|
||||
"7": Device("REL-01", SWITCH, False),
|
||||
"8": Device("-unknown-", LIGHT, False),
|
||||
"9": Device("-unknown-", LIGHT, False),
|
||||
"10": Device("-unknown-", LIGHT, False),
|
||||
"11": Device("DIM-01", LIGHT, True),
|
||||
"12": Device("-unknown-", LIGHT, False),
|
||||
"13": Device("Generic", LIGHT, False),
|
||||
"14": Device("-unknown-", LIGHT, False),
|
||||
"15": Device("-unknown-", LIGHT, False),
|
||||
"16": Device("-unknown-", LIGHT, False),
|
||||
"17": Device("REL-01", SWITCH, False),
|
||||
"18": Device("REL-02", SWITCH, False),
|
||||
"19": Device("-unknown-", LIGHT, False),
|
||||
"20": Device("SPR-01", SWITCH, False),
|
||||
}
|
||||
|
||||
class PlejdDevice:
|
||||
|
||||
def __init__(self, manager, address, BLE_address, name, type, model, dimmable, room, firmware):
|
||||
def __init__(self, manager, address, BLE_address, data):
|
||||
self.manager = manager
|
||||
self.address = address
|
||||
self._BLE_address = BLE_address
|
||||
self.name = name
|
||||
self.type = type
|
||||
self.model = model
|
||||
self.dimmable = dimmable
|
||||
self.room = room
|
||||
self.firmware = firmware
|
||||
self.data = data #{name, hardwareId, dimmable, room, firmware}
|
||||
|
||||
self.updateCallback = None
|
||||
|
||||
@@ -26,12 +51,37 @@ class PlejdDevice:
|
||||
|
||||
@property
|
||||
def dim(self):
|
||||
return self._dim
|
||||
return self._dim/255
|
||||
|
||||
@property
|
||||
def BLE_address(self):
|
||||
return self._BLE_address
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return self.data["name"]
|
||||
@property
|
||||
def room(self):
|
||||
return self.data["room"]
|
||||
@property
|
||||
def firmware(self):
|
||||
return self.data["firmware"]
|
||||
|
||||
@property
|
||||
def type(self):
|
||||
return self.hardware_data.type
|
||||
@property
|
||||
def model(self):
|
||||
return self.hardware_data.model
|
||||
@property
|
||||
def dimmable(self):
|
||||
return self.hardware_data.dimmable and self.data["dimmable"] != False
|
||||
|
||||
@property
|
||||
def hardware_data(self):
|
||||
deviceType = HARDWARE_TYPES.get(self.data["hardwareId"], HARDWARE_TYPES["0"])
|
||||
return deviceType
|
||||
|
||||
async def new_state(self, state, dim):
|
||||
update = False
|
||||
if state != self._state:
|
||||
|
||||
Reference in New Issue
Block a user