Allow downloading site data as diagnostics
This commit is contained in:
parent
85190aa9dd
commit
1812642773
@ -6,6 +6,7 @@ from homeassistant import config_entries
|
|||||||
from homeassistant.helpers.event import async_track_point_in_utc_time
|
from homeassistant.helpers.event import async_track_point_in_utc_time
|
||||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
|
||||||
from . import pyplejd
|
from . import pyplejd
|
||||||
|
|
||||||
@ -28,7 +29,21 @@ async def async_setup(hass, config):
|
|||||||
async def async_setup_entry(hass, config_entry):
|
async def async_setup_entry(hass, config_entry):
|
||||||
|
|
||||||
plejdManager = pyplejd.PlejdManager(config_entry.data)
|
plejdManager = pyplejd.PlejdManager(config_entry.data)
|
||||||
|
|
||||||
devices = await plejdManager.get_devices()
|
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():
|
for dev in devices.values():
|
||||||
ble_device = bluetooth.async_ble_device_from_address(
|
ble_device = bluetooth.async_ble_device_from_address(
|
||||||
hass, dev.BLE_address, True
|
hass, dev.BLE_address, True
|
||||||
|
6
custom_components/plejd/diagnostics.py
Normal file
6
custom_components/plejd/diagnostics.py
Normal file
@ -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()
|
@ -4,7 +4,7 @@ from datetime import timedelta
|
|||||||
from bleak_retry_connector import close_stale_connections
|
from bleak_retry_connector import close_stale_connections
|
||||||
|
|
||||||
from .mesh import PlejdMesh
|
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 .plejd_device import PlejdDevice
|
||||||
|
|
||||||
from .const import PLEJD_SERVICE
|
from .const import PLEJD_SERVICE
|
||||||
@ -32,6 +32,9 @@ class PlejdManager:
|
|||||||
def connected(self):
|
def connected(self):
|
||||||
return self.mesh is not None and self.mesh.connected
|
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):
|
async def get_devices(self):
|
||||||
devices = await get_devices(**self.credentials)
|
devices = await get_devices(**self.credentials)
|
||||||
self.devices = {k: PlejdDevice(self, **v) for (k,v) in devices.items()}
|
self.devices = {k: PlejdDevice(self, **v) for (k,v) in devices.items()}
|
||||||
|
@ -43,13 +43,17 @@ async def _get_site_details(session, siteId):
|
|||||||
# fp.write(json.dumps(data))
|
# fp.write(json.dumps(data))
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
site_data = None
|
||||||
async def get_site_data(username, password, siteId):
|
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:
|
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)
|
||||||
_LOGGER.debug("Session token: %s", session_token)
|
_LOGGER.debug("Session token: %s", session_token)
|
||||||
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
|
||||||
return details
|
return details
|
||||||
|
|
||||||
async def get_sites(username, password):
|
async def get_sites(username, password):
|
||||||
|
@ -7,10 +7,10 @@ Device = namedtuple("Device", ["model", "type", "dimmable"])
|
|||||||
LIGHT = "light"
|
LIGHT = "light"
|
||||||
SENSOR = "sensor"
|
SENSOR = "sensor"
|
||||||
SWITCH = "switch"
|
SWITCH = "switch"
|
||||||
|
UNKNOWN = "unknown"
|
||||||
|
|
||||||
HARDWARE_TYPES = {
|
HARDWARE_TYPES = {
|
||||||
"0": Device("-unknown-", LIGHT, False),
|
"0": Device("-unknown-", UNKNOWN, False),
|
||||||
"1": Device("DIM-01", LIGHT, True),
|
"1": Device("DIM-01", LIGHT, True),
|
||||||
"2": Device("DIM-02", LIGHT, True),
|
"2": Device("DIM-02", LIGHT, True),
|
||||||
"3": Device("CTR-01", LIGHT, False),
|
"3": Device("CTR-01", LIGHT, False),
|
||||||
|
@ -19,8 +19,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
async def updateCallback(data):
|
async def updateCallback(data):
|
||||||
coordinator.async_set_updated_data(data)
|
coordinator.async_set_updated_data(data)
|
||||||
dev.updateCallback = updateCallback
|
dev.updateCallback = updateCallback
|
||||||
light = PlejdSwitch(coordinator, dev)
|
switch = PlejdSwitch(coordinator, dev)
|
||||||
entities.append(light)
|
entities.append(switch)
|
||||||
async_add_entities(entities, False)
|
async_add_entities(entities, False)
|
||||||
|
|
||||||
class Coordinator(DataUpdateCoordinator):
|
class Coordinator(DataUpdateCoordinator):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user