Add new Roborock Integration (#89456)

* init roborock commit

* init commit of roborock

* removed some non-vacuum related code

* removed some non-needed constants

* removed translations

* removed options flow

* removed manual control

* remove password login

* removed go-to

* removed unneeded function and improved device_stat

* removed utils as it is unused

* typing changes in vacuum.py

* fixed test patch paths

* removed unneeded records

* removing unneeded code in tests

* remove password from strings

* removed maps in code

* changed const, reworked functions

* remove menu

* fixed tests

* 100% code coverage config_flow

* small changes

* removed unneeded patch

* bump to 0.1.7

* removed services

* removed extra functions and mop

* add () to configEntryNotReady

* moved coordinator into seperate file

* update roborock testing

* removed stale options code

* normalize username for unique id

* removed unneeded variables

* fixed linter problems

* removed stale comment

* additional pr changes

* simplify config_flow

* fix config flow test

* Apply suggestions from code review

Co-authored-by: Allen Porter <allen.porter@gmail.com>

* First pass at resolving PR comments

* reworked config flow

* moving vacuum attr

* attempt to clean up conflig flow more

* update package and use offline functionality

* Fixed errors and fan bug

* rework model and some other small changes

* bump version

* used default factory

* moved some client creation into coord

* fixed patch

* Update homeassistant/components/roborock/coordinator.py

Co-authored-by: Allen Porter <allen.porter@gmail.com>

* moved async functions into gather

* reworked gathers

* removed random line

* error catch if networking doesn't exist or timeout

* bump to 0.6.5

* fixed mocked data reference url

* change checking if we have no network information

Co-authored-by: Allen Porter <allen.porter@gmail.com>

---------

Co-authored-by: Allen Porter <allen.porter@gmail.com>
Co-authored-by: Allen Porter <allen@thebends.org>
This commit is contained in:
Luke
2023-04-20 10:02:58 -04:00
committed by GitHub
parent af193094b5
commit b4e0a1f1fc
22 changed files with 1218 additions and 4 deletions

View File

@@ -0,0 +1,77 @@
"""The Roborock component."""
from __future__ import annotations
import asyncio
from datetime import timedelta
import logging
from roborock.api import RoborockApiClient
from roborock.cloud_api import RoborockMqttClient
from roborock.containers import HomeDataDevice, RoborockDeviceInfo, UserData
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from .const import CONF_BASE_URL, CONF_USER_DATA, DOMAIN, PLATFORMS
from .coordinator import RoborockDataUpdateCoordinator
SCAN_INTERVAL = timedelta(seconds=30)
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up roborock from a config entry."""
_LOGGER.debug("Integration async setup entry: %s", entry.as_dict())
user_data = UserData.from_dict(entry.data[CONF_USER_DATA])
api_client = RoborockApiClient(entry.data[CONF_USERNAME], entry.data[CONF_BASE_URL])
_LOGGER.debug("Getting home data")
home_data = await api_client.get_home_data(user_data)
_LOGGER.debug("Got home data %s", home_data)
devices: list[HomeDataDevice] = home_data.devices + home_data.received_devices
# Create a mqtt_client, which is needed to get the networking information of the device for local connection and in the future, get the map.
mqtt_client = RoborockMqttClient(
user_data, {device.duid: RoborockDeviceInfo(device) for device in devices}
)
network_results = await asyncio.gather(
*(mqtt_client.get_networking(device.duid) for device in devices)
)
network_info = {
device.duid: result
for device, result in zip(devices, network_results)
if result is not None
}
await mqtt_client.async_disconnect()
if not network_info:
raise ConfigEntryNotReady(
"Could not get network information about your devices"
)
product_info = {product.id: product for product in home_data.products}
coordinator = RoborockDataUpdateCoordinator(
hass,
devices,
network_info,
product_info,
)
await coordinator.async_config_entry_first_refresh()
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
await hass.data[DOMAIN][entry.entry_id].release()
hass.data[DOMAIN].pop(entry.entry_id)
return unload_ok

View File

@@ -0,0 +1,99 @@
"""Config flow for Roborock."""
from __future__ import annotations
import logging
from typing import Any
from roborock.api import RoborockApiClient
from roborock.containers import UserData
from roborock.exceptions import RoborockException
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_USERNAME
from homeassistant.data_entry_flow import FlowResult
from .const import CONF_BASE_URL, CONF_ENTRY_CODE, CONF_USER_DATA, DOMAIN
_LOGGER = logging.getLogger(__name__)
class RoborockFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for Roborock."""
VERSION = 1
def __init__(self) -> None:
"""Initialize the config flow."""
self._username: str | None = None
self._client: RoborockApiClient | None = None
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
"""Handle a flow initialized by the user."""
errors: dict[str, str] = {}
if user_input is not None:
username = user_input[CONF_USERNAME]
await self.async_set_unique_id(username.lower())
self._abort_if_unique_id_configured()
self._username = username
_LOGGER.debug("Requesting code for Roborock account")
self._client = RoborockApiClient(username)
try:
await self._client.request_code()
except RoborockException as ex:
_LOGGER.exception(ex)
errors["base"] = "invalid_email"
except Exception as ex: # pylint: disable=broad-except
_LOGGER.exception(ex)
errors["base"] = "unknown"
else:
return await self.async_step_code()
return self.async_show_form(
step_id="user",
data_schema=vol.Schema({vol.Required(CONF_USERNAME): str}),
errors=errors,
)
async def async_step_code(
self,
user_input: dict[str, Any] | None = None,
) -> FlowResult:
"""Handle a flow initialized by the user."""
errors: dict[str, str] = {}
assert self._client
assert self._username
if user_input is not None:
code = user_input[CONF_ENTRY_CODE]
_LOGGER.debug("Logging into Roborock account using email provided code")
try:
login_data = await self._client.code_login(code)
except RoborockException as ex:
_LOGGER.exception(ex)
errors["base"] = "invalid_code"
except Exception as ex: # pylint: disable=broad-except
_LOGGER.exception(ex)
errors["base"] = "unknown"
else:
return self._create_entry(self._client, self._username, login_data)
return self.async_show_form(
step_id="code",
data_schema=vol.Schema({vol.Required(CONF_ENTRY_CODE): str}),
errors=errors,
)
def _create_entry(
self, client: RoborockApiClient, username: str, user_data: UserData
) -> FlowResult:
"""Finished config flow and create entry."""
return self.async_create_entry(
title=username,
data={
CONF_USERNAME: username,
CONF_USER_DATA: user_data.as_dict(),
CONF_BASE_URL: client.base_url,
},
)

View File

@@ -0,0 +1,9 @@
"""Constants for Roborock."""
from homeassistant.const import Platform
DOMAIN = "roborock"
CONF_ENTRY_CODE = "code"
CONF_BASE_URL = "base_url"
CONF_USER_DATA = "user_data"
PLATFORMS = [Platform.VACUUM]

View File

@@ -0,0 +1,88 @@
"""Roborock Coordinator."""
from __future__ import annotations
import asyncio
from datetime import timedelta
import logging
from roborock.containers import (
HomeDataDevice,
HomeDataProduct,
NetworkInfo,
RoborockLocalDeviceInfo,
)
from roborock.exceptions import RoborockException
from roborock.local_api import RoborockLocalClient
from roborock.typing import RoborockDeviceProp
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from .const import DOMAIN
from .models import RoborockHassDeviceInfo
SCAN_INTERVAL = timedelta(seconds=30)
_LOGGER = logging.getLogger(__name__)
class RoborockDataUpdateCoordinator(
DataUpdateCoordinator[dict[str, RoborockDeviceProp]]
):
"""Class to manage fetching data from the API."""
def __init__(
self,
hass: HomeAssistant,
devices: list[HomeDataDevice],
devices_networking: dict[str, NetworkInfo],
product_info: dict[str, HomeDataProduct],
) -> None:
"""Initialize."""
super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL)
local_devices_info: dict[str, RoborockLocalDeviceInfo] = {}
hass_devices_info: dict[str, RoborockHassDeviceInfo] = {}
for device in devices:
if not (networking := devices_networking.get(device.duid)):
_LOGGER.warning("Device %s is offline and cannot be setup", device.duid)
continue
hass_devices_info[device.duid] = RoborockHassDeviceInfo(
device,
networking,
product_info[device.product_id],
RoborockDeviceProp(),
)
local_devices_info[device.duid] = RoborockLocalDeviceInfo(
device, networking
)
self.api = RoborockLocalClient(local_devices_info)
self.devices_info = hass_devices_info
async def release(self) -> None:
"""Disconnect from API."""
await self.api.async_disconnect()
async def _update_device_prop(self, device_info: RoborockHassDeviceInfo) -> None:
"""Update device properties."""
device_prop = await self.api.get_prop(device_info.device.duid)
if device_prop:
if device_info.props:
device_info.props.update(device_prop)
else:
device_info.props = device_prop
async def _async_update_data(self) -> dict[str, RoborockDeviceProp]:
"""Update data via library."""
try:
asyncio.gather(
*(
self._update_device_prop(device_info)
for device_info in self.devices_info.values()
)
)
except RoborockException as ex:
raise UpdateFailed(ex) from ex
return {
device_id: device_info.props
for device_id, device_info in self.devices_info.items()
}

