2019-04-03 15:40:03 +00:00
|
|
|
"""Demo fan platform that has a fake fan."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2021-03-17 22:43:55 +00:00
|
|
|
from __future__ import annotations
|
2021-01-27 23:44:36 +00:00
|
|
|
|
2022-06-22 16:43:41 +00:00
|
|
|
from typing import Any
|
|
|
|
|
2022-04-01 18:53:38 +00:00
|
|
|
from homeassistant.components.fan import FanEntity, FanEntityFeature
|
2022-01-03 14:10:53 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2021-01-27 23:44:36 +00:00
|
|
|
|
|
|
|
PRESET_MODE_AUTO = "auto"
|
|
|
|
PRESET_MODE_SMART = "smart"
|
2021-02-06 11:48:18 +00:00
|
|
|
PRESET_MODE_SLEEP = "sleep"
|
|
|
|
PRESET_MODE_ON = "on"
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2022-04-01 18:53:38 +00:00
|
|
|
FULL_SUPPORT = (
|
2024-07-19 09:35:24 +00:00
|
|
|
FanEntityFeature.SET_SPEED
|
|
|
|
| FanEntityFeature.OSCILLATE
|
|
|
|
| FanEntityFeature.DIRECTION
|
|
|
|
| FanEntityFeature.TURN_OFF
|
|
|
|
| FanEntityFeature.TURN_ON
|
|
|
|
)
|
|
|
|
LIMITED_SUPPORT = (
|
|
|
|
FanEntityFeature.SET_SPEED | FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON
|
2022-04-01 18:53:38 +00:00
|
|
|
)
|
2016-08-27 20:53:12 +00:00
|
|
|
|
|
|
|
|
2023-10-15 21:12:41 +00:00
|
|
|
async def async_setup_entry(
|
2022-01-03 14:10:53 +00:00
|
|
|
hass: HomeAssistant,
|
2023-10-15 21:12:41 +00:00
|
|
|
config_entry: ConfigEntry,
|
2022-01-03 14:10:53 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2023-10-15 21:12:41 +00:00
|
|
|
"""Set up the Demo config entry."""
|
2019-11-13 15:37:31 +00:00
|
|
|
async_add_entities(
|
2019-07-31 19:25:30 +00:00
|
|
|
[
|
2021-12-04 05:57:46 +00:00
|
|
|
DemoPercentageFan(
|
2021-01-27 23:44:36 +00:00
|
|
|
hass,
|
|
|
|
"fan1",
|
|
|
|
"Living Room Fan",
|
|
|
|
FULL_SUPPORT,
|
|
|
|
[
|
|
|
|
PRESET_MODE_AUTO,
|
|
|
|
PRESET_MODE_SMART,
|
2021-02-06 11:48:18 +00:00
|
|
|
PRESET_MODE_SLEEP,
|
|
|
|
PRESET_MODE_ON,
|
2021-01-27 23:44:36 +00:00
|
|
|
],
|
|
|
|
),
|
2021-12-04 05:57:46 +00:00
|
|
|
DemoPercentageFan(
|
2021-01-27 23:44:36 +00:00
|
|
|
hass,
|
|
|
|
"fan2",
|
|
|
|
"Ceiling Fan",
|
|
|
|
LIMITED_SUPPORT,
|
|
|
|
None,
|
|
|
|
),
|
|
|
|
AsyncDemoPercentageFan(
|
|
|
|
hass,
|
|
|
|
"fan3",
|
|
|
|
"Percentage Full Fan",
|
|
|
|
FULL_SUPPORT,
|
2021-02-06 11:48:18 +00:00
|
|
|
[
|
|
|
|
PRESET_MODE_AUTO,
|
|
|
|
PRESET_MODE_SMART,
|
|
|
|
PRESET_MODE_SLEEP,
|
|
|
|
PRESET_MODE_ON,
|
|
|
|
],
|
2021-01-27 23:44:36 +00:00
|
|
|
),
|
|
|
|
DemoPercentageFan(
|
|
|
|
hass,
|
|
|
|
"fan4",
|
|
|
|
"Percentage Limited Fan",
|
|
|
|
LIMITED_SUPPORT,
|
2021-02-06 11:48:18 +00:00
|
|
|
[
|
|
|
|
PRESET_MODE_AUTO,
|
|
|
|
PRESET_MODE_SMART,
|
|
|
|
PRESET_MODE_SLEEP,
|
|
|
|
PRESET_MODE_ON,
|
|
|
|
],
|
2021-01-27 23:44:36 +00:00
|
|
|
),
|
|
|
|
AsyncDemoPercentageFan(
|
|
|
|
hass,
|
|
|
|
"fan5",
|
|
|
|
"Preset Only Limited Fan",
|
2024-07-19 09:35:24 +00:00
|
|
|
FanEntityFeature.PRESET_MODE
|
|
|
|
| FanEntityFeature.TURN_OFF
|
|
|
|
| FanEntityFeature.TURN_ON,
|
2021-02-06 11:48:18 +00:00
|
|
|
[
|
|
|
|
PRESET_MODE_AUTO,
|
|
|
|
PRESET_MODE_SMART,
|
|
|
|
PRESET_MODE_SLEEP,
|
|
|
|
PRESET_MODE_ON,
|
|
|
|
],
|
2021-01-27 23:44:36 +00:00
|
|
|
),
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|
|
|
|
)
|
2016-08-27 20:53:12 +00:00
|
|
|
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
class BaseDemoFan(FanEntity):
|
|
|
|
"""A demonstration fan component that uses legacy fan speeds."""
|
2016-08-27 20:53:12 +00:00
|
|
|
|
2022-08-28 22:51:10 +00:00
|
|
|
_attr_should_poll = False
|
2024-02-04 22:58:44 +00:00
|
|
|
_attr_translation_key = "demo"
|
2024-07-19 09:35:24 +00:00
|
|
|
_enable_turn_on_off_backwards_compatibility = False
|
2022-08-28 22:51:10 +00:00
|
|
|
|
2020-09-04 16:23:53 +00:00
|
|
|
def __init__(
|
2021-01-27 23:44:36 +00:00
|
|
|
self,
|
2022-08-28 22:51:10 +00:00
|
|
|
hass: HomeAssistant,
|
2021-01-27 23:44:36 +00:00
|
|
|
unique_id: str,
|
|
|
|
name: str,
|
2022-11-17 08:39:46 +00:00
|
|
|
supported_features: FanEntityFeature,
|
2021-03-17 22:43:55 +00:00
|
|
|
preset_modes: list[str] | None,
|
2020-09-04 16:23:53 +00:00
|
|
|
) -> None:
|
2016-08-27 20:53:12 +00:00
|
|
|
"""Initialize the entity."""
|
|
|
|
self.hass = hass
|
2020-09-04 16:23:53 +00:00
|
|
|
self._unique_id = unique_id
|
2022-11-17 08:39:46 +00:00
|
|
|
self._attr_supported_features = supported_features
|
2022-02-10 09:05:58 +00:00
|
|
|
self._percentage: int | None = None
|
2021-01-27 23:44:36 +00:00
|
|
|
self._preset_modes = preset_modes
|
2022-02-10 09:05:58 +00:00
|
|
|
self._preset_mode: str | None = None
|
|
|
|
self._oscillating: bool | None = None
|
|
|
|
self._direction: str | None = None
|
2022-08-28 22:51:10 +00:00
|
|
|
self._attr_name = name
|
2022-04-01 18:53:38 +00:00
|
|
|
if supported_features & FanEntityFeature.OSCILLATE:
|
2020-05-06 07:58:07 +00:00
|
|
|
self._oscillating = False
|
2022-04-01 18:53:38 +00:00
|
|
|
if supported_features & FanEntityFeature.DIRECTION:
|
2019-09-02 04:42:57 +00:00
|
|
|
self._direction = "forward"
|
2017-06-12 05:12:56 +00:00
|
|
|
|
2020-09-04 16:23:53 +00:00
|
|
|
@property
|
2022-06-23 14:34:40 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-09-04 16:23:53 +00:00
|
|
|
"""Return the unique id."""
|
|
|
|
return self._unique_id
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
@property
|
2022-02-10 09:05:58 +00:00
|
|
|
def current_direction(self) -> str | None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Fan direction."""
|
|
|
|
return self._direction
|
|
|
|
|
|
|
|
@property
|
2022-02-10 09:05:58 +00:00
|
|
|
def oscillating(self) -> bool | None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Oscillating."""
|
|
|
|
return self._oscillating
|
|
|
|
|
|
|
|
|
|
|
|
class DemoPercentageFan(BaseDemoFan, FanEntity):
|
|
|
|
"""A demonstration fan component that uses percentages."""
|
|
|
|
|
2017-01-14 06:08:13 +00:00
|
|
|
@property
|
2021-03-17 22:43:55 +00:00
|
|
|
def percentage(self) -> int | None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Return the current speed."""
|
|
|
|
return self._percentage
|
|
|
|
|
2021-02-19 07:05:09 +00:00
|
|
|
@property
|
2021-02-20 05:57:21 +00:00
|
|
|
def speed_count(self) -> int:
|
2021-02-19 07:05:09 +00:00
|
|
|
"""Return the number of speeds the fan supports."""
|
|
|
|
return 3
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
def set_percentage(self, percentage: int) -> None:
|
|
|
|
"""Set the speed of the fan, as a percentage."""
|
|
|
|
self._percentage = percentage
|
|
|
|
self._preset_mode = None
|
|
|
|
self.schedule_update_ha_state()
|
2017-01-14 06:08:13 +00:00
|
|
|
|
2020-05-06 07:58:07 +00:00
|
|
|
@property
|
2021-03-17 22:43:55 +00:00
|
|
|
def preset_mode(self) -> str | None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Return the current preset mode, e.g., auto, smart, interval, favorite."""
|
|
|
|
return self._preset_mode
|
2020-05-06 07:58:07 +00:00
|
|
|
|
2016-08-27 20:53:12 +00:00
|
|
|
@property
|
2021-03-17 22:43:55 +00:00
|
|
|
def preset_modes(self) -> list[str] | None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Return a list of available preset modes."""
|
|
|
|
return self._preset_modes
|
|
|
|
|
|
|
|
def set_preset_mode(self, preset_mode: str) -> None:
|
|
|
|
"""Set new preset mode."""
|
2023-11-29 12:56:51 +00:00
|
|
|
self._preset_mode = preset_mode
|
|
|
|
self._percentage = None
|
|
|
|
self.schedule_update_ha_state()
|
2021-01-27 23:44:36 +00:00
|
|
|
|
|
|
|
def turn_on(
|
|
|
|
self,
|
2022-06-22 16:43:41 +00:00
|
|
|
percentage: int | None = None,
|
|
|
|
preset_mode: str | None = None,
|
|
|
|
**kwargs: Any,
|
2021-01-27 23:44:36 +00:00
|
|
|
) -> None:
|
|
|
|
"""Turn on the entity."""
|
|
|
|
if preset_mode:
|
|
|
|
self.set_preset_mode(preset_mode)
|
|
|
|
return
|
|
|
|
|
|
|
|
if percentage is None:
|
|
|
|
percentage = 67
|
|
|
|
|
|
|
|
self.set_percentage(percentage)
|
|
|
|
|
2022-06-23 10:01:05 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Turn off the entity."""
|
|
|
|
self.set_percentage(0)
|
|
|
|
|
2021-12-04 05:57:46 +00:00
|
|
|
def set_direction(self, direction: str) -> None:
|
|
|
|
"""Set the direction of the fan."""
|
|
|
|
self._direction = direction
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
|
|
|
def oscillate(self, oscillating: bool) -> None:
|
|
|
|
"""Set oscillation."""
|
|
|
|
self._oscillating = oscillating
|
|
|
|
self.schedule_update_ha_state()
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
|
|
|
|
class AsyncDemoPercentageFan(BaseDemoFan, FanEntity):
|
|
|
|
"""An async demonstration fan component that uses percentages."""
|
|
|
|
|
|
|
|
@property
|
2021-03-17 22:43:55 +00:00
|
|
|
def percentage(self) -> int | None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Return the current speed."""
|
|
|
|
return self._percentage
|
|
|
|
|
2021-02-19 07:05:09 +00:00
|
|
|
@property
|
2021-02-20 05:57:21 +00:00
|
|
|
def speed_count(self) -> int:
|
2021-02-19 07:05:09 +00:00
|
|
|
"""Return the number of speeds the fan supports."""
|
|
|
|
return 3
|
|
|
|
|
2021-01-27 23:44:36 +00:00
|
|
|
async def async_set_percentage(self, percentage: int) -> None:
|
|
|
|
"""Set the speed of the fan, as a percentage."""
|
|
|
|
self._percentage = percentage
|
|
|
|
self._preset_mode = None
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
@property
|
2021-03-17 22:43:55 +00:00
|
|
|
def preset_mode(self) -> str | None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Return the current preset mode, e.g., auto, smart, interval, favorite."""
|
|
|
|
return self._preset_mode
|
|
|
|
|
|
|
|
@property
|
2021-03-17 22:43:55 +00:00
|
|
|
def preset_modes(self) -> list[str] | None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Return a list of available preset modes."""
|
|
|
|
return self._preset_modes
|
|
|
|
|
|
|
|
async def async_set_preset_mode(self, preset_mode: str) -> None:
|
|
|
|
"""Set new preset mode."""
|
|
|
|
self._preset_mode = preset_mode
|
|
|
|
self._percentage = None
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_turn_on(
|
|
|
|
self,
|
2022-06-22 16:43:41 +00:00
|
|
|
percentage: int | None = None,
|
|
|
|
preset_mode: str | None = None,
|
|
|
|
**kwargs: Any,
|
2021-01-27 23:44:36 +00:00
|
|
|
) -> None:
|
|
|
|
"""Turn on the entity."""
|
|
|
|
if preset_mode:
|
|
|
|
await self.async_set_preset_mode(preset_mode)
|
|
|
|
return
|
|
|
|
|
|
|
|
if percentage is None:
|
|
|
|
percentage = 67
|
|
|
|
|
|
|
|
await self.async_set_percentage(percentage)
|
|
|
|
|
2022-06-23 10:01:05 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2021-01-27 23:44:36 +00:00
|
|
|
"""Turn off the entity."""
|
|
|
|
await self.async_oscillate(False)
|
|
|
|
await self.async_set_percentage(0)
|
|
|
|
|
|
|
|
async def async_set_direction(self, direction: str) -> None:
|
|
|
|
"""Set the direction of the fan."""
|
|
|
|
self._direction = direction
|
|
|
|
self.async_write_ha_state()
|
|
|
|
|
|
|
|
async def async_oscillate(self, oscillating: bool) -> None:
|
|
|
|
"""Set oscillation."""
|
|
|
|
self._oscillating = oscillating
|
|
|
|
self.async_write_ha_state()
|