mirror of
https://github.com/fmtr/haco.git
synced 2026-10-26 02:48:54 -04:00
Migrate Fan control from v0
This commit is contained in:
parent
c8d6d66641
commit
f1b1cac91a
2 changed files with 101 additions and 57 deletions
101
haco/fan.py
Normal file
101
haco/fan.py
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
from dataclasses import dataclass
|
||||
|
||||
from haco.capabilities import Capability
|
||||
from haco.control import Control
|
||||
from haco.utils import ON, OFF, ConvertersNumeric, ConvertersString
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class Fan(Control):
|
||||
"""
|
||||
|
||||
A fan control with support for speed percentage, preset modes, and oscillation.
|
||||
|
||||
"""
|
||||
DATA = dict(
|
||||
platform="fan"
|
||||
)
|
||||
|
||||
speed_range_min: int | None = None
|
||||
speed_range_max: int | None = None
|
||||
preset_modes: list[str] | None = None
|
||||
payload_oscillation_on: str = ON
|
||||
payload_oscillation_off: str = OFF
|
||||
|
||||
def command(self, value: bool) -> bool:
|
||||
"""
|
||||
|
||||
Handle on/off commands for the fan.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def state(self, value: bool | None) -> bool:
|
||||
"""
|
||||
|
||||
Return the current on/off state of the fan.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def preset_mode_command(self, value: str) -> str:
|
||||
"""
|
||||
|
||||
Handle preset mode commands.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def preset_mode_state(self, value: str | None) -> str:
|
||||
"""
|
||||
|
||||
Return the current preset mode.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def percentage_command(self, value: int) -> int:
|
||||
"""
|
||||
|
||||
Handle percentage (speed) commands.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def percentage_state(self, value: int | None) -> int:
|
||||
"""
|
||||
|
||||
Return the current speed percentage.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def oscillation_command(self, value: bool) -> bool:
|
||||
"""
|
||||
|
||||
Handle oscillation toggle commands.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
def oscillation_state(self, value: bool | None) -> bool:
|
||||
"""
|
||||
|
||||
Return the current oscillation state.
|
||||
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def get_capabilities(cls) -> list[Capability]:
|
||||
"""
|
||||
|
||||
Get the capabilities for the fan control.
|
||||
|
||||
"""
|
||||
return [
|
||||
Capability(name=None),
|
||||
Capability(name="preset_mode", converters=ConvertersString),
|
||||
Capability(name="percentage", converters=ConvertersNumeric),
|
||||
Capability(name="oscillation"),
|
||||
]
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
from haco.control import Control
|
||||
from haco.schema import Schema, Capability, AnnounceTopic, Tasmota, ControlType, HomeAssistant
|
||||
from haco.tools import get_range_pair, invert_bool
|
||||
|
||||
from haco import constants
|
||||
|
||||
|
||||
class Fan(Control):
|
||||
NAME = 'fan'
|
||||
|
||||
@classmethod
|
||||
def get_schema(cls):
|
||||
schema = Schema(
|
||||
capabilities=[
|
||||
Capability(
|
||||
ha=HomeAssistant(announce_data=AnnounceTopic(key='{io_ha}_topic')),
|
||||
tasmota=Tasmota(announce_data=AnnounceTopic(key='{io_ha}_topic'))
|
||||
),
|
||||
Capability(
|
||||
name='preset_mode',
|
||||
type=str
|
||||
),
|
||||
Capability(
|
||||
name='percentage',
|
||||
type=int
|
||||
),
|
||||
Capability(
|
||||
name='oscillation',
|
||||
type=ControlType
|
||||
)
|
||||
|
||||
]
|
||||
)
|
||||
return schema
|
||||
|
||||
def __init__(self, name=None, preset_modes=None, icon=None, speed_range=None):
|
||||
self.temperature_unit = speed_range
|
||||
self.speed_min, self.speed_max = get_range_pair(speed_range)
|
||||
self.preset_modes = preset_modes
|
||||
|
||||
super().__init__(name, icon=icon)
|
||||
|
||||
def get_config_ha_ex(self):
|
||||
data = {
|
||||
'speed_range_min': self.speed_min,
|
||||
'speed_range_max': self.speed_max,
|
||||
'preset_modes': self.preset_modes,
|
||||
'payload_oscillation_on': constants.ON,
|
||||
'payload_oscillation_off': constants.OFF,
|
||||
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
@property
|
||||
def control_type(self):
|
||||
return invert_bool
|
||||
Loading…
Add table
Add a link
Reference in a new issue