From 181264277353dee79fa394b9fb047aba03d5ac40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Lov=C3=A9n?= Date: Sun, 16 Oct 2022 22:19:01 +0200 Subject: [PATCH] Allow downloading site data as diagnostics --- custom_components/plejd/__init__.py | 15 +++++++++++++++ custom_components/plejd/diagnostics.py | 6 ++++++ custom_components/plejd/pyplejd/__init__.py | 5 ++++- custom_components/plejd/pyplejd/api.py | 6 +++++- custom_components/plejd/pyplejd/plejd_device.py | 4 ++-- custom_components/plejd/switch.py | 4 ++-- 6 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 custom_components/plejd/diagnostics.py diff --git a/custom_components/plejd/__init__.py b/custom_components/plejd/__init__.py index 2783032..683eb1c 100644 --- a/custom_components/plejd/__init__.py +++ b/custom_components/plejd/__init__.py @@ -6,6 +6,7 @@ from homeassistant import config_entries from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.const import EVENT_HOMEASSISTANT_STOP import homeassistant.util.dt as dt_util +from homeassistant.helpers import device_registry as dr from . import pyplejd @@ -28,7 +29,21 @@ async def async_setup(hass, config): async def async_setup_entry(hass, config_entry): plejdManager = pyplejd.PlejdManager(config_entry.data) + devices = await plejdManager.get_devices() + + if sum(d.type in ["light", "switch"] for d in devices.values()) == 0: + site_data = await plejdManager.get_site_data() + + device_registry = dr.async_get(hass) + device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + identifiers={(DOMAIN, config_entry.data["siteId"])}, + manufacturer="Plejd", + name=site_data.get("site", {}).get("title", "Unknown site"), + entry_type=dr.DeviceEntryType.SERVICE, + ) + for dev in devices.values(): ble_device = bluetooth.async_ble_device_from_address( hass, dev.BLE_address, True diff --git a/custom_components/plejd/diagnostics.py b/custom_components/plejd/diagnostics.py new file mode 100644 index 0000000..544d285 --- /dev/null +++ b/custom_components/plejd/diagnostics.py @@ -0,0 +1,6 @@ +from . import pyplejd + +async def async_get_config_entry_diagnostics(hass, config_entry): + plejdManager = pyplejd.PlejdManager(config_entry.data) + + return await plejdManager.get_site_data() \ No newline at end of file diff --git a/custom_components/plejd/pyplejd/__init__.py b/custom_components/plejd/pyplejd/__init__.py index 5a6f8e3..92fb5b9 100644 --- a/custom_components/plejd/pyplejd/__init__.py +++ b/custom_components/plejd/pyplejd/__init__.py @@ -4,7 +4,7 @@ from datetime import timedelta from bleak_retry_connector import close_stale_connections from .mesh import PlejdMesh -from .api import get_cryptokey, get_devices +from .api import get_cryptokey, get_devices, get_site_data from .plejd_device import PlejdDevice from .const import PLEJD_SERVICE @@ -32,6 +32,9 @@ class PlejdManager: def connected(self): return self.mesh is not None and self.mesh.connected + async def get_site_data(self): + return await get_site_data(**self.credentials) + async def get_devices(self): devices = await get_devices(**self.credentials) self.devices = {k: PlejdDevice(self, **v) for (k,v) in devices.items()} diff --git a/custom_components/plejd/pyplejd/api.py b/custom_components/plejd/pyplejd/api.py index c54e25c..5384672 100644 --- a/custom_components/plejd/pyplejd/api.py +++ b/custom_components/plejd/pyplejd/api.py @@ -43,13 +43,17 @@ async def _get_site_details(session, siteId): # fp.write(json.dumps(data)) return data +site_data = None async def get_site_data(username, password, siteId): - # TODO: Memoize this somehow? + global site_data + if site_data is not None: + return site_data async with ClientSession(base_url=API_BASE_URL, headers=headers) as session: session_token = await _login(session, username, password) _LOGGER.debug("Session token: %s", session_token) session.headers["X-Parse-Session-Token"] = session_token details = await _get_site_details(session, siteId) + site_data = details return details async def get_sites(username, password): diff --git a/custom_components/plejd/pyplejd/plejd_device.py b/custom_components/plejd/pyplejd/plejd_device.py index 0b613e1..0e9ef5c 100644 --- a/custom_components/plejd/pyplejd/plejd_device.py +++ b/custom_components/plejd/pyplejd/plejd_device.py @@ -7,10 +7,10 @@ Device = namedtuple("Device", ["model", "type", "dimmable"]) LIGHT = "light" SENSOR = "sensor" SWITCH = "switch" - +UNKNOWN = "unknown" HARDWARE_TYPES = { - "0": Device("-unknown-", LIGHT, False), + "0": Device("-unknown-", UNKNOWN, False), "1": Device("DIM-01", LIGHT, True), "2": Device("DIM-02", LIGHT, True), "3": Device("CTR-01", LIGHT, False), diff --git a/custom_components/plejd/switch.py b/custom_components/plejd/switch.py index bd16b18..b7c920a 100644 --- a/custom_components/plejd/switch.py +++ b/custom_components/plejd/switch.py @@ -19,8 +19,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities): async def updateCallback(data): coordinator.async_set_updated_data(data) dev.updateCallback = updateCallback - light = PlejdSwitch(coordinator, dev) - entities.append(light) + switch = PlejdSwitch(coordinator, dev) + entities.append(switch) async_add_entities(entities, False) class Coordinator(DataUpdateCoordinator):