1
mirror of https://github.com/home-assistant/core synced 2024-08-02 23:40:32 +02:00
ha-core/homeassistant/components/tradfri/fan.py
Patrik Lindgren 9d404b749a
Implement coordinator class for Tradfri integration (#64166)
* Initial commit coordinator

* More coordinator implementation

* More coordinator implementation

* Allow integration reload

* Move API calls to try/catch block

* Move back fixture

* Remove coordinator test file

* Ensure unchanged file

* Ensure unchanged conftest.py file

* Remove coordinator key check

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Import RequestError

* Move async_setup_platforms to end of setup_entry

* Remove centralised handling of device data and device controllers

* Remove platform_type argument

* Remove exception

* Remove the correct exception

* Refactor coordinator error handling

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Remove platform type from base class

* Remove timeout context manager

* Refactor exception callback

* Simplify starting device observation

* Update test

* Move observe start into update method

* Remove await self.coordinator.async_request_refresh()

* Refactor cover.py

* Uncomment const.py

* Add back extra_state_attributes

* Update homeassistant/components/tradfri/coordinator.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Refactor switch platform

* Expose switch state

* Refactor sensor platform

* Put back accidentally deleted code

* Add set_hub_available

* Apply suggestions from code review

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix tests for fan platform

* Update homeassistant/components/tradfri/base_class.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Update homeassistant/components/tradfri/base_class.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* Fix non-working tests

* Refresh sensor state

* Remove commented line

* Add group coordinator

* Add groups during setup

* Refactor light platform

* Fix tests

* Move outside of try...except

* Remove error handler

* Remove unneeded methods

* Update sensor

* Update .coveragerc

* Move signal

* Add signals for groups

* Fix signal

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2022-01-27 11:12:52 +01:00

185 lines
5.2 KiB
Python

"""Represent an air purifier."""
from __future__ import annotations
from collections.abc import Callable
from typing import Any, cast
from pytradfri.command import Command
from homeassistant.components.fan import (
SUPPORT_PRESET_MODE,
SUPPORT_SET_SPEED,
FanEntity,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .base_class import TradfriBaseEntity
from .const import (
ATTR_AUTO,
ATTR_MAX_FAN_STEPS,
CONF_GATEWAY_ID,
COORDINATOR,
COORDINATOR_LIST,
DOMAIN,
KEY_API,
)
from .coordinator import TradfriDeviceDataUpdateCoordinator
def _from_fan_percentage(percentage: int) -> int:
"""Convert percent to a value that the Tradfri API understands."""
return round(max(2, (percentage / 100 * ATTR_MAX_FAN_STEPS) + 1))
def _from_fan_speed(fan_speed: int) -> int:
"""Convert the Tradfri API fan speed to a percentage value."""
return max(round((fan_speed - 1) / ATTR_MAX_FAN_STEPS * 100), 0)
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Load Tradfri switches based on a config entry."""
gateway_id = config_entry.data[CONF_GATEWAY_ID]
coordinator_data = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]
api = coordinator_data[KEY_API]
async_add_entities(
TradfriAirPurifierFan(
device_coordinator,
api,
gateway_id,
)
for device_coordinator in coordinator_data[COORDINATOR_LIST]
if device_coordinator.device.has_air_purifier_control
)
class TradfriAirPurifierFan(TradfriBaseEntity, FanEntity):
"""The platform class required by Home Assistant."""
def __init__(
self,
device_coordinator: TradfriDeviceDataUpdateCoordinator,
api: Callable[[Command | list[Command]], Any],
gateway_id: str,
) -> None:
"""Initialize a switch."""
super().__init__(
device_coordinator=device_coordinator,
api=api,
gateway_id=gateway_id,
)
self._device_control = self._device.air_purifier_control
self._device_data = self._device_control.air_purifiers[0]
def _refresh(self) -> None:
"""Refresh the device."""
self._device_data = self.coordinator.data.air_purifier_control.air_purifiers[0]
@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_PRESET_MODE + SUPPORT_SET_SPEED
@property
def speed_count(self) -> int:
"""
Return the number of speeds the fan supports.
These are the steps:
0 = Off
1 = Preset: Auto mode
2 = Min
... with step size 1
50 = Max
"""
return ATTR_MAX_FAN_STEPS
@property
def is_on(self) -> bool:
"""Return true if switch is on."""
if not self._device_data:
return False
return cast(bool, self._device_data.state)
@property
def preset_modes(self) -> list[str] | None:
"""Return a list of available preset modes."""
return [ATTR_AUTO]
@property
def percentage(self) -> int | None:
"""Return the current speed percentage."""
if not self._device_data:
return None
if self._device_data.fan_speed:
return _from_fan_speed(self._device_data.fan_speed)
return None
@property
def preset_mode(self) -> str | None:
"""Return the current preset mode."""
if not self._device_data:
return None
if self._device_data.is_auto_mode:
return ATTR_AUTO
return None
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode of the fan."""
if not self._device_control:
return
if not preset_mode == ATTR_AUTO:
raise ValueError("Preset must be 'Auto'.")
await self._api(self._device_control.turn_on_auto_mode())
async def async_turn_on(
self,
speed: str | None = None,
percentage: int | None = None,
preset_mode: str | None = None,
**kwargs: Any,
) -> None:
"""Turn on the fan. Auto-mode if no argument is given."""
if not self._device_control:
return
if percentage is not None:
await self.async_set_percentage(percentage)
return
preset_mode = preset_mode or ATTR_AUTO
await self.async_set_preset_mode(preset_mode)
async def async_set_percentage(self, percentage: int) -> None:
"""Set the speed percentage of the fan."""
if not self._device_control:
return
if percentage == 0:
await self.async_turn_off()
return
await self._api(
self._device_control.set_fan_speed(_from_fan_percentage(percentage))
)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the fan."""
if not self._device_control:
return
await self._api(self._device_control.turn_off())