Add Dyson Pure Cool Link support (#7795)

* Add Dyson Pure Cool Link support

* Code review

* Improve auto/night mode

* Move night_mode to Dyson fan component

* Code review

* fix asynchrone/sync

* Create dyson.py
This commit is contained in:
Charles Blonde authored and Pascal Vizeli committed 2017-06-14 13:56:03 +02:00
1 parent bf2fe60cb5
commit 8c0967a190
10 files changed
+924 -1

No files matched your search

+279
View File
@@ -0,0 +1,279 @@
"""Test the Dyson fan component."""
import unittest
from unittest import mock
from homeassistant.components.dyson import DYSON_DEVICES
from homeassistant.components.fan import dyson
from tests.common import get_test_home_assistant
from libpurecoollink.const import FanSpeed, FanMode, NightMode, Oscillation
def _get_device_with_no_state():
"""Return a device with no state."""
device = mock.Mock()
device.name = "Device_name"
device.state = None
return device
def _get_device_off():
"""Return a device with state off."""
device = mock.Mock()
device.name = "Device_name"
device.state = mock.Mock()
device.state.fan_mode = "OFF"
device.state.night_mode = "ON"
device.state.speed = "0004"
return device
def _get_device_auto():
"""Return a device with state auto."""
device = mock.Mock()
device.name = "Device_name"
device.state = mock.Mock()
device.state.fan_mode = "AUTO"
device.state.night_mode = "ON"
device.state.speed = "AUTO"
return device
def _get_device_on():
"""Return a valid state on."""
device = mock.Mock()
device.name = "Device_name"
device.state = mock.Mock()
device.state.fan_mode = "FAN"
device.state.fan_state = "FAN"
device.state.oscillation = "ON"
device.state.night_mode = "OFF"
device.state.speed = "0001"
return device
class DysonTest(unittest.TestCase):
"""Dyson Sensor component test class."""
def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
def test_setup_component_with_no_devices(self):
"""Test setup component with no devices."""
self.hass.data[dyson.DYSON_DEVICES] = []
add_devices = mock.MagicMock()
dyson.setup_platform(self.hass, None, add_devices)
add_devices.assert_called_with([])
def test_setup_component(self):
"""Test setup component with devices."""
def _add_device(devices):
assert len(devices) == 1
assert devices[0].name == "Device_name"
device = _get_device_on()
self.hass.data[dyson.DYSON_DEVICES] = [device]
dyson.setup_platform(self.hass, None, _add_device)
def test_dyson_set_speed(self):
"""Test set fan speed."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.should_poll)
component.set_speed("1")
set_config = device.set_configuration
set_config.assert_called_with(fan_mode=FanMode.FAN,
fan_speed=FanSpeed.FAN_SPEED_1)
component.set_speed("AUTO")
set_config = device.set_configuration
set_config.assert_called_with(fan_mode=FanMode.AUTO)
def test_dyson_turn_on(self):
"""Test turn on fan."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.should_poll)
component.turn_on()
set_config = device.set_configuration
set_config.assert_called_with(fan_mode=FanMode.FAN)
def test_dyson_turn_night_mode(self):
"""Test turn on fan with night mode."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.should_poll)
component.night_mode(True)
set_config = device.set_configuration
set_config.assert_called_with(night_mode=NightMode.NIGHT_MODE_ON)
component.night_mode(False)
set_config = device.set_configuration
set_config.assert_called_with(night_mode=NightMode.NIGHT_MODE_OFF)
def test_is_night_mode(self):
"""Test night mode."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.is_night_mode)
device = _get_device_off()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertTrue(component.is_night_mode)
def test_dyson_turn_auto_mode(self):
"""Test turn on/off fan with auto mode."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.should_poll)
component.auto_mode(True)
set_config = device.set_configuration
set_config.assert_called_with(fan_mode=FanMode.AUTO)
component.auto_mode(False)
set_config = device.set_configuration
set_config.assert_called_with(fan_mode=FanMode.FAN)
def test_is_auto_mode(self):
"""Test auto mode."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.is_auto_mode)
device = _get_device_auto()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertTrue(component.is_auto_mode)
def test_dyson_turn_on_speed(self):
"""Test turn on fan with specified speed."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.should_poll)
component.turn_on("1")
set_config = device.set_configuration
set_config.assert_called_with(fan_mode=FanMode.FAN,
fan_speed=FanSpeed.FAN_SPEED_1)
component.turn_on("AUTO")
set_config = device.set_configuration
set_config.assert_called_with(fan_mode=FanMode.AUTO)
def test_dyson_turn_off(self):
"""Test turn off fan."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.should_poll)
component.turn_off()
set_config = device.set_configuration
set_config.assert_called_with(fan_mode=FanMode.OFF)
def test_dyson_oscillate_off(self):
"""Test turn off oscillation."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
component.oscillate(False)
set_config = device.set_configuration
set_config.assert_called_with(oscillation=Oscillation.OSCILLATION_OFF)
def test_dyson_oscillate_on(self):
"""Test turn on oscillation."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
component.oscillate(True)
set_config = device.set_configuration
set_config.assert_called_with(oscillation=Oscillation.OSCILLATION_ON)
def test_dyson_oscillate_value_on(self):
"""Test get oscillation value on."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertTrue(component.oscillating)
def test_dyson_oscillate_value_off(self):
"""Test get oscillation value off."""
device = _get_device_off()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.oscillating)
def test_dyson_on(self):
"""Test device is on."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertTrue(component.is_on)
def test_dyson_off(self):
"""Test device is off."""
device = _get_device_off()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.is_on)
device = _get_device_with_no_state()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertFalse(component.is_on)
def test_dyson_get_speed(self):
"""Test get device speed."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertEqual(component.speed, 1)
device = _get_device_off()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertEqual(component.speed, 4)
device = _get_device_with_no_state()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertIsNone(component.speed)
device = _get_device_auto()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertEqual(component.speed, "AUTO")
def test_dyson_get_direction(self):
"""Test get device direction."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertIsNone(component.current_direction)
def test_dyson_get_speed_list(self):
"""Test get speeds list."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertEqual(len(component.speed_list), 11)
def test_dyson_supported_features(self):
"""Test supported features."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
self.assertEqual(component.supported_features, 3)
def test_on_message(self):
"""Test when message is received."""
device = _get_device_on()
component = dyson.DysonPureCoolLinkDevice(self.hass, device)
component.entity_id = "entity_id"
component.schedule_update_ha_state = mock.Mock()
component.on_message("Message")
component.schedule_update_ha_state.assert_called_with()
def test_service_set_night_mode(self):
"""Test set night mode service."""
dyson_device = mock.MagicMock()
self.hass.data[DYSON_DEVICES] = []
dyson_device.entity_id = 'fan.living_room'
self.hass.data[dyson.DYSON_FAN_DEVICES] = [dyson_device]
dyson.setup_platform(self.hass, None, mock.MagicMock())
self.hass.services.call(dyson.DOMAIN, dyson.SERVICE_SET_NIGHT_MODE,
{"entity_id": "fan.bed_room",
"night_mode": True}, True)
assert not dyson_device.night_mode.called
self.hass.services.call(dyson.DOMAIN, dyson.SERVICE_SET_NIGHT_MODE,
{"entity_id": "fan.living_room",
"night_mode": True}, True)
dyson_device.night_mode.assert_called_with(True)
+76
View File
@@ -0,0 +1,76 @@
"""Test the Dyson sensor(s) component."""
import unittest
from unittest import mock
from homeassistant.const import STATE_UNKNOWN
from homeassistant.components.sensor import dyson
from tests.common import get_test_home_assistant
def _get_device_without_state():
"""Return a valid device provide by Dyson web services."""
device = mock.Mock()
device.name = "Device_name"
device.state = None
return device
def _get_with_state():
"""Return a valid device with state values."""
device = mock.Mock()
device.name = "Device_name"
device.state = mock.Mock()
device.state.filter_life = 100
return device
class DysonTest(unittest.TestCase):
"""Dyson Sensor component test class."""
def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
def test_setup_component_with_no_devices(self):
"""Test setup component with no devices."""
self.hass.data[dyson.DYSON_DEVICES] = []
add_devices = mock.MagicMock()
dyson.setup_platform(self.hass, None, add_devices)
add_devices.assert_called_with([])
def test_setup_component(self):
"""Test setup component with devices."""
def _add_device(devices):
assert len(devices) == 1
assert devices[0].name == "Device_name filter life"
device = _get_device_without_state()
self.hass.data[dyson.DYSON_DEVICES] = [device]
dyson.setup_platform(self.hass, None, _add_device)
def test_dyson_filter_life_sensor(self):
"""Test sensor with no value."""
sensor = dyson.DysonFilterLifeSensor(self.hass,
_get_device_without_state())
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, STATE_UNKNOWN)
self.assertEqual(sensor.unit_of_measurement, "hours")
self.assertEqual(sensor.name, "Device_name filter life")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")
sensor.on_message('message')
def test_dyson_filter_life_sensor_with_values(self):
"""Test sensor with values."""
sensor = dyson.DysonFilterLifeSensor(self.hass, _get_with_state())
sensor.entity_id = "sensor.dyson_1"
self.assertFalse(sensor.should_poll)
self.assertEqual(sensor.state, 100)
self.assertEqual(sensor.unit_of_measurement, "hours")
self.assertEqual(sensor.name, "Device_name filter life")
self.assertEqual(sensor.entity_id, "sensor.dyson_1")
sensor.on_message('message')
+161
View File
@@ -0,0 +1,161 @@
"""Test the parent Dyson component."""
import unittest
from unittest import mock
from homeassistant.components import dyson
from tests.common import get_test_home_assistant
def _get_dyson_account_device_available():
"""Return a valid device provide by Dyson web services."""
device = mock.Mock()
device.serial = "XX-XXXXX-XX"
device.connect = mock.Mock(return_value=True)
return device
def _get_dyson_account_device_not_available():
"""Return an invalid device provide by Dyson web services."""
device = mock.Mock()
device.serial = "XX-XXXXX-XX"
device.connect = mock.Mock(return_value=False)
return device
class DysonTest(unittest.TestCase):
"""Dyson parent component test class."""
def setUp(self): # pylint: disable=invalid-name
"""Setup things to be run when tests are started."""
self.hass = get_test_home_assistant()
def tearDown(self): # pylint: disable=invalid-name
"""Stop everything that was started."""
self.hass.stop()
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=False)
def test_dyson_login_failed(self, mocked_login):
"""Test if Dyson connection failed."""
dyson.setup(self.hass, {dyson.DOMAIN: {
dyson.CONF_USERNAME: "email",
dyson.CONF_PASSWORD: "password",
dyson.CONF_LANGUAGE: "FR"
}})
self.assertEqual(mocked_login.call_count, 1)
@mock.patch('libpurecoollink.dyson.DysonAccount.devices', return_value=[])
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
def test_dyson_login(self, mocked_login, mocked_devices):
"""Test valid connection to dyson web service."""
dyson.setup(self.hass, {dyson.DOMAIN: {
dyson.CONF_USERNAME: "email",
dyson.CONF_PASSWORD: "password",
dyson.CONF_LANGUAGE: "FR"
}})
self.assertEqual(mocked_login.call_count, 1)
self.assertEqual(mocked_devices.call_count, 1)
self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)
@mock.patch('homeassistant.helpers.discovery.load_platform')
@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
return_value=[_get_dyson_account_device_available()])
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
def test_dyson_custom_conf(self, mocked_login, mocked_devices,
mocked_discovery):
"""Test device connection using custom configuration."""
dyson.setup(self.hass, {dyson.DOMAIN: {
dyson.CONF_USERNAME: "email",
dyson.CONF_PASSWORD: "password",
dyson.CONF_LANGUAGE: "FR",
dyson.CONF_DEVICES: [
{
"device_id": "XX-XXXXX-XX",
"device_ip": "192.168.0.1"
}
]
}})
self.assertEqual(mocked_login.call_count, 1)
self.assertEqual(mocked_devices.call_count, 1)
self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 1)
self.assertEqual(mocked_discovery.call_count, 2)
@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
return_value=[_get_dyson_account_device_not_available()])
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
def test_dyson_custom_conf_device_not_available(self, mocked_login,
mocked_devices):
"""Test device connection with an invalid device."""
dyson.setup(self.hass, {dyson.DOMAIN: {
dyson.CONF_USERNAME: "email",
dyson.CONF_PASSWORD: "password",
dyson.CONF_LANGUAGE: "FR",
dyson.CONF_DEVICES: [
{
"device_id": "XX-XXXXX-XX",
"device_ip": "192.168.0.1"
}
]
}})
self.assertEqual(mocked_login.call_count, 1)
self.assertEqual(mocked_devices.call_count, 1)
self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)
@mock.patch('homeassistant.helpers.discovery.load_platform')
@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
return_value=[_get_dyson_account_device_available()])
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
def test_dyson_custom_conf_with_unknown_device(self, mocked_login,
mocked_devices,
mocked_discovery):
"""Test device connection with custom conf and unknown device."""
dyson.setup(self.hass, {dyson.DOMAIN: {
dyson.CONF_USERNAME: "email",
dyson.CONF_PASSWORD: "password",
dyson.CONF_LANGUAGE: "FR",
dyson.CONF_DEVICES: [
{
"device_id": "XX-XXXXX-XY",
"device_ip": "192.168.0.1"
}
]
}})
self.assertEqual(mocked_login.call_count, 1)
self.assertEqual(mocked_devices.call_count, 1)
self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)
self.assertEqual(mocked_discovery.call_count, 0)
@mock.patch('homeassistant.helpers.discovery.load_platform')
@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
return_value=[_get_dyson_account_device_available()])
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
def test_dyson_discovery(self, mocked_login, mocked_devices,
mocked_discovery):
"""Test device connection using discovery."""
dyson.setup(self.hass, {dyson.DOMAIN: {
dyson.CONF_USERNAME: "email",
dyson.CONF_PASSWORD: "password",
dyson.CONF_LANGUAGE: "FR",
dyson.CONF_TIMEOUT: 5,
dyson.CONF_RETRY: 2
}})
self.assertEqual(mocked_login.call_count, 1)
self.assertEqual(mocked_devices.call_count, 1)
self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 1)
self.assertEqual(mocked_discovery.call_count, 2)
@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
return_value=[_get_dyson_account_device_not_available()])
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
def test_dyson_discovery_device_not_available(self, mocked_login,
mocked_devices):
"""Test device connection with discovery and invalid device."""
dyson.setup(self.hass, {dyson.DOMAIN: {
dyson.CONF_USERNAME: "email",
dyson.CONF_PASSWORD: "password",
dyson.CONF_LANGUAGE: "FR",
dyson.CONF_TIMEOUT: 5,
dyson.CONF_RETRY: 2
}})
self.assertEqual(mocked_login.call_count, 1)
self.assertEqual(mocked_devices.call_count, 1)
self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)