Make setup of Ecovacs async (#96200)
* make setup async * apply suggestions
This commit is contained in:
parent
99e7b42127
commit
cce9d938f6
2 changed files with 46 additions and 41 deletions
|
@ -46,12 +46,11 @@ ECOVACS_API_DEVICEID = "".join(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
"""Set up the Ecovacs component."""
|
"""Set up the Ecovacs component."""
|
||||||
_LOGGER.debug("Creating new Ecovacs component")
|
_LOGGER.debug("Creating new Ecovacs component")
|
||||||
|
|
||||||
hass.data[ECOVACS_DEVICES] = []
|
def get_devices() -> list[VacBot]:
|
||||||
|
|
||||||
ecovacs_api = EcoVacsAPI(
|
ecovacs_api = EcoVacsAPI(
|
||||||
ECOVACS_API_DEVICEID,
|
ECOVACS_API_DEVICEID,
|
||||||
config[DOMAIN].get(CONF_USERNAME),
|
config[DOMAIN].get(CONF_USERNAME),
|
||||||
|
@ -59,11 +58,11 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
config[DOMAIN].get(CONF_COUNTRY),
|
config[DOMAIN].get(CONF_COUNTRY),
|
||||||
config[DOMAIN].get(CONF_CONTINENT),
|
config[DOMAIN].get(CONF_CONTINENT),
|
||||||
)
|
)
|
||||||
|
ecovacs_devices = ecovacs_api.devices()
|
||||||
|
_LOGGER.debug("Ecobot devices: %s", ecovacs_devices)
|
||||||
|
|
||||||
devices = ecovacs_api.devices()
|
devices: list[VacBot] = []
|
||||||
_LOGGER.debug("Ecobot devices: %s", devices)
|
for device in ecovacs_devices:
|
||||||
|
|
||||||
for device in devices:
|
|
||||||
_LOGGER.info(
|
_LOGGER.info(
|
||||||
"Discovered Ecovacs device on account: %s with nickname %s",
|
"Discovered Ecovacs device on account: %s with nickname %s",
|
||||||
device.get("did"),
|
device.get("did"),
|
||||||
|
@ -78,22 +77,29 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
||||||
config[DOMAIN].get(CONF_CONTINENT).lower(),
|
config[DOMAIN].get(CONF_CONTINENT).lower(),
|
||||||
monitor=True,
|
monitor=True,
|
||||||
)
|
)
|
||||||
hass.data[ECOVACS_DEVICES].append(vacbot)
|
|
||||||
|
|
||||||
def stop(event: object) -> None:
|
devices.append(vacbot)
|
||||||
|
return devices
|
||||||
|
|
||||||
|
hass.data[ECOVACS_DEVICES] = await hass.async_add_executor_job(get_devices)
|
||||||
|
|
||||||
|
async def async_stop(event: object) -> None:
|
||||||
"""Shut down open connections to Ecovacs XMPP server."""
|
"""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(
|
_LOGGER.info(
|
||||||
"Shutting down connection to Ecovacs device %s",
|
"Shutting down connection to Ecovacs device %s",
|
||||||
device.vacuum.get("did"),
|
device.vacuum.get("did"),
|
||||||
)
|
)
|
||||||
device.disconnect()
|
await hass.async_add_executor_job(device.disconnect)
|
||||||
|
|
||||||
# Listen for HA stop to 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]:
|
if hass.data[ECOVACS_DEVICES]:
|
||||||
_LOGGER.debug("Starting vacuum components")
|
_LOGGER.debug("Starting vacuum components")
|
||||||
discovery.load_platform(hass, Platform.VACUUM, DOMAIN, {}, config)
|
hass.async_create_task(
|
||||||
|
discovery.async_load_platform(hass, Platform.VACUUM, DOMAIN, {}, config)
|
||||||
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -28,18 +28,20 @@ ATTR_ERROR = "error"
|
||||||
ATTR_COMPONENT_PREFIX = "component_"
|
ATTR_COMPONENT_PREFIX = "component_"
|
||||||
|
|
||||||
|
|
||||||
def setup_platform(
|
async def async_setup_platform(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
config: ConfigType,
|
config: ConfigType,
|
||||||
add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
discovery_info: DiscoveryInfoType | None = None,
|
discovery_info: DiscoveryInfoType | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the Ecovacs vacuums."""
|
"""Set up the Ecovacs vacuums."""
|
||||||
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))
|
vacuums.append(EcovacsVacuum(device))
|
||||||
_LOGGER.debug("Adding Ecovacs Vacuums to Home Assistant: %s", vacuums)
|
_LOGGER.debug("Adding Ecovacs Vacuums to Home Assistant: %s", vacuums)
|
||||||
add_entities(vacuums, True)
|
async_add_entities(vacuums)
|
||||||
|
|
||||||
|
|
||||||
class EcovacsVacuum(StateVacuumEntity):
|
class EcovacsVacuum(StateVacuumEntity):
|
||||||
|
@ -62,15 +64,12 @@ class EcovacsVacuum(StateVacuumEntity):
|
||||||
def __init__(self, device: sucks.VacBot) -> None:
|
def __init__(self, device: sucks.VacBot) -> None:
|
||||||
"""Initialize the Ecovacs Vacuum."""
|
"""Initialize the Ecovacs Vacuum."""
|
||||||
self.device = device
|
self.device = device
|
||||||
self.device.connect_and_wait_until_ready()
|
|
||||||
vacuum = self.device.vacuum
|
vacuum = self.device.vacuum
|
||||||
|
|
||||||
self.error = None
|
self.error = None
|
||||||
self._attr_unique_id = vacuum["did"]
|
self._attr_unique_id = vacuum["did"]
|
||||||
self._attr_name = vacuum.get("nick", vacuum["did"])
|
self._attr_name = vacuum.get("nick", vacuum["did"])
|
||||||
|
|
||||||
_LOGGER.debug("StateVacuum initialized: %s", self.name)
|
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Set up the event listeners now that hass is ready."""
|
"""Set up the event listeners now that hass is ready."""
|
||||||
self.device.statusEvents.subscribe(lambda _: self.schedule_update_ha_state())
|
self.device.statusEvents.subscribe(lambda _: self.schedule_update_ha_state())
|
||||||
|
|
Loading…
Add table
Reference in a new issue