Add sensor support for dyson 2018 models (#22578)

fix check for already created entities

remove hepa and carbon filter

add AQI attribute

initial commit

fix check for already created entities

remove hepa and carbon filter

add AQI attribute

add air quality component tests

fix method call tests

fix line lengths

fix pylint issues

fix docstrings

revert fan related changes

remove whitespace change

fix fan update state test

add for loop for platform initialization

add requested changes to aiq platform

change string concatenation to new style string formatting

update air quality tests

update air quality tests

refactor sensor component changes

fix pylint issues

fix debug string in the air quality component

replace failing tests for older devices

fix line length fan tests

remove dependencies const and move imports

move back imports to methods

remove whitespace from blank line
This commit is contained in:
etheralm authored and Martin Hjelmare committed 2019-04-30 02:24:05 +02:00
1 parent b4e2a0ef84
commit 1d70005b01
9 files changed
+430 -112

No files matched your search

+145
View File
@@ -0,0 +1,145 @@
"""Test the Dyson air quality component."""
import json
from unittest import mock
import asynctest
from libpurecool.dyson_pure_cool import DysonPureCool
from libpurecool.dyson_pure_state_v2 import DysonEnvironmentalSensorV2State
import homeassistant.components.dyson.air_quality as dyson
from homeassistant.components import dyson as dyson_parent
from homeassistant.components.air_quality import DOMAIN as AIQ_DOMAIN, \
ATTR_PM_2_5, ATTR_PM_10, ATTR_NO2
from homeassistant.helpers import discovery
from homeassistant.setup import async_setup_component
def _get_dyson_purecool_device():
"""Return a valid device as provided by the Dyson web services."""
device = mock.Mock(spec=DysonPureCool)
device.serial = 'XX-XXXXX-XX'
device.name = 'Living room'
device.connect = mock.Mock(return_value=True)
device.auto_connect = mock.Mock(return_value=True)
device.environmental_state.particulate_matter_25 = '0014'
device.environmental_state.particulate_matter_10 = '0025'
device.environmental_state.nitrogen_dioxide = '0042'
device.environmental_state.volatile_organic_compounds = '0035'
return device
def _get_config():
"""Return a config dictionary."""
return {dyson_parent.DOMAIN: {
dyson_parent.CONF_USERNAME: 'email',
dyson_parent.CONF_PASSWORD: 'password',
dyson_parent.CONF_LANGUAGE: 'GB',
dyson_parent.CONF_DEVICES: [
{
'device_id': 'XX-XXXXX-XX',
'device_ip': '192.168.0.1'
}
]
}}
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
async def test_purecool_aiq_attributes(devices, login, hass):
"""Test state attributes."""
await async_setup_component(hass, dyson_parent.DOMAIN, _get_config())
await hass.async_block_till_done()
fan_state = hass.states.get("air_quality.living_room")
attributes = fan_state.attributes
assert fan_state.state == '14'
assert attributes[ATTR_PM_2_5] == 14
assert attributes[ATTR_PM_10] == 25
assert attributes[ATTR_NO2] == 42
assert attributes[dyson.ATTR_VOC] == 35
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
async def test_purecool_aiq_update_state(devices, login, hass):
"""Test state update."""
device = devices.return_value[0]
await async_setup_component(hass, dyson_parent.DOMAIN, _get_config())
await hass.async_block_till_done()
event = {
"msg": "ENVIRONMENTAL-CURRENT-SENSOR-DATA",
"time": "2019-03-29T10:00:01.000Z",
"data": {
"pm10": "0080",
"p10r": "0151",
"hact": "0040",
"va10": "0055",
"p25r": "0161",
"noxl": "0069",
"pm25": "0035",
"sltm": "OFF",
"tact": "2960"
}
}
device.environmental_state = \
DysonEnvironmentalSensorV2State(json.dumps(event))
callback = device.add_message_listener.call_args_list[2][0][0]
callback(device.environmental_state)
await hass.async_block_till_done()
fan_state = hass.states.get("air_quality.living_room")
attributes = fan_state.attributes
assert fan_state.state == '35'
assert attributes[ATTR_PM_2_5] == 35
assert attributes[ATTR_PM_10] == 80
assert attributes[ATTR_NO2] == 69
assert attributes[dyson.ATTR_VOC] == 55
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
async def test_purecool_component_setup_only_once(devices, login, hass):
"""Test if entities are created only once."""
config = _get_config()
await async_setup_component(hass, dyson_parent.DOMAIN, config)
await hass.async_block_till_done()
discovery.load_platform(hass, AIQ_DOMAIN,
dyson_parent.DOMAIN, {}, config)
await hass.async_block_till_done()
assert len(hass.data[dyson.DYSON_AIQ_DEVICES]) == 1
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
async def test_purecool_aiq_without_discovery(devices, login, hass):
"""Test if component correctly returns if discovery not set."""
await async_setup_component(hass, dyson_parent.DOMAIN, _get_config())
await hass.async_block_till_done()
add_entities_mock = mock.MagicMock()
dyson.setup_platform(hass, None, add_entities_mock, None)
assert add_entities_mock.call_count == 0
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
async def test_purecool_aiq_empty_environment_state(devices, login, hass):
"""Test device with empty environmental state."""
await async_setup_component(hass, dyson_parent.DOMAIN, _get_config())
await hass.async_block_till_done()
device = hass.data[dyson.DYSON_AIQ_DEVICES][0]
device._device.environmental_state = None
assert device.state is None
assert device.particulate_matter_2_5 is None
assert device.particulate_matter_10 is None
assert device.nitrogen_dioxide is None
assert device.volatile_organic_compounds is None
+35 -19
View File
@@ -2,6 +2,7 @@
import unittest
from unittest import mock
import asynctest
from libpurecool.const import (FocusMode, HeatMode,
HeatState, HeatTarget, TiltState)
from libpurecool.dyson_pure_hotcool_link import DysonPureHotCoolLink
@@ -10,7 +11,7 @@ from libpurecool.dyson_pure_state import DysonPureHotCoolState
from homeassistant.components import dyson as dyson_parent
from homeassistant.components.dyson import climate as dyson
from homeassistant.const import TEMP_CELSIUS, ATTR_TEMPERATURE
from homeassistant.setup import setup_component
from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
@@ -22,6 +23,25 @@ class MockDysonState(DysonPureHotCoolState):
pass
def _get_config():
"""Return a config dictionary."""
return {dyson_parent.DOMAIN: {
dyson_parent.CONF_USERNAME: "email",
dyson_parent.CONF_PASSWORD: "password",
dyson_parent.CONF_LANGUAGE: "GB",
dyson_parent.CONF_DEVICES: [
{
"device_id": "XX-XXXXX-XX",
"device_ip": "192.168.0.1"
},
{
"device_id": "YY-YYYYY-YY",
"device_ip": "192.168.0.2"
}
]
}}
def _get_device_with_no_state():
"""Return a device with no state."""
device = mock.Mock(spec=DysonPureHotCoolLink)
@@ -60,6 +80,7 @@ def _get_device_cool():
"""Return a device with state of cooling."""
device = mock.Mock(spec=DysonPureHotCoolLink)
device.name = "Device_name"
device.serial = "XX-XXXXX-XX"
device.state.tilt = TiltState.TILT_FALSE.value
device.state.focus_mode = FocusMode.FOCUS_OFF.value
device.state.heat_target = HeatTarget.celsius(12)
@@ -89,6 +110,7 @@ def _get_device_heat_on():
"""Return a device with state of heating."""
device = mock.Mock(spec=DysonPureHotCoolLink)
device.name = "Device_name"
device.serial = "YY-YYYYY-YY"
device.state = mock.Mock()
device.state.tilt = TiltState.TILT_FALSE.value
device.state.focus_mode = FocusMode.FOCUS_ON.value
@@ -111,24 +133,6 @@ class DysonTest(unittest.TestCase):
"""Stop everything that was started."""
self.hass.stop()
@mock.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_device_heat_on(), _get_device_cool()])
@mock.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
def test_setup_component_with_parent_discovery(self, mocked_login,
mocked_devices):
"""Test setup_component using discovery."""
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",
}
})
assert len(self.hass.data[dyson.DYSON_DEVICES]) == 2
self.hass.block_till_done()
for m in mocked_devices.return_value:
assert m.add_message_listener.called
def test_setup_component_without_devices(self):
"""Test setup component with no devices."""
self.hass.data[dyson.DYSON_DEVICES] = []
@@ -357,3 +361,15 @@ class DysonTest(unittest.TestCase):
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.target_temperature == 23
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_device_heat_on(), _get_device_cool()])
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
async def test_setup_component_with_parent_discovery(mocked_login,
mocked_devices, hass):
"""Test setup_component using discovery."""
await async_setup_component(hass, dyson_parent.DOMAIN, _get_config())
await hass.async_block_till_done()
assert len(hass.data[dyson.DYSON_DEVICES]) == 2
+34 -67
View File
@@ -13,7 +13,7 @@ from libpurecool.dyson_pure_state_v2 import DysonPureCoolV2State
import homeassistant.components.dyson.fan as dyson
from homeassistant.components import dyson as dyson_parent
from homeassistant.components.dyson import DYSON_DEVICES
from homeassistant.components.fan import (DOMAIN, ATTR_SPEED, ATTR_SPEED_LIST,
from homeassistant.components.fan import (DOMAIN, ATTR_SPEED,
ATTR_OSCILLATING, SPEED_LOW,
SPEED_MEDIUM, SPEED_HIGH,
SERVICE_OSCILLATE)
@@ -21,7 +21,7 @@ from homeassistant.const import (SERVICE_TURN_ON,
SERVICE_TURN_OFF,
ATTR_ENTITY_ID)
from homeassistant.helpers import discovery
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
@@ -55,6 +55,21 @@ def _get_dyson_purecool_device():
return device
def _get_dyson_purecoollink_device():
"""Return a valid device as provided by the Dyson web services."""
device = mock.Mock(spec=DysonPureCoolLink)
device.serial = "XX-XXXXX-XX"
device.name = "Living room"
device.connect = mock.Mock(return_value=True)
device.auto_connect = mock.Mock(return_value=True)
device.state = mock.Mock()
device.state.oscillation = "ON"
device.state.fan_mode = "FAN"
device.state.speed = FanSpeed.FAN_SPEED_AUTO.value
device.state.night_mode = "OFF"
return device
def _get_supported_speeds():
return [
int(FanSpeed.FAN_SPEED_1.value),
@@ -173,45 +188,6 @@ class DysonTest(unittest.TestCase):
"""Stop everything that was started."""
self.hass.stop()
@mock.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_device_on()])
@mock.patch('libpurecool.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(
DOMAIN,
mocked_devices.return_value[0].name))
assert dyson.ATTR_NIGHT_MODE in state.attributes
assert dyson.ATTR_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('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_device_on()])
@mock.patch('libpurecool.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()
assert 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()
@@ -415,6 +391,22 @@ class DysonTest(unittest.TestCase):
dyson_device.set_night_mode.assert_called_with(True)
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecoollink_device()])
async def test_purecoollink_attributes(devices, login, hass):
"""Test state attributes."""
await async_setup_component(hass, dyson.DYSON_DOMAIN, _get_config())
await hass.async_block_till_done()
fan_state = hass.states.get("fan.living_room")
attributes = fan_state.attributes
assert fan_state.state == "on"
assert attributes[dyson.ATTR_NIGHT_MODE] is False
assert attributes[ATTR_SPEED] == FanSpeed.FAN_SPEED_AUTO.value
assert attributes[ATTR_OSCILLATING] is True
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
@@ -670,31 +662,6 @@ async def test_purecool_set_timer(devices, login, hass):
assert device.disable_sleep_timer.call_count == 1
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
async def test_purecool_attributes(devices, login, hass):
"""Test state attributes."""
await async_setup_component(hass, dyson.DYSON_DOMAIN, _get_config())
await hass.async_block_till_done()
fan_state = hass.states.get("fan.living_room")
attributes = fan_state.attributes
assert fan_state.state == "on"
assert attributes[dyson.ATTR_NIGHT_MODE] is False
assert attributes[dyson.ATTR_AUTO_MODE] is True
assert attributes[dyson.ATTR_ANGLE_LOW] == 90
assert attributes[dyson.ATTR_ANGLE_HIGH] == 180
assert attributes[dyson.ATTR_FLOW_DIRECTION_FRONT] is True
assert attributes[dyson.ATTR_TIMER] == 60
assert attributes[dyson.ATTR_HEPA_FILTER] == 90
assert attributes[dyson.ATTR_CARBON_FILTER] == 80
assert attributes[dyson.ATTR_DYSON_SPEED] == FanSpeed.FAN_SPEED_AUTO.value
assert attributes[ATTR_SPEED] == SPEED_MEDIUM
assert attributes[ATTR_OSCILLATING] is True
assert attributes[dyson.ATTR_DYSON_SPEED_LIST] == _get_supported_speeds()
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
@@ -713,7 +680,7 @@ async def test_purecool_update_state(devices, login, hass):
"osau": "0095", "ancp": "CUST"}}
device.state = DysonPureCoolV2State(json.dumps(event))
callback = device.add_message_listener.call_args_list[0][0][0]
callback = device.add_message_listener.call_args_list[3][0][0]
callback(device.state)
await hass.async_block_till_done()
fan_state = hass.states.get("fan.living_room")
+2 -2
View File
@@ -87,7 +87,7 @@ class DysonTest(unittest.TestCase):
assert mocked_login.call_count == 1
assert mocked_devices.call_count == 1
assert len(self.hass.data[dyson.DYSON_DEVICES]) == 1
assert mocked_discovery.call_count == 4
assert mocked_discovery.call_count == 5
@mock.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_account_device_not_available()])
@@ -172,7 +172,7 @@ class DysonTest(unittest.TestCase):
assert mocked_login.call_count == 1
assert mocked_devices.call_count == 1
assert len(self.hass.data[dyson.DYSON_DEVICES]) == 1
assert mocked_discovery.call_count == 4
assert mocked_discovery.call_count == 5
@mock.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_account_device_not_available()])
+52 -4
View File
@@ -2,17 +2,51 @@
import unittest
from unittest import mock
import asynctest
from libpurecool.dyson_pure_cool import DysonPureCool
from libpurecool.dyson_pure_cool_link import DysonPureCoolLink
from homeassistant.components import dyson as dyson_parent
from homeassistant.components.dyson import sensor as dyson
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT, \
STATE_OFF
from homeassistant.helpers import discovery
from homeassistant.setup import async_setup_component
from tests.common import get_test_home_assistant
def _get_dyson_purecool_device():
"""Return a valid device provide by Dyson web services."""
device = mock.Mock(spec=DysonPureCool)
device.serial = "XX-XXXXX-XX"
device.name = "Living room"
device.connect = mock.Mock(return_value=True)
device.auto_connect = mock.Mock(return_value=True)
device.environmental_state.humidity = 42
device.environmental_state.temperature = 280
device.state.hepa_filter_state = 90
device.state.carbon_filter_state = 80
return device
def _get_config():
"""Return a config dictionary."""
return {dyson_parent.DOMAIN: {
dyson_parent.CONF_USERNAME: "email",
dyson_parent.CONF_PASSWORD: "password",
dyson_parent.CONF_LANGUAGE: "GB",
dyson_parent.CONF_DEVICES: [
{
"device_id": "XX-XXXXX-XX",
"device_ip": "192.168.0.1"
}
]
}}
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
@@ -21,7 +55,7 @@ def _get_device_without_state():
def _get_with_state():
"""Return a valid device with state values."""
device = mock.Mock(spec=DysonPureCoolLink)
device = mock.Mock()
device.name = "Device_name"
device.state = mock.Mock()
device.state.filter_life = 100
@@ -65,7 +99,7 @@ class DysonTest(unittest.TestCase):
self.hass.data[dyson.DYSON_DEVICES] = []
add_entities = mock.MagicMock()
dyson.setup_platform(self.hass, None, add_entities)
add_entities.assert_called_with([])
add_entities.assert_not_called()
def test_setup_component(self):
"""Test setup component with devices."""
@@ -80,7 +114,7 @@ class DysonTest(unittest.TestCase):
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)
dyson.setup_platform(self.hass, None, _add_device, mock.MagicMock())
def test_dyson_filter_life_sensor(self):
"""Test filter life sensor with no value."""
@@ -228,3 +262,17 @@ class DysonTest(unittest.TestCase):
assert sensor.unit_of_measurement is None
assert sensor.name == "Device_name AQI"
assert sensor.entity_id == "sensor.dyson_1"
@asynctest.patch('libpurecool.dyson.DysonAccount.login', return_value=True)
@asynctest.patch('libpurecool.dyson.DysonAccount.devices',
return_value=[_get_dyson_purecool_device()])
async def test_purecool_component_setup_only_once(devices, login, hass):
"""Test if entities are created only once."""
config = _get_config()
await async_setup_component(hass, dyson_parent.DOMAIN, config)
await hass.async_block_till_done()
discovery.load_platform(hass, "sensor", dyson_parent.DOMAIN, {}, config)
await hass.async_block_till_done()
assert len(hass.data[dyson.DYSON_SENSOR_DEVICES]) == 2