Skip to content

Commit

Permalink
Expose internal states and fixed on/off state of Dyson Fans (home-ass…
Browse files Browse the repository at this point in the history
…istant#15716)

* exposing internal state and fixed onoff state

* fixed styling

* revert file mode changes

* removed self type hints

* better unit test and changed the way to return attributes

* made wolfie happy
  • Loading branch information
soraxas authored and Jacob Mansfield committed Sep 4, 2018
1 parent 2e82eda commit e5c7d64
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
13 changes: 12 additions & 1 deletion homeassistant/components/fan/dyson.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

CONF_NIGHT_MODE = 'night_mode'

ATTR_IS_NIGHT_MODE = 'is_night_mode'
ATTR_IS_AUTO_MODE = 'is_auto_mode'

DEPENDENCIES = ['dyson']
DYSON_FAN_DEVICES = 'dyson_fan_devices'

Expand Down Expand Up @@ -158,7 +161,7 @@ def oscillating(self):
def is_on(self):
"""Return true if the entity is on."""
if self._device.state:
return self._device.state.fan_state == "FAN"
return self._device.state.fan_mode == "FAN"
return False

@property
Expand Down Expand Up @@ -232,3 +235,11 @@ def speed_list(self) -> list:
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_OSCILLATE | SUPPORT_SET_SPEED

@property
def device_state_attributes(self) -> dict:
"""Return optional state attributes."""
return {
ATTR_IS_NIGHT_MODE: self.is_night_mode,
ATTR_IS_AUTO_MODE: self.is_auto_mode
}
44 changes: 43 additions & 1 deletion tests/components/fan/test_dyson.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import unittest
from unittest import mock

from homeassistant.setup import setup_component
from homeassistant.components import dyson as dyson_parent
from homeassistant.components.dyson import DYSON_DEVICES
from homeassistant.components.fan import dyson
from homeassistant.components.fan import (dyson, ATTR_SPEED, ATTR_SPEED_LIST,
ATTR_OSCILLATING)
from tests.common import get_test_home_assistant
from libpurecoollink.const import FanSpeed, FanMode, NightMode, Oscillation
from libpurecoollink.dyson_pure_state import DysonPureCoolState
Expand Down Expand Up @@ -91,6 +94,45 @@ def _add_device(devices):
self.hass.data[dyson.DYSON_DEVICES] = [device_fan, device_non_fan]
dyson.setup_platform(self.hass, None, _add_device)

@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
return_value=[_get_device_on()])
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
def test_get_state_attributes(self, mocked_login, mocked_devices):
"""Test async added to hass."""
setup_component(self.hass, dyson_parent.DOMAIN, {
dyson_parent.DOMAIN: {
dyson_parent.CONF_USERNAME: "email",
dyson_parent.CONF_PASSWORD: "password",
dyson_parent.CONF_LANGUAGE: "US",
}
})
self.hass.block_till_done()
state = self.hass.states.get("{}.{}".format(
dyson.DOMAIN,
mocked_devices.return_value[0].name))

assert dyson.ATTR_IS_NIGHT_MODE in state.attributes
assert dyson.ATTR_IS_AUTO_MODE in state.attributes
assert ATTR_SPEED in state.attributes
assert ATTR_SPEED_LIST in state.attributes
assert ATTR_OSCILLATING in state.attributes

@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
return_value=[_get_device_on()])
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
def test_async_added_to_hass(self, mocked_login, mocked_devices):
"""Test async added to hass."""
setup_component(self.hass, dyson_parent.DOMAIN, {
dyson_parent.DOMAIN: {
dyson_parent.CONF_USERNAME: "email",
dyson_parent.CONF_PASSWORD: "password",
dyson_parent.CONF_LANGUAGE: "US",
}
})
self.hass.block_till_done()
self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 1)
assert mocked_devices.return_value[0].add_message_listener.called

def test_dyson_set_speed(self):
"""Test set fan speed."""
device = _get_device_on()
Expand Down

0 comments on commit e5c7d64

Please sign in to comment.