Add support to Dyson 360 Eye robot vacuum using new vacuum platform (#8852)
* Add support to Dyson 360 Eye robot vacuum using new vacuum platform * Fix tests with Python 3.5 * Code review * Code review - v2 * Code review - v3
This commit is contained in:
1 parent
82a7dffc03
commit
83afd12807
11 files changed
+468
-25
No files matched your search
@@ -7,6 +7,7 @@ from homeassistant.components.fan import dyson
|
||||
from tests.common import get_test_home_assistant
|
||||
from libpurecoollink.const import FanSpeed, FanMode, NightMode, Oscillation
|
||||
from libpurecoollink.dyson_pure_state import DysonPureCoolState
|
||||
from libpurecoollink.dyson_pure_cool_link import DysonPureCoolLink
|
||||
|
||||
|
||||
class MockDysonState(DysonPureCoolState):
|
||||
@@ -49,7 +50,7 @@ def _get_device_auto():
|
||||
|
||||
def _get_device_on():
|
||||
"""Return a valid state on."""
|
||||
device = mock.Mock()
|
||||
device = mock.Mock(spec=DysonPureCoolLink)
|
||||
device.name = "Device_name"
|
||||
device.state = mock.Mock()
|
||||
device.state.fan_mode = "FAN"
|
||||
@@ -84,8 +85,10 @@ class DysonTest(unittest.TestCase):
|
||||
assert len(devices) == 1
|
||||
assert devices[0].name == "Device_name"
|
||||
|
||||
device = _get_device_on()
|
||||
self.hass.data[dyson.DYSON_DEVICES] = [device]
|
||||
device_fan = _get_device_on()
|
||||
device_non_fan = _get_device_off()
|
||||
|
||||
self.hass.data[dyson.DYSON_DEVICES] = [device_fan, device_non_fan]
|
||||
dyson.setup_platform(self.hass, None, _add_device)
|
||||
|
||||
def test_dyson_set_speed(self):
|
||||
|
||||
@@ -6,11 +6,12 @@ from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT, \
|
||||
STATE_OFF
|
||||
from homeassistant.components.sensor import dyson
|
||||
from tests.common import get_test_home_assistant
|
||||
from libpurecoollink.dyson_pure_cool_link import DysonPureCoolLink
|
||||
|
||||
|
||||
def _get_device_without_state():
|
||||
"""Return a valid device provide by Dyson web services."""
|
||||
device = mock.Mock()
|
||||
device = mock.Mock(spec=DysonPureCoolLink)
|
||||
device.name = "Device_name"
|
||||
device.state = None
|
||||
device.environmental_state = None
|
||||
@@ -75,8 +76,9 @@ class DysonTest(unittest.TestCase):
|
||||
assert devices[3].name == "Device_name temperature"
|
||||
assert devices[4].name == "Device_name air quality"
|
||||
|
||||
device = _get_device_without_state()
|
||||
self.hass.data[dyson.DYSON_DEVICES] = [device]
|
||||
device_fan = _get_device_without_state()
|
||||
device_non_fan = _get_with_state()
|
||||
self.hass.data[dyson.DYSON_DEVICES] = [device_fan, device_non_fan]
|
||||
dyson.setup_platform(self.hass, None, _add_device)
|
||||
|
||||
def test_dyson_filter_life_sensor(self):
|
||||
|
||||
@@ -11,6 +11,7 @@ def _get_dyson_account_device_available():
|
||||
device = mock.Mock()
|
||||
device.serial = "XX-XXXXX-XX"
|
||||
device.connect = mock.Mock(return_value=True)
|
||||
device.auto_connect = mock.Mock(return_value=True)
|
||||
return device
|
||||
|
||||
|
||||
@@ -19,6 +20,15 @@ def _get_dyson_account_device_not_available():
|
||||
device = mock.Mock()
|
||||
device.serial = "XX-XXXXX-XX"
|
||||
device.connect = mock.Mock(return_value=False)
|
||||
device.auto_connect = mock.Mock(return_value=False)
|
||||
return device
|
||||
|
||||
|
||||
def _get_dyson_account_device_error():
|
||||
"""Return an invalid device raising OSError while connecting."""
|
||||
device = mock.Mock()
|
||||
device.serial = "XX-XXXXX-XX"
|
||||
device.connect = mock.Mock(side_effect=OSError("Network error"))
|
||||
return device
|
||||
|
||||
|
||||
@@ -77,7 +87,7 @@ class DysonTest(unittest.TestCase):
|
||||
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)
|
||||
self.assertEqual(mocked_discovery.call_count, 3)
|
||||
|
||||
@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
|
||||
return_value=[_get_dyson_account_device_not_available()])
|
||||
@@ -100,6 +110,27 @@ class DysonTest(unittest.TestCase):
|
||||
self.assertEqual(mocked_devices.call_count, 1)
|
||||
self.assertEqual(len(self.hass.data[dyson.DYSON_DEVICES]), 0)
|
||||
|
||||
@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
|
||||
return_value=[_get_dyson_account_device_error()])
|
||||
@mock.patch('libpurecoollink.dyson.DysonAccount.login', return_value=True)
|
||||
def test_dyson_custom_conf_device_error(self, mocked_login,
|
||||
mocked_devices):
|
||||
"""Test device connection with device raising an exception."""
|
||||
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()])
|
||||
@@ -141,7 +172,7 @@ class DysonTest(unittest.TestCase):
|
||||
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)
|
||||
self.assertEqual(mocked_discovery.call_count, 3)
|
||||
|
||||
@mock.patch('libpurecoollink.dyson.DysonAccount.devices',
|
||||
return_value=[_get_dyson_account_device_not_available()])
|
||||
|
||||
@@ -0,0 +1,189 @@
|
||||
"""Test the Dyson 360 eye robot vacuum component."""
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from libpurecoollink.dyson_360_eye import Dyson360Eye
|
||||
from libpurecoollink.const import Dyson360EyeMode, PowerMode
|
||||
|
||||
from homeassistant.components.vacuum import dyson
|
||||
from homeassistant.components.vacuum.dyson import Dyson360EyeDevice
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
def _get_non_vacuum_device():
|
||||
"""Return a non vacuum device."""
|
||||
device = mock.Mock()
|
||||
device.name = "Device_Fan"
|
||||
device.state = None
|
||||
return device
|
||||
|
||||
|
||||
def _get_vacuum_device_cleaning():
|
||||
"""Return a vacuum device running."""
|
||||
device = mock.Mock(spec=Dyson360Eye)
|
||||
device.name = "Device_Vacuum"
|
||||
device.state = mock.MagicMock()
|
||||
device.state.state = Dyson360EyeMode.FULL_CLEAN_RUNNING
|
||||
device.state.battery_level = 85
|
||||
device.state.power_mode = PowerMode.QUIET
|
||||
device.state.position = (0, 0)
|
||||
return device
|
||||
|
||||
|
||||
def _get_vacuum_device_charging():
|
||||
"""Return a vacuum device charging."""
|
||||
device = mock.Mock(spec=Dyson360Eye)
|
||||
device.name = "Device_Vacuum"
|
||||
device.state = mock.MagicMock()
|
||||
device.state.state = Dyson360EyeMode.INACTIVE_CHARGING
|
||||
device.state.battery_level = 40
|
||||
device.state.power_mode = PowerMode.QUIET
|
||||
device.state.position = (0, 0)
|
||||
return device
|
||||
|
||||
|
||||
def _get_vacuum_device_pause():
|
||||
"""Return a vacuum device in pause."""
|
||||
device = mock.MagicMock(spec=Dyson360Eye)
|
||||
device.name = "Device_Vacuum"
|
||||
device.state = mock.MagicMock()
|
||||
device.state.state = Dyson360EyeMode.FULL_CLEAN_PAUSED
|
||||
device.state.battery_level = 40
|
||||
device.state.power_mode = PowerMode.QUIET
|
||||
device.state.position = (0, 0)
|
||||
return device
|
||||
|
||||
|
||||
def _get_vacuum_device_unknown_state():
|
||||
"""Return a vacuum device with unknown state."""
|
||||
device = mock.Mock(spec=Dyson360Eye)
|
||||
device.name = "Device_Vacuum"
|
||||
device.state = mock.MagicMock()
|
||||
device.state.state = "Unknown"
|
||||
return device
|
||||
|
||||
|
||||
class DysonTest(unittest.TestCase):
|
||||
"""Dyson 360 eye robot vacuum 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, {}, 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_Vacuum"
|
||||
|
||||
device_vacuum = _get_vacuum_device_cleaning()
|
||||
device_non_vacuum = _get_non_vacuum_device()
|
||||
self.hass.data[dyson.DYSON_DEVICES] = [device_vacuum,
|
||||
device_non_vacuum]
|
||||
dyson.setup_platform(self.hass, {}, _add_device)
|
||||
|
||||
def test_on_message(self):
|
||||
"""Test when message is received."""
|
||||
device = _get_vacuum_device_cleaning()
|
||||
component = Dyson360EyeDevice(device)
|
||||
component.entity_id = "entity_id"
|
||||
component.schedule_update_ha_state = mock.Mock()
|
||||
component.on_message(mock.Mock())
|
||||
self.assertTrue(component.schedule_update_ha_state.called)
|
||||
|
||||
def test_should_poll(self):
|
||||
"""Test polling is disable."""
|
||||
device = _get_vacuum_device_cleaning()
|
||||
component = Dyson360EyeDevice(device)
|
||||
self.assertFalse(component.should_poll)
|
||||
|
||||
def test_properties(self):
|
||||
"""Test component properties."""
|
||||
device1 = _get_vacuum_device_cleaning()
|
||||
device2 = _get_vacuum_device_unknown_state()
|
||||
device3 = _get_vacuum_device_charging()
|
||||
component = Dyson360EyeDevice(device1)
|
||||
component2 = Dyson360EyeDevice(device2)
|
||||
component3 = Dyson360EyeDevice(device3)
|
||||
self.assertEqual(component.name, "Device_Vacuum")
|
||||
self.assertTrue(component.is_on)
|
||||
self.assertEqual(component.icon, "mdi:roomba")
|
||||
self.assertEqual(component.status, "Cleaning")
|
||||
self.assertEqual(component2.status, "Unknown")
|
||||
self.assertEqual(component.battery_level, 85)
|
||||
self.assertEqual(component.fan_speed, "Quiet")
|
||||
self.assertEqual(component.fan_speed_list, ["Quiet", "Max"])
|
||||
self.assertEqual(component.device_state_attributes['position'],
|
||||
'(0, 0)')
|
||||
self.assertTrue(component.available)
|
||||
self.assertEqual(component.supported_features, 255)
|
||||
self.assertEqual(component.battery_icon, "mdi:battery-80")
|
||||
self.assertEqual(component3.battery_icon, "mdi:battery-charging-40")
|
||||
|
||||
def test_turn_on(self):
|
||||
"""Test turn on vacuum."""
|
||||
device1 = _get_vacuum_device_charging()
|
||||
component1 = Dyson360EyeDevice(device1)
|
||||
component1.turn_on()
|
||||
self.assertTrue(device1.start.called)
|
||||
|
||||
device2 = _get_vacuum_device_pause()
|
||||
component2 = Dyson360EyeDevice(device2)
|
||||
component2.turn_on()
|
||||
self.assertTrue(device2.resume.called)
|
||||
|
||||
def test_turn_off(self):
|
||||
"""Test turn off vacuum."""
|
||||
device1 = _get_vacuum_device_cleaning()
|
||||
component1 = Dyson360EyeDevice(device1)
|
||||
component1.turn_off()
|
||||
self.assertTrue(device1.pause.called)
|
||||
|
||||
def test_stop(self):
|
||||
"""Test stop vacuum."""
|
||||
device1 = _get_vacuum_device_cleaning()
|
||||
component1 = Dyson360EyeDevice(device1)
|
||||
component1.stop()
|
||||
self.assertTrue(device1.pause.called)
|
||||
|
||||
def test_set_fan_speed(self):
|
||||
"""Test set fan speed vacuum."""
|
||||
device1 = _get_vacuum_device_cleaning()
|
||||
component1 = Dyson360EyeDevice(device1)
|
||||
component1.set_fan_speed("Max")
|
||||
device1.set_power_mode.assert_called_with(PowerMode.MAX)
|
||||
|
||||
def test_start_pause(self):
|
||||
"""Test start/pause."""
|
||||
device1 = _get_vacuum_device_charging()
|
||||
component1 = Dyson360EyeDevice(device1)
|
||||
component1.start_pause()
|
||||
self.assertTrue(device1.start.called)
|
||||
|
||||
device2 = _get_vacuum_device_pause()
|
||||
component2 = Dyson360EyeDevice(device2)
|
||||
component2.start_pause()
|
||||
self.assertTrue(device2.resume.called)
|
||||
|
||||
device3 = _get_vacuum_device_cleaning()
|
||||
component3 = Dyson360EyeDevice(device3)
|
||||
component3.start_pause()
|
||||
self.assertTrue(device3.pause.called)
|
||||
|
||||
def test_return_to_base(self):
|
||||
"""Test return to base."""
|
||||
device = _get_vacuum_device_pause()
|
||||
component = Dyson360EyeDevice(device)
|
||||
component.return_to_base()
|
||||
self.assertTrue(device.abort.called)
|
||||
Reference in new issue
Block a user