From e5c7d645059cc1fa98fe75f16a5e97e4b3fa9513 Mon Sep 17 00:00:00 2001 From: Oscar Tin Yiu Lai Date: Wed, 1 Aug 2018 14:38:34 +1000 Subject: [PATCH] Expose internal states and fixed on/off state of Dyson Fans (#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 --- homeassistant/components/fan/dyson.py | 13 +++++++- tests/components/fan/test_dyson.py | 44 ++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/fan/dyson.py b/homeassistant/components/fan/dyson.py index fbe9ffc948cdd0..3eb4646e6dcbdc 100644 --- a/homeassistant/components/fan/dyson.py +++ b/homeassistant/components/fan/dyson.py @@ -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' @@ -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 @@ -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 + } diff --git a/tests/components/fan/test_dyson.py b/tests/components/fan/test_dyson.py index 49338e123e364c..2953ea2754ba02 100644 --- a/tests/components/fan/test_dyson.py +++ b/tests/components/fan/test_dyson.py @@ -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 @@ -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()