2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Ecovacs Deebot vacuums."""
|
2018-08-20 15:42:53 +00:00
|
|
|
import logging
|
|
|
|
|
2019-11-26 02:01:48 +00:00
|
|
|
from sucks import EcoVacsAPI, VacBot
|
2018-08-20 15:42:53 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2024-01-16 12:31:42 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2022-01-10 09:30:47 +00:00
|
|
|
from homeassistant.const import (
|
2024-01-16 12:31:42 +00:00
|
|
|
CONF_COUNTRY,
|
2022-01-10 09:30:47 +00:00
|
|
|
CONF_PASSWORD,
|
|
|
|
CONF_USERNAME,
|
|
|
|
EVENT_HOMEASSISTANT_STOP,
|
|
|
|
Platform,
|
|
|
|
)
|
2021-12-31 10:05:44 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-02-13 20:21:14 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2021-12-31 10:05:44 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2024-01-16 12:31:42 +00:00
|
|
|
from .const import CONF_CONTINENT, DOMAIN
|
|
|
|
from .util import get_client_device_id
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2024-01-16 12:31:42 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-08-20 15:42:53 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
vol.Required(CONF_COUNTRY): vol.All(vol.Lower, cv.string),
|
|
|
|
vol.Required(CONF_CONTINENT): vol.All(vol.Lower, cv.string),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2024-01-16 12:31:42 +00:00
|
|
|
PLATFORMS = [
|
|
|
|
Platform.VACUUM,
|
|
|
|
]
|
2018-08-20 15:42:53 +00:00
|
|
|
|
|
|
|
|
2023-07-24 22:07:43 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
2018-08-20 15:42:53 +00:00
|
|
|
"""Set up the Ecovacs component."""
|
2024-01-16 12:31:42 +00:00
|
|
|
if DOMAIN in config:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=config[DOMAIN]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up this integration using UI."""
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2023-07-24 22:07:43 +00:00
|
|
|
def get_devices() -> list[VacBot]:
|
|
|
|
ecovacs_api = EcoVacsAPI(
|
2024-01-16 12:31:42 +00:00
|
|
|
get_client_device_id(),
|
|
|
|
entry.data[CONF_USERNAME],
|
|
|
|
EcoVacsAPI.md5(entry.data[CONF_PASSWORD]),
|
|
|
|
entry.data[CONF_COUNTRY],
|
|
|
|
entry.data[CONF_CONTINENT],
|
2023-07-24 22:07:43 +00:00
|
|
|
)
|
|
|
|
ecovacs_devices = ecovacs_api.devices()
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2024-01-16 12:31:42 +00:00
|
|
|
_LOGGER.debug("Ecobot devices: %s", ecovacs_devices)
|
2023-07-24 22:07:43 +00:00
|
|
|
devices: list[VacBot] = []
|
|
|
|
for device in ecovacs_devices:
|
2024-01-16 12:31:42 +00:00
|
|
|
_LOGGER.debug(
|
2023-07-24 22:07:43 +00:00
|
|
|
"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,
|
2024-01-16 12:31:42 +00:00
|
|
|
entry.data[CONF_CONTINENT],
|
2023-07-24 22:07:43 +00:00
|
|
|
monitor=True,
|
|
|
|
)
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2023-07-24 22:07:43 +00:00
|
|
|
devices.append(vacbot)
|
|
|
|
return devices
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2024-01-16 12:31:42 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[
|
|
|
|
entry.entry_id
|
|
|
|
] = await hass.async_add_executor_job(get_devices)
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2023-07-24 22:07:43 +00:00
|
|
|
async def async_stop(event: object) -> None:
|
2018-08-20 15:42:53 +00:00
|
|
|
"""Shut down open connections to Ecovacs XMPP server."""
|
2024-01-16 12:31:42 +00:00
|
|
|
devices: list[VacBot] = hass.data[DOMAIN][entry.entry_id]
|
2023-07-24 22:07:43 +00:00
|
|
|
for device in devices:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.info(
|
2021-07-07 18:18:43 +00:00
|
|
|
"Shutting down connection to Ecovacs device %s",
|
|
|
|
device.vacuum.get("did"),
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2023-07-24 22:07:43 +00:00
|
|
|
await hass.async_add_executor_job(device.disconnect)
|
2018-08-20 15:42:53 +00:00
|
|
|
|
|
|
|
# Listen for HA stop to disconnect.
|
2023-07-24 22:07:43 +00:00
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop)
|
2018-08-20 15:42:53 +00:00
|
|
|
|
2024-01-16 12:31:42 +00:00
|
|
|
if hass.data[DOMAIN][entry.entry_id]:
|
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2018-08-20 15:42:53 +00:00
|
|
|
|
|
|
|
return True
|