mirror of
https://github.com/python-kasa/python-kasa.git
synced 2024-12-23 19:53:34 +00:00
40e40522f9
Picked this up while updating the [Fan platform PR](https://github.com/home-assistant/core/pull/116605) for HA. The smart fan module was not correctly deriving from the common interface and the speed_level is reported as >0 when off.
82 lines
2.5 KiB
Python
82 lines
2.5 KiB
Python
"""Implementation of fan_control module."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from ...feature import Feature
|
|
from ...interfaces.fan import Fan as FanInterface
|
|
from ..smartmodule import SmartModule
|
|
|
|
if TYPE_CHECKING:
|
|
from ..smartdevice import SmartDevice
|
|
|
|
|
|
class Fan(SmartModule, FanInterface):
|
|
"""Implementation of fan_control module."""
|
|
|
|
REQUIRED_COMPONENT = "fan_control"
|
|
|
|
def __init__(self, device: SmartDevice, module: str):
|
|
super().__init__(device, module)
|
|
|
|
self._add_feature(
|
|
Feature(
|
|
device,
|
|
id="fan_speed_level",
|
|
name="Fan speed level",
|
|
container=self,
|
|
attribute_getter="fan_speed_level",
|
|
attribute_setter="set_fan_speed_level",
|
|
icon="mdi:fan",
|
|
type=Feature.Type.Number,
|
|
minimum_value=0,
|
|
maximum_value=4,
|
|
category=Feature.Category.Primary,
|
|
)
|
|
)
|
|
self._add_feature(
|
|
Feature(
|
|
device,
|
|
id="fan_sleep_mode",
|
|
name="Fan sleep mode",
|
|
container=self,
|
|
attribute_getter="sleep_mode",
|
|
attribute_setter="set_sleep_mode",
|
|
icon="mdi:sleep",
|
|
type=Feature.Type.Switch,
|
|
)
|
|
)
|
|
|
|
def query(self) -> dict:
|
|
"""Query to execute during the update cycle."""
|
|
return {}
|
|
|
|
@property
|
|
def fan_speed_level(self) -> int:
|
|
"""Return fan speed level."""
|
|
return 0 if self.data["device_on"] is False else self.data["fan_speed_level"]
|
|
|
|
async def set_fan_speed_level(self, level: int):
|
|
"""Set fan speed level, 0 for off, 1-4 for on."""
|
|
if level < 0 or level > 4:
|
|
raise ValueError("Invalid level, should be in range 0-4.")
|
|
if level == 0:
|
|
return await self.call("set_device_info", {"device_on": False})
|
|
return await self.call(
|
|
"set_device_info", {"device_on": True, "fan_speed_level": level}
|
|
)
|
|
|
|
@property
|
|
def sleep_mode(self) -> bool:
|
|
"""Return sleep mode status."""
|
|
return self.data["fan_sleep_mode_on"]
|
|
|
|
async def set_sleep_mode(self, on: bool):
|
|
"""Set sleep mode."""
|
|
return await self.call("set_device_info", {"fan_sleep_mode_on": on})
|
|
|
|
async def _check_supported(self):
|
|
"""Is the module available on this device."""
|
|
return "fan_speed_level" in self.data
|