View File

@@ -0,0 +1,62 @@
"""Support for Roborock device base class."""
from typing import Any
from roborock.containers import Status
from roborock.typing import RoborockCommand
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import RoborockDataUpdateCoordinator
from .const import DOMAIN
from .models import RoborockHassDeviceInfo
class RoborockCoordinatedEntity(CoordinatorEntity[RoborockDataUpdateCoordinator]):
"""Representation of a base a coordinated Roborock Entity."""
_attr_has_entity_name = True
def __init__(
self,
unique_id: str,
device_info: RoborockHassDeviceInfo,
coordinator: RoborockDataUpdateCoordinator,
) -> None:
"""Initialize the coordinated Roborock Device."""
super().__init__(coordinator)
self._attr_unique_id = unique_id
self._device_name = device_info.device.name
self._device_id = device_info.device.duid
self._device_model = device_info.product.model
self._fw_version = device_info.device.fv
@property
def _device_status(self) -> Status:
"""Return the status of the device."""
data = self.coordinator.data
if data:
device_data = data.get(self._device_id)
if device_data:
status = device_data.status
if status:
return status
return Status({})
@property
def device_info(self) -> DeviceInfo:
"""Return the device info."""
return DeviceInfo(
name=self._device_name,
identifiers={(DOMAIN, self._device_id)},
manufacturer="Roborock",
model=self._device_model,
sw_version=self._fw_version,
)
async def send(
self, command: RoborockCommand, params: dict[str, Any] | list[Any] | None = None
) -> dict:
"""Send a command to a vacuum cleaner."""
return await self.coordinator.api.send_command(self._device_id, command, params)

