1
mirror of https://github.com/home-assistant/core synced 2024-09-12 15:16:21 +02:00

Fix fan not checking supported_features (#35248)

* Fix fan not checking supported_features

* Fix pylint

* Fix test

* Code cleanup

* Fix fan demo

* Code style improvement
This commit is contained in:
Xiaonan Shen 2020-05-06 00:58:07 -07:00 committed by GitHub
parent 1c5b4dbd97
commit 2581b031d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 17 deletions

View File

@ -37,12 +37,12 @@ class DemoFan(FanEntity):
self.hass = hass
self._supported_features = supported_features
self._speed = STATE_OFF
self.oscillating = None
self._oscillating = None
self._direction = None
self._name = name
if supported_features & SUPPORT_OSCILLATE:
self.oscillating = False
self._oscillating = False
if supported_features & SUPPORT_DIRECTION:
self._direction = "forward"
@ -89,7 +89,7 @@ class DemoFan(FanEntity):
def oscillate(self, oscillating: bool) -> None:
"""Set oscillation."""
self.oscillating = oscillating
self._oscillating = oscillating
self.schedule_update_ha_state()
@property
@ -97,6 +97,11 @@ class DemoFan(FanEntity):
"""Fan direction."""
return self._direction
@property
def oscillating(self) -> bool:
"""Oscillating."""
return self._oscillating
@property
def supported_features(self) -> int:
"""Flag supported features."""

View File

@ -45,12 +45,6 @@ ATTR_SPEED_LIST = "speed_list"
ATTR_OSCILLATING = "oscillating"
ATTR_DIRECTION = "direction"
PROP_TO_ATTR = {
"speed": ATTR_SPEED,
"oscillating": ATTR_OSCILLATING,
"current_direction": ATTR_DIRECTION,
}
@bind_hass
def is_on(hass, entity_id: str) -> bool:
@ -166,23 +160,32 @@ class FanEntity(ToggleEntity):
"""Return the current direction of the fan."""
return None
@property
def oscillating(self):
"""Return whether or not the fan is currently oscillating."""
return None
@property
def capability_attributes(self):
"""Return capability attributes."""
return {ATTR_SPEED_LIST: self.speed_list}
if self.supported_features & SUPPORT_SET_SPEED:
return {ATTR_SPEED_LIST: self.speed_list}
return {}
@property
def state_attributes(self) -> dict:
"""Return optional state attributes."""
data = {}
supported_features = self.supported_features
for prop, attr in PROP_TO_ATTR.items():
if not hasattr(self, prop):
continue
if supported_features & SUPPORT_DIRECTION:
data[ATTR_DIRECTION] = self.current_direction
value = getattr(self, prop)
if value is not None:
data[attr] = value
if supported_features & SUPPORT_OSCILLATE:
data[ATTR_OSCILLATING] = self.oscillating
if supported_features & SUPPORT_SET_SPEED:
data[ATTR_SPEED] = self.speed
return data

View File

@ -31,7 +31,7 @@ class TestFanEntity(unittest.TestCase):
assert self.fan.state == "off"
assert len(self.fan.speed_list) == 0
assert self.fan.supported_features == 0
assert {"speed_list": []} == self.fan.capability_attributes
assert self.fan.capability_attributes == {}
# Test set_speed not required
self.fan.oscillate(True)
with pytest.raises(NotImplementedError):