From 2581b031d9f6b2cfe8f5384d6f12e04e1aab324d Mon Sep 17 00:00:00 2001 From: Xiaonan Shen Date: Wed, 6 May 2020 00:58:07 -0700 Subject: [PATCH] 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 --- homeassistant/components/demo/fan.py | 11 ++++++--- homeassistant/components/fan/__init__.py | 29 +++++++++++++----------- tests/components/fan/test_init.py | 2 +- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/demo/fan.py b/homeassistant/components/demo/fan.py index 966ba51cacb..4b75e817153 100644 --- a/homeassistant/components/demo/fan.py +++ b/homeassistant/components/demo/fan.py @@ -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.""" diff --git a/homeassistant/components/fan/__init__.py b/homeassistant/components/fan/__init__.py index a395a5da470..4531656f7af 100644 --- a/homeassistant/components/fan/__init__.py +++ b/homeassistant/components/fan/__init__.py @@ -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 diff --git a/tests/components/fan/test_init.py b/tests/components/fan/test_init.py index d9935cc0063..f27a3ff8ab6 100644 --- a/tests/components/fan/test_init.py +++ b/tests/components/fan/test_init.py @@ -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):