View File

@@ -1,6 +1,10 @@
{
"domain": "roborock",
"name": "Roborock",
"integration_type": "virtual",
"supported_by": "xiaomi_miio"
"codeowners": ["@humbertogontijo", "@Lash-L"],
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/roborock",
"iot_class": "local_polling",
"loggers": ["roborock"],
"requirements": ["python-roborock==0.6.5"]
}

View File

@@ -0,0 +1,15 @@
"""Roborock Models."""
from dataclasses import dataclass
from roborock.containers import HomeDataDevice, HomeDataProduct, NetworkInfo
from roborock.typing import RoborockDeviceProp
@dataclass
class RoborockHassDeviceInfo:
"""A model to describe roborock devices."""
device: HomeDataDevice
network_info: NetworkInfo
product: HomeDataProduct
props: RoborockDeviceProp

View File

@@ -0,0 +1,26 @@
{
"config": {
"step": {
"user": {
"description": "Enter your Roborock email address.",
"data": {
"username": "Email"
}
},
"code": {
"description": "Type the verification code sent to your email",
"data": {
"code": "Verification code"
}
}
},
"error": {
"invalid_code": "The code you entered was incorrect, please check it and try again.",
"invalid_email": "There is no account associated with the email you entered, please try again.",
"unknown": "[%key:common::config_flow::error::unknown%]"
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
}
}
}

View File

