Alexander Somov d754ea7e22
Add new Rabbit Air integration (#66130)
* Add new Rabbit Air integration

* Remove py.typed file

It is not needed and was just accidentally added to the commit.

* Enable strict type checking for rabbitair component

Keeping the code fully type hinted is a good idea.

* Add missing type annotations

* Remove translation file

* Prevent data to be added to hass.data if refresh fails

* Reload the config entry when the options change

* Add missing type parameters for generics

* Avoid using assert in production code

* Move zeroconf to optional dependencies

* Remove unnecessary logging

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Remove unused keys from the manifest

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Replace property with attr

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Allow to return None for power

The type of the is_on property now allows this.

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Remove unnecessary method call

Co-authored-by: Franck Nijhof <frenck@frenck.nl>

* Update the python library

The new version properly re-exports names from the package root.

* Remove options flow

Scan interval should not be part of integration configuration. This was
the only option, so the options flow can be fully removed.

* Replace properties with attrs

* Remove multiline ternary operator

* Use NamedTuple for hass.data

* Remove unused logger variable

* Move async_setup_entry up in the file

* Adjust debouncer settings to use request_refresh

* Prevent status updates during the cooldown period

* Move device polling code to the update coordinator

* Fix the problem with the switch jumping back and forth

The UI seems to have a timeout of 2 seconds somewhere, which is just a
little bit less than what we normally need to get an updated state. So
the power switch would jump to its previous state and then immediately
return to the new state.

* Update the python library

The new version fixes errors when multiple requests are executed
simultaneously.

* Fix incorrect check for pending call in debouncer

This caused the polling to stop.

* Fix tests

* Update .coveragerc to exclude new file.
* Remove test for Options Flow.

* Update the existing entry when device access details change

* Add Zeroconf discovery step

* Fix tests

The ZeroconfServiceInfo constructor now requires one more argument.

* Fix typing for CoordinatorEntity

* Fix signature of async_turn_on

* Fix depreciation warnings

* Fix manifest formatting

* Fix warning about debouncer typing

relates to 5ae5ae5392729b4c94a8004bd02e147d60227341

* Wait for config entry platform forwards

* Apply some of the suggested changes

* Do not put the MAC address in the title. Use a fixed title instead.
* Do not format the MAC to use as a unique ID.
* Do not catch exceptions in _async_update_data().
* Remove unused _entry field in the base entity class.
* Use the standard attribute self._attr_is_on to keep the power state.

* Store the MAC in the config entry data

* Change the order of except clauses

OSError is an ancestor class of TimeoutError, so TimeoutError should be
handled first

* Fix depreciation warnings

* Fix tests

The ZeroconfServiceInfo constructor arguments have changed.

* Fix DeviceInfo import

* Rename the method to make it clearer what it does

* Apply suggestions from code review

* Fix tests

* Change speed/mode logic to use is_on from the base class

* A zero value is more appropriate than None

since None means "unknown", but we actually know that the speed is zero
when the power is off.

---------

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
Co-authored-by: Erik Montnemery <erik@montnemery.com>
2024-01-05 16:34:28 +01:00

148 lines
4.8 KiB
Python

"""Support for Rabbit Air fan entity."""
from __future__ import annotations
from typing import Any
from rabbitair import Mode, Model, Speed
from homeassistant.components.fan import FanEntity, FanEntityFeature
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.percentage import (
ordered_list_item_to_percentage,
percentage_to_ordered_list_item,
)
from .const import DOMAIN
from .coordinator import RabbitAirDataUpdateCoordinator
from .entity import RabbitAirBaseEntity
SPEED_LIST = [
Speed.Silent,
Speed.Low,
Speed.Medium,
Speed.High,
Speed.Turbo,
]
PRESET_MODE_AUTO = "Auto"
PRESET_MODE_MANUAL = "Manual"
PRESET_MODE_POLLEN = "Pollen"
PRESET_MODES = {
PRESET_MODE_AUTO: Mode.Auto,
PRESET_MODE_MANUAL: Mode.Manual,
PRESET_MODE_POLLEN: Mode.Pollen,
}
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up a config entry."""
coordinator: RabbitAirDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities([RabbitAirFanEntity(coordinator, entry)])
class RabbitAirFanEntity(RabbitAirBaseEntity, FanEntity):
"""Fan control functions of the Rabbit Air air purifier."""
_attr_supported_features = FanEntityFeature.PRESET_MODE | FanEntityFeature.SET_SPEED
def __init__(
self,
coordinator: RabbitAirDataUpdateCoordinator,
entry: ConfigEntry,
) -> None:
"""Initialize the entity."""
super().__init__(coordinator, entry)
if self._is_model(Model.MinusA2):
self._attr_preset_modes = list(PRESET_MODES)
elif self._is_model(Model.A3):
# A3 does not support Pollen mode
self._attr_preset_modes = [
k for k in PRESET_MODES if k != PRESET_MODE_POLLEN
]
self._attr_speed_count = len(SPEED_LIST)
self._get_state_from_coordinator_data()
@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self._get_state_from_coordinator_data()
super()._handle_coordinator_update()
def _get_state_from_coordinator_data(self) -> None:
"""Populate the entity fields with values from the coordinator data."""
data = self.coordinator.data
# Speed as a percentage
if not data.power:
self._attr_percentage = 0
elif data.speed is None:
self._attr_percentage = None
elif data.speed is Speed.SuperSilent:
self._attr_percentage = 1
else:
self._attr_percentage = ordered_list_item_to_percentage(
SPEED_LIST, data.speed
)
# Preset mode
if not data.power or data.mode is None:
self._attr_preset_mode = None
else:
# Get key by value in dictionary
self._attr_preset_mode = next(
k for k, v in PRESET_MODES.items() if v == data.mode
)
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set new preset mode."""
await self._set_state(power=True, mode=PRESET_MODES[preset_mode])
self._attr_preset_mode = preset_mode
self.async_write_ha_state()
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed of the fan, as a percentage."""
if percentage > 0:
value = percentage_to_ordered_list_item(SPEED_LIST, percentage)
await self._set_state(power=True, speed=value)
self._attr_percentage = percentage
else:
await self._set_state(power=False)
self._attr_percentage = 0
self._attr_preset_mode = None
self.async_write_ha_state()
async def async_turn_on(
self,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs: Any,
) -> None:
"""Turn on the fan."""
mode_value: Mode | None = None
if preset_mode is not None:
mode_value = PRESET_MODES[preset_mode]
speed_value: Speed | None = None
if percentage is not None:
speed_value = percentage_to_ordered_list_item(SPEED_LIST, percentage)
await self._set_state(power=True, mode=mode_value, speed=speed_value)
if percentage is not None:
self._attr_percentage = percentage
if preset_mode is not None:
self._attr_preset_mode = preset_mode
self.async_write_ha_state()
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the fan off."""
await self._set_state(power=False)
self._attr_percentage = 0
self._attr_preset_mode = None
self.async_write_ha_state()