Skip to content

Commit

Permalink
Fix fan speed range for A5
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed Dec 10, 2023
1 parent b131b82 commit f61f6de
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
7 changes: 7 additions & 0 deletions custom_components/airmx/airwater/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
class AirWaterFeature(IntFlag):
HEATER = 1
ANION = 2
FAN_SPEED_PERCENTAGE = 4
FAN_SPEED_STEPS = 8


class AirWaterModel(StrEnum):
Expand All @@ -29,6 +31,11 @@ def features(self) -> int:
if self not in [self.A5]:
features |= AirWaterFeature.ANION

if self in [self.A5]:
features |= AirWaterFeature.FAN_SPEED_STEPS
else:
features |= AirWaterFeature.FAN_SPEED_PERCENTAGE

return features


Expand Down
61 changes: 51 additions & 10 deletions custom_components/airmx/number.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,75 @@
from homeassistant.components.number import NumberEntity
from dataclasses import dataclass

from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import PERCENTAGE
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .airwater.const import AirWaterFeature
from .airwater.device import AirWaterDevice
from .const import ATTR_FAN_SPEED, DEVICES, DOMAIN
from .entity import AirWaterEntity


@dataclass
class AirWaterFanSpeedDescriptionMixin:
feature: int


@dataclass
class AirWaterFanSpeedDescription(NumberEntityDescription, AirWaterFanSpeedDescriptionMixin):
...


FAN_SPEED_TYPES = (
AirWaterFanSpeedDescription(
key=ATTR_FAN_SPEED,
translation_key=ATTR_FAN_SPEED,
icon="mdi:fan",
feature=AirWaterFeature.FAN_SPEED_PERCENTAGE,
native_unit_of_measurement=PERCENTAGE,
native_min_value=0,
native_max_value=100,
native_step=1,
),
AirWaterFanSpeedDescription(
key=ATTR_FAN_SPEED,
translation_key=ATTR_FAN_SPEED,
icon="mdi:fan",
feature=AirWaterFeature.FAN_SPEED_STEPS,
native_min_value=0,
native_max_value=6,
native_step=1,
),
)


async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
device: AirWaterDevice = hass.data[DOMAIN][DEVICES][entry.entry_id]
async_add_entities([AirWaterFanSpeedEntity(device, entry)])
entities: list[NumberEntity] = []

for description in FAN_SPEED_TYPES:
if bool(device.model.features & description.feature):
entities.append(AirWaterGenericFanSpeedEntity(device, entry, description))

async_add_entities(entities)


class AirWaterGenericFanSpeedEntity(AirWaterEntity, NumberEntity):
entity_description: AirWaterFanSpeedDescription

class AirWaterFanSpeedEntity(AirWaterEntity, NumberEntity):
_attr_translation_key = ATTR_FAN_SPEED
_attr_icon = "mdi:fan"
_attr_native_unit_of_measurement = PERCENTAGE
_attr_native_min_value = 0
_attr_native_max_value = 100
_attr_native_step = 1
def __init__(self, device: AirWaterDevice, entry: ConfigEntry, description: AirWaterFanSpeedDescription) -> None:
super().__init__(device, entry)
self.entity_description = description

@property
def unique_id(self) -> str:
return f"{super().unique_id}_{self._attr_translation_key}"
return f"{super().unique_id}_{self.entity_description.translation_key}"

@property
def native_value(self) -> float:
Expand Down

0 comments on commit f61f6de

Please sign in to comment.