Skip to content

Commit

Permalink
Support fan changes in Hass 2024.8 #1462
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Sep 7, 2024
1 parent 9b40568 commit db85a72
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
15 changes: 14 additions & 1 deletion custom_components/sonoff/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
FanEntity,
FanEntityFeature,
)
from homeassistant.const import MAJOR_VERSION, MINOR_VERSION

from .core.const import DOMAIN
from .core.entity import XEntity
Expand Down Expand Up @@ -29,7 +30,16 @@ async def async_setup_entry(hass, config_entry, add_entities):
class XFan(XEntity, FanEntity):
params = {"switches", "fan"}
_attr_speed_count = 3
_attr_supported_features = FanEntityFeature.SET_SPEED

# https://developers.home-assistant.io/blog/2024/07/19/fan-fanentityfeatures-turn-on_off/
if (MAJOR_VERSION, MINOR_VERSION) >= (2024, 8):
_attr_supported_features = (
FanEntityFeature.SET_SPEED
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
else:
_attr_supported_features = FanEntityFeature.SET_SPEED

def __init__(self, ewelink: XRegistry, device: XDevice) -> None:
super().__init__(ewelink, device)
Expand Down Expand Up @@ -166,6 +176,9 @@ async def async_set_percentage(self, percentage: int):

# noinspection PyAbstractClass
class XToggleFan(XEntity, FanEntity):
if (MAJOR_VERSION, MINOR_VERSION) >= (2024, 8):
_attr_supported_features = FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON

@property
def is_on(self):
return self._attr_is_on
31 changes: 31 additions & 0 deletions tests/test_backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,34 @@ def test_2024_1_cached_properties():
_, entities = init({"extra": {"uiid": 1256}})
sensor: SensorEntity = next(e for e in entities)
assert sensor.should_poll is False


def test_2024_2_climate():
_, entities = init({"extra": {"uiid": 15}})
climate: ClimateEntity = next(e for e in entities if isinstance(e, XClimateTH))
if (MAJOR_VERSION, MINOR_VERSION) >= (2024, 2):
assert climate.supported_features == (
ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
| ClimateEntityFeature.TURN_ON
| ClimateEntityFeature.TURN_OFF
)
else:
assert (
climate.supported_features == ClimateEntityFeature.TARGET_TEMPERATURE_RANGE
)


def test_2024_8_fan():
_, entities = init({"extra": {"uiid": 34}})
fan: FanEntity = next(e for e in entities if isinstance(e, XFan))
if (MAJOR_VERSION, MINOR_VERSION) >= (2024, 8):
assert fan.supported_features == (
FanEntityFeature.SET_SPEED
| FanEntityFeature.PRESET_MODE
| FanEntityFeature.TURN_OFF
| FanEntityFeature.TURN_ON
)
else:
assert fan.supported_features == (
FanEntityFeature.SET_SPEED | FanEntityFeature.PRESET_MODE
)
25 changes: 22 additions & 3 deletions tests/test_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Union

from homeassistant.components.binary_sensor import BinarySensorDeviceClass
from homeassistant.components.fan import FanEntity
from homeassistant.components.fan import FanEntity, FanEntityFeature
from homeassistant.components.light import (
COLOR_MODE_COLOR_TEMP,
COLOR_MODE_RGB,
Expand All @@ -12,7 +12,12 @@
)
from homeassistant.components.sensor import SensorDeviceClass, SensorStateClass
from homeassistant.components.switch import SwitchEntity
from homeassistant.const import TEMP_FAHRENHEIT, UnitOfEnergy
from homeassistant.const import (
TEMP_FAHRENHEIT,
UnitOfEnergy,
MAJOR_VERSION,
MINOR_VERSION,
)
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.util.unit_system import IMPERIAL_SYSTEM

Expand All @@ -27,7 +32,7 @@
SIGNAL_UPDATE,
)
from custom_components.sonoff.cover import XCover, XCoverDualR3, XZigbeeCover, XCover91
from custom_components.sonoff.fan import XFan
from custom_components.sonoff.fan import XFan, XToggleFan
from custom_components.sonoff.light import (
UIID22_MODES,
XDiffuserLight,
Expand Down Expand Up @@ -910,6 +915,20 @@ def test_device_class2():
assert light.__class__.async_turn_on == func


def test_device_class_fan():
entities = get_entitites(
{"extra": {"uiid": 1}}, {"devices": {DEVICEID: {"device_class": "fan"}}}
)

fan: FanEntity = next(e for e in entities if isinstance(e, XToggleFan))
if (MAJOR_VERSION, MINOR_VERSION) >= (2024, 8):
assert fan.supported_features == (
FanEntityFeature.TURN_OFF | FanEntityFeature.TURN_ON
)
else:
assert fan.supported_features == 0


def test_light_group():
entities = get_entitites(
{
Expand Down

0 comments on commit db85a72

Please sign in to comment.