Cleanup and refactoring
This commit is contained in:
parent
53d1f1f9ae
commit
17eafdee41
@ -11,6 +11,8 @@ from . import pyplejd
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = "plejd"
|
||||
|
||||
async def async_setup(hass, config):
|
||||
_LOGGER.error("Setting up plejd")
|
||||
if not hass.config_entries.async_entries("plejd"):
|
||||
@ -23,17 +25,8 @@ async def async_setup(hass, config):
|
||||
return True
|
||||
|
||||
|
||||
BLE_UUID_SUFFIX = '6085-4726-be45-040c957391b5'
|
||||
PLEJD_SERVICE = f'31ba0001-{BLE_UUID_SUFFIX}'
|
||||
|
||||
DEVICE_ADDR = "fc:f8:73:37:78:0e"
|
||||
|
||||
DOMAIN = "plejd"
|
||||
|
||||
async def async_setup_entry(hass, config_entry):
|
||||
|
||||
_LOGGER.info(config_entry.data)
|
||||
|
||||
plejdManager = pyplejd.PlejdManager(config_entry.data)
|
||||
devices = await plejdManager.get_devices()
|
||||
for dev in devices.values():
|
||||
@ -57,17 +50,11 @@ async def async_setup_entry(hass, config_entry):
|
||||
if hass.data[DOMAIN]["stopping"]: return
|
||||
if not await plejdManager.keepalive():
|
||||
_LOGGER.debug("Ping failed")
|
||||
else:
|
||||
await plejdManager.poll() # TODO: Remove when not needed
|
||||
hass.data[DOMAIN]["ping_timer"] = async_track_point_in_utc_time(
|
||||
hass,
|
||||
_ping,
|
||||
dt_util.utcnow() + plejdManager.keepalive_interval
|
||||
)
|
||||
# TODO: Pinging often now because that's how to get updates with an ESP repeater
|
||||
# Once that's been fixed and the esp gets the LASTDATA announcements this can be
|
||||
# increased significantly to like 5-10 minutes
|
||||
|
||||
hass.async_create_task(_ping())
|
||||
|
||||
bluetooth.async_register_callback(
|
||||
@ -81,25 +68,20 @@ async def async_setup_entry(hass, config_entry):
|
||||
)
|
||||
|
||||
|
||||
|
||||
async def _stop(ev):
|
||||
hass.data[DOMAIN]["stopping"] = True
|
||||
if "ping_timer" in hass.data[DOMAIN]:
|
||||
hass.data[DOMAIN]["ping_timer"]()
|
||||
await plejdManager.disconnect()
|
||||
|
||||
config_entry.async_on_unload(
|
||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _stop)
|
||||
)
|
||||
|
||||
for service_info in bluetooth.async_discovered_service_info(hass, True):
|
||||
if PLEJD_SERVICE.lower() in service_info.advertisement.service_uuids:
|
||||
if pyplejd.PLEJD_SERVICE.lower() in service_info.advertisement.service_uuids:
|
||||
plejdManager.discover_plejd(service_info)
|
||||
|
||||
|
||||
_LOGGER.error("async_setup_entry done")
|
||||
|
||||
|
||||
return True
|
||||
|
||||
|
||||
|
@ -72,7 +72,7 @@ class PlejdLight(LightEntity, CoordinatorEntity):
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
return self._data.get("dim",0)/255
|
||||
return self._data.get("dim",0)
|
||||
|
||||
@property
|
||||
def supported_color_modes(self):
|
||||
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user