Cleanup and refactoring

This commit is contained in:
2022-10-15 23:25:44 +02:00
parent 53d1f1f9ae
commit 17eafdee41
5 changed files with 80 additions and 76 deletions

View File

@@ -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(
@@ -79,7 +66,6 @@ async def async_setup_entry(hass, config_entry):
),
bluetooth.BluetoothScanningMode.PASSIVE
)
async def _stop(ev):
@@ -87,19 +73,15 @@ async def async_setup_entry(hass, config_entry):
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