Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions homeassistant/components/demo/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def __init__(self, hass, name: str, supported_features: int) -> None:
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"

Expand Down Expand Up @@ -89,14 +89,19 @@ def set_direction(self, direction: str) -> None:

def oscillate(self, oscillating: bool) -> None:
"""Set oscillation."""
self.oscillating = oscillating
self._oscillating = oscillating
self.schedule_update_ha_state()

@property
def current_direction(self) -> str:
"""Fan direction."""
return self._direction

@property
def oscillating(self) -> bool:
"""Oscillating."""
return self._oscillating

@property
def supported_features(self) -> int:
"""Flag supported features."""
Expand Down
29 changes: 16 additions & 13 deletions homeassistant/components/fan/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@
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:
Expand Down Expand Up @@ -166,23 +160,32 @@ def current_direction(self) -> Optional[str]:
"""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

if supported_features & SUPPORT_DIRECTION:
data[ATTR_DIRECTION] = self.current_direction

for prop, attr in PROP_TO_ATTR.items():
if not hasattr(self, prop):
continue
if supported_features & SUPPORT_OSCILLATE:
data[ATTR_OSCILLATING] = self.oscillating

value = getattr(self, prop)
if value is not None:
data[attr] = value
if supported_features & SUPPORT_SET_SPEED:
data[ATTR_SPEED] = self.speed

return data

Expand Down
2 changes: 1 addition & 1 deletion tests/components/fan/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_fanentity(self):
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):
Expand Down