Cleaner logs. Only connect to devices in site
This commit is contained in:
parent
2a1bb9ee63
commit
ee45fa097d
@ -15,7 +15,6 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
DOMAIN = "plejd"
|
DOMAIN = "plejd"
|
||||||
|
|
||||||
async def async_setup(hass, config):
|
async def async_setup(hass, config):
|
||||||
_LOGGER.error("Setting up plejd")
|
|
||||||
if not hass.config_entries.async_entries("plejd"):
|
if not hass.config_entries.async_entries("plejd"):
|
||||||
hass.async_create_task(
|
hass.async_create_task(
|
||||||
hass.config_entries.flow.async_init(
|
hass.config_entries.flow.async_init(
|
||||||
@ -32,6 +31,7 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
|
|
||||||
devices = await plejdManager.get_devices()
|
devices = await plejdManager.get_devices()
|
||||||
|
|
||||||
|
# Add a service entry if there are no devices - just so the user can get diagnostics data
|
||||||
if sum(d.type in ["light", "switch"] for d in devices.values()) == 0:
|
if sum(d.type in ["light", "switch"] for d in devices.values()) == 0:
|
||||||
site_data = await plejdManager.get_site_data()
|
site_data = await plejdManager.get_site_data()
|
||||||
|
|
||||||
@ -44,13 +44,6 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
entry_type=dr.DeviceEntryType.SERVICE,
|
entry_type=dr.DeviceEntryType.SERVICE,
|
||||||
)
|
)
|
||||||
|
|
||||||
for dev in devices.values():
|
|
||||||
ble_device = bluetooth.async_ble_device_from_address(
|
|
||||||
hass, dev.BLE_address, True
|
|
||||||
)
|
|
||||||
if ble_device:
|
|
||||||
await plejdManager.close_stale(ble_device)
|
|
||||||
|
|
||||||
hass.data.setdefault(DOMAIN, {}).update(
|
hass.data.setdefault(DOMAIN, {}).update(
|
||||||
{
|
{
|
||||||
"stopping": False,
|
"stopping": False,
|
||||||
@ -59,8 +52,36 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Close any stale connections that may be open
|
||||||
|
for dev in devices.values():
|
||||||
|
ble_device = bluetooth.async_ble_device_from_address(
|
||||||
|
hass, dev.BLE_address, True
|
||||||
|
)
|
||||||
|
if ble_device:
|
||||||
|
await plejdManager.close_stale(ble_device)
|
||||||
|
|
||||||
|
# Search for devices in the mesh
|
||||||
|
def _discovered_plejd(service_info, *_):
|
||||||
|
plejdManager.add_mesh_device(service_info.device)
|
||||||
|
bluetooth.async_register_callback(
|
||||||
|
hass,
|
||||||
|
_discovered_plejd,
|
||||||
|
BluetoothCallbackMatcher(
|
||||||
|
connectable=True,
|
||||||
|
service_uuid=pyplejd.const.PLEJD_SERVICE.lower()
|
||||||
|
),
|
||||||
|
bluetooth.BluetoothScanningMode.PASSIVE
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run through already discovered devices and add plejds to the mesh
|
||||||
|
for service_info in bluetooth.async_discovered_service_info(hass, True):
|
||||||
|
if pyplejd.PLEJD_SERVICE.lower() in service_info.advertisement.service_uuids:
|
||||||
|
plejdManager.add_mesh_device(service_info.device)
|
||||||
|
|
||||||
|
|
||||||
await hass.config_entries.async_forward_entry_setups(config_entry, ["light", "switch"])
|
await hass.config_entries.async_forward_entry_setups(config_entry, ["light", "switch"])
|
||||||
|
|
||||||
|
# Ping mesh intermittently to keep the connection alive
|
||||||
async def _ping(now=None):
|
async def _ping(now=None):
|
||||||
if hass.data[DOMAIN]["stopping"]: return
|
if hass.data[DOMAIN]["stopping"]: return
|
||||||
if not await plejdManager.keepalive():
|
if not await plejdManager.keepalive():
|
||||||
@ -72,17 +93,7 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
)
|
)
|
||||||
hass.async_create_task(_ping())
|
hass.async_create_task(_ping())
|
||||||
|
|
||||||
bluetooth.async_register_callback(
|
# Cleanup when Home Assistant stops
|
||||||
hass,
|
|
||||||
plejdManager.discover_plejd,
|
|
||||||
BluetoothCallbackMatcher(
|
|
||||||
connectable=True,
|
|
||||||
service_uuid=pyplejd.const.PLEJD_SERVICE.lower()
|
|
||||||
),
|
|
||||||
bluetooth.BluetoothScanningMode.PASSIVE
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def _stop(ev):
|
async def _stop(ev):
|
||||||
hass.data[DOMAIN]["stopping"] = True
|
hass.data[DOMAIN]["stopping"] = True
|
||||||
if "ping_timer" in hass.data[DOMAIN]:
|
if "ping_timer" in hass.data[DOMAIN]:
|
||||||
@ -92,12 +103,4 @@ async def async_setup_entry(hass, config_entry):
|
|||||||
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _stop)
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _stop)
|
||||||
)
|
)
|
||||||
|
|
||||||
for service_info in bluetooth.async_discovered_service_info(hass, True):
|
|
||||||
if pyplejd.PLEJD_SERVICE.lower() in service_info.advertisement.service_uuids:
|
|
||||||
plejdManager.discover_plejd(service_info)
|
|
||||||
|
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,9 +20,12 @@ class PlejdManager:
|
|||||||
self.devices = { }
|
self.devices = { }
|
||||||
self.credentials = credentials
|
self.credentials = credentials
|
||||||
|
|
||||||
def discover_plejd(self, service_info, *_):
|
def add_mesh_device(self, device):
|
||||||
_LOGGER.debug("Adding plejd %s", service_info)
|
_LOGGER.debug("Adding plejd %s", device)
|
||||||
self.mesh.add_mesh_node(service_info.device)
|
for d in self.devices.items():
|
||||||
|
if d.BLE_address.upper() == device.address.replace(":","").replace("-","").upper():
|
||||||
|
return self.mesh.add_mesh_node(device)
|
||||||
|
_LOGGER.debug("Device was not expected in current mesh")
|
||||||
|
|
||||||
async def close_stale(self, device):
|
async def close_stale(self, device):
|
||||||
_LOGGER.info("Closing stale connections for %s", device)
|
_LOGGER.info("Closing stale connections for %s", device)
|
||||||
|
@ -43,6 +43,10 @@ class PlejdDevice:
|
|||||||
self._state = None
|
self._state = None
|
||||||
self._dim = None
|
self._dim = None
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<PlejdDevice(manager, {self.address}, {self.BLE_address}, {self.data}>"
|
||||||
|
pass
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
return self._state is not None
|
return self._state is not None
|
||||||
@ -104,5 +108,3 @@ class PlejdDevice:
|
|||||||
|
|
||||||
async def turn_off(self):
|
async def turn_off(self):
|
||||||
await self.manager.mesh.set_state(self.address, False)
|
await self.manager.mesh.set_state(self.address, False)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user