@@ -0,0 +1,173 @@
"""Support for Roborock vacuum class."""
from typing import Any
from roborock.code_mappings import RoborockFanPowerCode, RoborockStateCode
from roborock.typing import RoborockCommand
from homeassistant.components.vacuum import (
STATE_CLEANING,
STATE_DOCKED,
STATE_ERROR,
STATE_IDLE,
STATE_PAUSED,
STATE_RETURNING,
StateVacuumEntity,
VacuumEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import slugify
from .const import DOMAIN
from .coordinator import RoborockDataUpdateCoordinator
from .device import RoborockCoordinatedEntity
from .models import RoborockHassDeviceInfo
STATE_CODE_TO_STATE = {
RoborockStateCode["1"]: STATE_IDLE, # "Starting"
RoborockStateCode["2"]: STATE_IDLE, # "Charger disconnected"
RoborockStateCode["3"]: STATE_IDLE, # "Idle"
RoborockStateCode["4"]: STATE_CLEANING, # "Remote control active"
RoborockStateCode["5"]: STATE_CLEANING, # "Cleaning"
RoborockStateCode["6"]: STATE_RETURNING, # "Returning home"
RoborockStateCode["7"]: STATE_CLEANING, # "Manual mode"
RoborockStateCode["8"]: STATE_DOCKED, # "Charging"
RoborockStateCode["9"]: STATE_ERROR, # "Charging problem"
RoborockStateCode["10"]: STATE_PAUSED, # "Paused"
RoborockStateCode["11"]: STATE_CLEANING, # "Spot cleaning"
RoborockStateCode["12"]: STATE_ERROR, # "Error"
RoborockStateCode["13"]: STATE_IDLE, # "Shutting down"
RoborockStateCode["14"]: STATE_DOCKED, # "Updating"
RoborockStateCode["15"]: STATE_RETURNING, # "Docking"
RoborockStateCode["16"]: STATE_CLEANING, # "Going to target"
RoborockStateCode["17"]: STATE_CLEANING, # "Zoned cleaning"
RoborockStateCode["18"]: STATE_CLEANING, # "Segment cleaning"
RoborockStateCode["22"]: STATE_DOCKED, # "Emptying the bin" on s7+
RoborockStateCode["23"]: STATE_DOCKED, # "Washing the mop" on s7maxV
RoborockStateCode["26"]: STATE_RETURNING, # "Going to wash the mop" on s7maxV
RoborockStateCode["100"]: STATE_DOCKED, # "Charging complete"
RoborockStateCode["101"]: STATE_ERROR, # "Device offline"
}
ATTR_STATUS = "status"
ATTR_ERROR = "error"
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Roborock sensor."""
coordinator: RoborockDataUpdateCoordinator = hass.data[DOMAIN][
config_entry.entry_id
]
async_add_entities(
RoborockVacuum(slugify(device_id), device_info, coordinator)
for device_id, device_info in coordinator.devices_info.items()
)
class RoborockVacuum(RoborockCoordinatedEntity, StateVacuumEntity):
"""General Representation of a Roborock vacuum."""
_attr_icon = "mdi:robot-vacuum"
_attr_supported_features = (
VacuumEntityFeature.PAUSE
| VacuumEntityFeature.STOP
| VacuumEntityFeature.RETURN_HOME
| VacuumEntityFeature.FAN_SPEED
| VacuumEntityFeature.BATTERY
| VacuumEntityFeature.STATUS
| VacuumEntityFeature.SEND_COMMAND
| VacuumEntityFeature.LOCATE
| VacuumEntityFeature.CLEAN_SPOT
| VacuumEntityFeature.STATE
| VacuumEntityFeature.START
)
_attr_fan_speed_list = RoborockFanPowerCode.values()
def __init__(
self,
unique_id: str,
device: RoborockHassDeviceInfo,
coordinator: RoborockDataUpdateCoordinator,
) -> None:
"""Initialize a vacuum."""
StateVacuumEntity.__init__(self)
RoborockCoordinatedEntity.__init__(self, unique_id, device, coordinator)
@property
def state(self) -> str | None:
"""Return the status of the vacuum cleaner."""
return STATE_CODE_TO_STATE.get(self._device_status.state)
@property
def status(self) -> str | None:
"""Return the status of the vacuum cleaner."""
return self._device_status.status
@property
def battery_level(self) -> int | None:
"""Return the battery level of the vacuum cleaner."""
return self._device_status.battery
@property
def fan_speed(self) -> str | None:
"""Return the fan speed of the vacuum cleaner."""
return self._device_status.fan_power
@property
def error(self) -> str | None:
"""Get the error str if an error code exists."""
return self._device_status.error
async def async_start(self) -> None:
"""Start the vacuum."""
await self.send(RoborockCommand.APP_START)
async def async_pause(self) -> None:
"""Pause the vacuum."""
await self.send(RoborockCommand.APP_PAUSE)
async def async_stop(self, **kwargs: Any) -> None:
"""Stop the vacuum."""
await self.send(RoborockCommand.APP_STOP)
async def async_return_to_base(self, **kwargs: Any) -> None:
"""Send vacuum back to base."""
await self.send(RoborockCommand.APP_CHARGE)
async def async_clean_spot(self, **kwargs: Any) -> None:
"""Spot clean."""
await self.send(RoborockCommand.APP_SPOT)
async def async_locate(self, **kwargs: Any) -> None:
"""Locate vacuum."""
await self.send(RoborockCommand.FIND_ME)
async def async_set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None:
"""Set vacuum fan speed."""
await self.send(
RoborockCommand.SET_CUSTOM_MODE,
[k for k, v in RoborockFanPowerCode.items() if v == fan_speed],
)
await self.coordinator.async_request_refresh()
async def async_start_pause(self):
"""Start, pause or resume the cleaning task."""
if self.state == STATE_CLEANING:
await self.async_pause()
else:
await self.async_start()
async def async_send_command(
self,
command: str,
params: dict[str, Any] | list[Any] | None = None,
**kwargs: Any,
) -> None:
"""Send a command to a vacuum cleaner."""
await self.send(command, params)

View File

@@ -361,6 +361,7 @@ FLOWS = {
"ring",
"risco",
"rituals_perfume_genie",
"roborock",
"roku",
"roomba",
"roon",

View File

@@ -4590,8 +4590,9 @@
},
"roborock": {
"name": "Roborock",
"integration_type": "virtual",
"supported_by": "xiaomi_miio"
"integration_type": "hub",
"config_flow": true,
"iot_class": "local_polling"
},
"rocketchat": {
"name": "Rocket.Chat",