From 4f92ce5ebacf376f7f74f78585adb6ca4576e4e9 Mon Sep 17 00:00:00 2001 From: bittles Date: Sat, 27 Apr 2024 17:40:15 -0500 Subject: [PATCH] async changes put back in to push for 1.7.0 --- custom_components/ecovacs/__init__.py | 78 ++++++++++++++------------- custom_components/ecovacs/vacuum.py | 13 ++--- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/custom_components/ecovacs/__init__.py b/custom_components/ecovacs/__init__.py index fd5af4e..51ae539 100644 --- a/custom_components/ecovacs/__init__.py +++ b/custom_components/ecovacs/__init__.py @@ -1,4 +1,3 @@ - """Support for Ecovacs Deebot vacuums.""" import random import string @@ -45,55 +44,60 @@ random.choice(string.ascii_uppercase + string.digits) for _ in range(8) ) -def setup(hass: HomeAssistant, config: ConfigType) -> bool: +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the Ecovacs component.""" LOGGER.debug("Creating new Ecovacs component") - hass.data[ECOVACS_DEVICES] = [] + def get_devices() -> list[VacBot]: + ecovacs_api = EcoVacsAPI( + ECOVACS_API_DEVICEID, + config[DOMAIN].get(CONF_USERNAME), + EcoVacsAPI.md5(config[DOMAIN].get(CONF_PASSWORD)), + config[DOMAIN].get(CONF_COUNTRY), + config[DOMAIN].get(CONF_CONTINENT), + ) + ecovacs_devices = ecovacs_api.devices() + _LOGGER.debug("Ecobot devices: %s", ecovacs_devices) + SERVER_ADDRESS = None - ecovacs_api = EcoVacsAPI( - ECOVACS_API_DEVICEID, - config[DOMAIN].get(CONF_USERNAME), - EcoVacsAPI.md5(config[DOMAIN].get(CONF_PASSWORD)), - config[DOMAIN].get(CONF_COUNTRY), - config[DOMAIN].get(CONF_CONTINENT), - config[DOMAIN].get(CONF_VERIFY_SSL), # add to class call - ) + devices: list[VacBot] = [] + for device in ecovacs_devices: + _LOGGER.info( + "Discovered Ecovacs device on account: %s with nickname %s", + device.get("did"), + device.get("nick"), + ) + vacbot = VacBot( + ecovacs_api.uid, + ecovacs_api.REALM, + ecovacs_api.resource, + ecovacs_api.user_access_token, + device, + config[DOMAIN].get(CONF_CONTINENT).lower(), + config[DOMAIN].get(CONF_VERIFY_SSL), # add to class call + monitor=True, + ) - devices = ecovacs_api.devices() - LOGGER.debug("Ecobot devices: %s", devices) + devices.append(vacbot) + return devices - for device in devices: - LOGGER.info( - "Discovered Ecovacs device on account: %s with nickname %s", - device.get("did"), - device.get("nick"), - ) - vacbot = VacBot( - ecovacs_api.uid, - ecovacs_api.REALM, - ecovacs_api.resource, - ecovacs_api.user_access_token, - device, - config[DOMAIN].get(CONF_CONTINENT).lower(), - SERVER_ADDRESS, # include server address in class, if it's null should be no effect - config[DOMAIN].get(CONF_VERIFY_SSL), # add to class call - monitor=True - ) - hass.data[ECOVACS_DEVICES].append(vacbot) + hass.data[ECOVACS_DEVICES] = await hass.async_add_executor_job(get_devices) - def stop(event: object) -> None: + async def async_stop(event: object) -> None: """Shut down open connections to Ecovacs XMPP server.""" - for device in hass.data[ECOVACS_DEVICES]: + devices: list[VacBot] = hass.data[ECOVACS_DEVICES] + for device in devices: LOGGER.info( "Shutting down connection to Ecovacs device %s", device.vacuum.get("did"), ) - device.disconnect() + await hass.async_add_executor_job(device.disconnect) # Listen for HA stop to disconnect. - hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop) + hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop) if hass.data[ECOVACS_DEVICES]: LOGGER.debug("Starting vacuum components") - discovery.load_platform(hass, Platform.VACUUM, DOMAIN, {}, config) - return True + hass.async_create_task( + discovery.async_load_platform(hass, Platform.VACUUM, DOMAIN, {}, config) + ) + return True \ No newline at end of file diff --git a/custom_components/ecovacs/vacuum.py b/custom_components/ecovacs/vacuum.py index 50f283c..e18aa7e 100644 --- a/custom_components/ecovacs/vacuum.py +++ b/custom_components/ecovacs/vacuum.py @@ -21,18 +21,20 @@ ATTR_COMPONENT_PREFIX = "component_" -def setup_platform( +async def async_setup_platform( hass: HomeAssistant, config: ConfigType, - add_entities: AddEntitiesCallback, + async_add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the Ecovacs vacuums.""" vacuums = [] - for device in hass.data[ECOVACS_DEVICES]: + devices: list[sucks.VacBot] = hass.data[ECOVACS_DEVICES] + for device in devices: + await hass.async_add_executor_job(device.connect_and_wait_until_ready) vacuums.append(EcovacsVacuum(device)) _LOGGER.debug("Adding Ecovacs Vacuums to Home Assistant: %s", vacuums) - add_entities(vacuums, True) + async_add_entities(vacuums) class EcovacsVacuum(StateVacuumEntity): @@ -56,7 +58,7 @@ class EcovacsVacuum(StateVacuumEntity): def __init__(self, device: sucks.VacBot) -> None: """Initialize the Ecovacs Vacuum.""" self.device = device - self.device.connect_and_wait_until_ready() + if self.device.vacuum.get("nick") is not None: self._attr_name = str(self.device.vacuum["nick"]) else: @@ -64,7 +66,6 @@ def __init__(self, device: sucks.VacBot) -> None: self._attr_name = str(format(self.device.vacuum["did"])) self._error = None - _LOGGER.debug("Vacuum initialized: %s", self.name) async def async_added_to_hass(self) -> None: """Set up the event listeners now that hass is ready."""