core/tests/components/vacuum/__init__.py

85 lines
2.3 KiB
Python
Raw Normal View History

Xiaomi vacuum as platform of new `vacuum` component derived from ToggleEntity, and services (#8623) * Xiaomi vacuum as component with switch, sensors and services - Conversion from switch platform to async component. - Add services proposed in #8416 to the new component, with shorter names. - Add sensors for the vacuum robot as a selectable list from `battery`, `state`, `error`, `fanspeed`, `clean_time` and `clean_area` (the state attributes of the switch). The sensors don't poll, but listen to a signal to update the state, the switch fires this signal when updating. - Assign default icons to sensors and the switch (`mdi:google-circles-group` looks like the robot!) * path change in requirements_all (from switch platform to component) * copy pasting is a bad habit * services to the components services.yaml, modify .coveragerc * review: use with multiple hosts, fix calls to async_add_devices, fix ranges for services * `icon_for_battery_level` util method * Xiaomi vacuum as platform of new component vacuum - Created new component `vacuum` from a ToggleEntity. - Add services `turn_on`, `turn_off`, `cleaning_play_pause`, `stop`, `return_to_base`, `locate`, `set_fanspeed` and `send_command`. - Remove the main switch for the xiaomi vacuum (the toggable main entity is the switch). - Add `support flags` for the common services - Assign default icons to sensors and the switch (`mdi:google-circles-group` looks like the robot!) - Move services descriptions to a yaml file for the new component. - Update requirements_all. - Update coveragerc. * fix coveragerc * fix battery icon helper to use more icons * remove sensors, create properties and support flags for custom UI * cleaning * updated state_attrs for filtering in UI, renamed platform to simply `xiaomi` * fix platform rename * change fanspeed and expose `fanspeed_list` to use speed steps * minor fixes - Rename service `start_pause` - Add 'Error' attribute only if `got_error`. - Minor changes * rename state attrs * rename state attrs * review changes: cut fan__speed, style changes, remove logging, and more * add ATTR_COMMAND = 'command' to const * pop entity_id from service data * remove property accessor for vacuum object * lint fix * fix extra attrs names * module level functions for calling the services * params as optional keyword for `send_command` * params as optional keyword for `send_command`, remove debug logs * explicit parameters for `set_fan_speed` and `send_command` * Demo platform for the vacuum component * vacuum tests for the Demo platform * some fixes * don't omit vacuum * vacuum tests for the Xiaomi platform * fix test * fix * fix xiaomi test * fix coveragerc * test send command * fix coveragerc * fix string formatting * The coverage is to low. It need 93% or more
2017-08-04 13:27:10 +00:00
"""The tests for vacuum platforms."""
from typing import Any
from homeassistant.components.vacuum import (
DOMAIN,
STATE_CLEANING,
STATE_DOCKED,
STATE_IDLE,
STATE_PAUSED,
STATE_RETURNING,
StateVacuumEntity,
VacuumEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from tests.common import MockEntity
class MockVacuum(MockEntity, StateVacuumEntity):
"""Mock vacuum class."""
_attr_supported_features = (
VacuumEntityFeature.PAUSE
| VacuumEntityFeature.STOP
| VacuumEntityFeature.RETURN_HOME
| VacuumEntityFeature.FAN_SPEED
| VacuumEntityFeature.BATTERY
| VacuumEntityFeature.CLEAN_SPOT
| VacuumEntityFeature.MAP
| VacuumEntityFeature.STATE
| VacuumEntityFeature.START
)
_attr_battery_level = 99
_attr_fan_speed_list = ["slow", "fast"]
def __init__(self, **values: Any) -> None:
"""Initialize a mock vacuum entity."""
super().__init__(**values)
self._attr_state = STATE_DOCKED
self._attr_fan_speed = "slow"
def stop(self, **kwargs: Any) -> None:
"""Stop cleaning."""
self._attr_state = STATE_IDLE
def return_to_base(self, **kwargs: Any) -> None:
"""Return to base."""
self._attr_state = STATE_RETURNING
def clean_spot(self, **kwargs: Any) -> None:
"""Clean a spot."""
self._attr_state = STATE_CLEANING
def set_fan_speed(self, fan_speed: str, **kwargs: Any) -> None:
"""Set the fan speed."""
self._attr_fan_speed = fan_speed
def start(self) -> None:
"""Start cleaning."""
self._attr_state = STATE_CLEANING
def pause(self) -> None:
"""Pause cleaning."""
self._attr_state = STATE_PAUSED
async def help_async_setup_entry_init(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Set up test config entry."""
Ensure config entries are not unloaded while their platforms are setting up (#118767) * Report non-awaited/non-locked config entry platform forwards Its currently possible for config entries to be reloaded while their platforms are being forwarded if platform forwards are not awaited or done after the config entry is setup since the lock will not be held in this case. In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards we advised to await platform forwards to ensure this does not happen, however for sleeping devices and late discovered devices, platform forwards may happen later. If config platform forwards are happening during setup, they should be awaited If config entry platform forwards are not happening during setup, instead async_late_forward_entry_setups should be used which will hold the lock to prevent the config entry from being unloaded while its platforms are being setup * Report non-awaited/non-locked config entry platform forwards Its currently possible for config entries to be reloaded while their platforms are being forwarded if platform forwards are not awaited or done after the config entry is setup since the lock will not be held in this case. In https://developers.home-assistant.io/blog/2022/07/08/config_entry_forwards we advised to await platform forwards to ensure this does not happen, however for sleeping devices and late discovered devices, platform forwards may happen later. If config platform forwards are happening during setup, they should be awaited If config entry platform forwards are not happening during setup, instead async_late_forward_entry_setups should be used which will hold the lock to prevent the config entry from being unloaded while its platforms are being setup * run with error on to find them * cert_exp, hold lock * cert_exp, hold lock * shelly async_late_forward_entry_setups * compact * compact * found another * patch up mobileapp * patch up hue tests * patch up smartthings * fix mqtt * fix esphome * zwave_js * mqtt * rework * fixes * fix mocking * fix mocking * do not call async_forward_entry_setup directly * docstrings * docstrings * docstrings * add comments * doc strings * fixed all in core, turn off strict * coverage * coverage * missing * coverage
2024-06-05 01:34:39 +00:00
await hass.config_entries.async_forward_entry_setups(config_entry, [DOMAIN])
return True
async def help_async_unload_entry(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Unload test config emntry."""
return await hass.config_entries.async_unload_platforms(
config_entry, [Platform.VACUUM]
)