hass-core/tests/components/dyson/test_climate.py
Pascal Vizeli 84cf76ba36
Climate 1.0 (#23899)
* Climate 1.0 / part 1/2/3

* fix flake

* Lint

* Update Google Assistant

* ambiclimate to climate 1.0 (#24911)

* Fix Alexa

* Lint

* Migrate zhong_hong

* Migrate tuya

* Migrate honeywell to new climate schema (#24257)

* Update one

* Fix model climate v2

* Cleanup p4

* Add comfort hold mode

* Fix old code

* Update homeassistant/components/climate/__init__.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* Update homeassistant/components/climate/const.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* First renaming

* Rename operation to hvac for paulus

* Rename hold mode to preset mode

* Cleanup & update comments

* Remove on/off

* Fix supported feature count

* Update services

* Update demo

* Fix tests & use current_hvac

* Update comment

* Fix tests & add typing

* Add more typing

* Update modes

* Fix tests

* Cleanup low/high with range

* Update homematic part 1

* Finish homematic

* Fix lint

* fix hm mapping

* Support simple devices

* convert lcn

* migrate oem

* Fix xs1

* update hive

* update mil

* Update toon

* migrate deconz

* cleanup

* update tesla

* Fix lint

* Fix vera

* Migrate zwave

* Migrate velbus

* Cleanup humity feature

* Cleanup

* Migrate wink

* migrate dyson

* Fix current hvac

* Renaming

* Fix lint

* Migrate tfiac

* migrate tado

* Fix PRESET can be None

* apply PR#23913 from dev

* remove EU component, etc.

* remove EU component, etc.

* ready to test now

* de-linted

* some tweaks

* de-lint

* better handling of edge cases

* delint

* fix set_mode typos

* apply PR#23913 from dev

* remove EU component, etc.

* ready to test now

* de-linted

* some tweaks

* de-lint

* better handling of edge cases

* delint

* fix set_mode typos

* delint, move debug code

* away preset now working

* code tidy-up

* code tidy-up 2

* code tidy-up 3

* address issues #18932, #15063

* address issues #18932, #15063 - 2/2

* refactor MODE_AUTO to MODE_HEAT_COOL and use F not C

* add low/high to set_temp

* add low/high to set_temp 2

* add low/high to set_temp - delint

* run HA scripts

* port changes from PR #24402

* manual rebase

* manual rebase 2

* delint

* minor change

* remove SUPPORT_HVAC_ACTION

* Migrate radiotherm

* Convert touchline

* Migrate flexit

* Migrate nuheat

* Migrate maxcube

* Fix names maxcube const

* Migrate proliphix

* Migrate heatmiser

* Migrate fritzbox

* Migrate opentherm_gw

* Migrate venstar

* Migrate daikin

* Migrate modbus

* Fix elif

* Migrate Homematic IP Cloud to climate-1.0 (#24913)

* hmip climate fix

* Update hvac_mode and preset_mode

* fix lint

* Fix lint

* Migrate generic_thermostat

* Migrate incomfort to new climate schema (#24915)

* initial commit

* Update climate.py

* Migrate eq3btsmart

* Lint

* cleanup PRESET_MANUAL

* Migrate ecobee

* No conditional features

* KNX: Migrate climate component to new climate platform (#24931)

* Migrate climate component

* Remove unused code

* Corrected line length

* Lint

* Lint

* fix tests

* Fix value

* Migrate geniushub to new climate schema (#24191)

* Update one

* Fix model climate v2

* Cleanup p4

* Add comfort hold mode

* Fix old code

* Update homeassistant/components/climate/__init__.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* Update homeassistant/components/climate/const.py

Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io>

* First renaming

* Rename operation to hvac for paulus

* Rename hold mode to preset mode

* Cleanup & update comments

* Remove on/off

* Fix supported feature count

* Update services

* Update demo

* Fix tests & use current_hvac

* Update comment

* Fix tests & add typing

* Add more typing

* Update modes

* Fix tests

* Cleanup low/high with range

* Update homematic part 1

* Finish homematic

* Fix lint

* fix hm mapping

* Support simple devices

* convert lcn

* migrate oem

* Fix xs1

* update hive

* update mil

* Update toon

* migrate deconz

* cleanup

* update tesla

* Fix lint

* Fix vera

* Migrate zwave

* Migrate velbus

* Cleanup humity feature

* Cleanup

* Migrate wink

* migrate dyson

* Fix current hvac

* Renaming

* Fix lint

* Migrate tfiac

* migrate tado

* delinted

* delinted

* use latest client

* clean up mappings

* clean up mappings

* add duration to set_temperature

* add duration to set_temperature

* manual rebase

* tweak

* fix regression

* small fix

* fix rebase mixup

* address comments

* finish refactor

* fix regression

* tweak type hints

* delint

* manual rebase

* WIP: Fixes for honeywell migration to climate-1.0 (#24938)

* add type hints

* code tidy-up

* Fixes for incomfort migration to climate-1.0 (#24936)

* delint type hints

* no async unless await

* revert: no async unless await

* revert: no async unless await 2

* delint

* fix typo

* Fix homekit_controller on climate-1.0 (#24948)

* Fix tests on climate-1.0 branch

* As part of climate-1.0, make state return the heating-cooling.current characteristic

* Fixes from review

* lint

* Fix imports

* Migrate stibel_eltron

* Fix lint

* Migrate coolmaster to climate 1.0 (#24967)

* Migrate coolmaster to climate 1.0

* fix lint errors

* More lint fixes

* Fix demo to work with UI

* Migrate spider

* Demo update

* Updated frontend to 20190705.0

* Fix boost mode (#24980)

* Prepare Netatmo for climate 1.0 (#24973)

* Migration Netatmo

* Address comments

* Update climate.py

* Migrate ephember

* Migrate Sensibo

* Implemented review comments (#24942)

* Migrate ESPHome

* Migrate MQTT

* Migrate Nest

* Migrate melissa

* Initial/partial migration of ST

* Migrate ST

* Remove Away mode (#24995)

* Migrate evohome, cache access tokens (#24491)

* add water_heater, add storage - initial commit

* add water_heater, add storage - initial commit

delint

add missing code

desiderata

update honeywell client library & CODEOWNER

add auth_tokens code, refactor & delint

refactor for broker

delint

* Add Broker, Water Heater & Refactor

add missing code

desiderata

* update honeywell client library & CODEOWNER

add auth_tokens code, refactor & delint

refactor for broker

* bugfix - loc_idx may not be 0

more refactor - ensure pure async

more refactoring

appears all r/o attributes are working

tweak precsion, DHW & delint

remove unused code

remove unused code 2

remove unused code, refactor _save_auth_tokens()

* support RoundThermostat

bugfix opmode, switch to util.dt, add until=1h

revert breaking change

* store at_expires as naive UTC

remove debug code

delint

tidy up exception handling

delint

add water_heater, add storage - initial commit

delint

add missing code

desiderata

update honeywell client library & CODEOWNER

add auth_tokens code, refactor & delint

refactor for broker

add water_heater, add storage - initial commit

delint

add missing code

desiderata

update honeywell client library & CODEOWNER

add auth_tokens code, refactor & delint

refactor for broker

delint

bugfix - loc_idx may not be 0

more refactor - ensure pure async

more refactoring

appears all r/o attributes are working

tweak precsion, DHW & delint

remove unused code

remove unused code 2

remove unused code, refactor _save_auth_tokens()

support RoundThermostat

bugfix opmode, switch to util.dt, add until=1h

revert breaking change

store at_expires as naive UTC

remove debug code

delint

tidy up exception handling

delint

* update CODEOWNERS

* fix regression

* fix requirements

* migrate to climate-1.0

* tweaking

* de-lint

* TCS working? & delint

* tweaking

* TCS code finalised

* remove available() logic

* refactor _switchpoints()

* tidy up switchpoint code

* tweak

* teaking device_state_attributes

* some refactoring

* move PRESET_CUSTOM back to evohome

* move CONF_ACCESS_TOKEN_EXPIRES CONF_REFRESH_TOKEN back to evohome

* refactor SP code and dt conversion

* delinted

* delinted

* remove water_heater

* fix regression

* Migrate homekit

* Cleanup away mode

* Fix tests

* add helpers

* fix tests melissa

* Fix nehueat

* fix zwave

* add more tests

* fix deconz

* Fix climate test emulate_hue

* fix tests

* fix dyson tests

* fix demo with new layout

* fix honeywell

* Switch homekit_controller to use HVAC_MODE_HEAT_COOL instead of HVAC_MODE_AUTO (#25009)

* Lint

* PyLint

* Pylint

* fix fritzbox tests

* Fix google

* Fix all tests

* Fix lint

* Fix auto for homekit like controler

* Fix lint

* fix lint
2019-07-08 14:00:24 +02:00

376 lines
14 KiB
Python

"""Test the Dyson fan component."""
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
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 async_setup_component
from tests.common import get_test_home_assistant
class MockDysonState(DysonPureHotCoolState):
"""Mock Dyson state."""
def __init__(self):
"""Create new Mock Dyson State."""
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)
device.name = "Device_name"
device.state = None
device.environmental_state = None
return device
def _get_device_off():
"""Return a device with state off."""
device = mock.Mock(spec=DysonPureHotCoolLink)
device.name = "Device_name"
device.state = mock.Mock()
device.environmental_state = mock.Mock()
return device
def _get_device_focus():
"""Return a device with fan state of focus mode."""
device = mock.Mock(spec=DysonPureHotCoolLink)
device.name = "Device_name"
device.state.focus_mode = FocusMode.FOCUS_ON.value
return device
def _get_device_diffuse():
"""Return a device with fan state of diffuse mode."""
device = mock.Mock(spec=DysonPureHotCoolLink)
device.name = "Device_name"
device.state.focus_mode = FocusMode.FOCUS_OFF.value
return device
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)
device.state.heat_mode = HeatMode.HEAT_OFF.value
device.state.heat_state = HeatState.HEAT_STATE_OFF.value
device.environmental_state.temperature = 288
device.environmental_state.humidity = 53
return device
def _get_device_heat_off():
"""Return a device with state of heat reached target."""
device = mock.Mock(spec=DysonPureHotCoolLink)
device.name = "Device_name"
device.state = mock.Mock()
device.state.tilt = TiltState.TILT_FALSE.value
device.state.focus_mode = FocusMode.FOCUS_ON.value
device.state.heat_target = HeatTarget.celsius(20)
device.state.heat_mode = HeatMode.HEAT_ON.value
device.state.heat_state = HeatState.HEAT_STATE_OFF.value
device.environmental_state.temperature = 293
device.environmental_state.humidity = 53
return device
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
device.state.heat_target = HeatTarget.celsius(23)
device.state.heat_mode = HeatMode.HEAT_ON.value
device.state.heat_state = HeatState.HEAT_STATE_ON.value
device.environmental_state.temperature = 289
device.environmental_state.humidity = 53
return device
class DysonTest(unittest.TestCase):
"""Dyson Climate component test class."""
def setUp(self): # pylint: disable=invalid-name
"""Set up 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_without_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_not_called()
def test_setup_component_with_devices(self):
"""Test setup component with valid devices."""
devices = [
_get_device_with_no_state(),
_get_device_off(),
_get_device_heat_on()
]
self.hass.data[dyson.DYSON_DEVICES] = devices
add_devices = mock.MagicMock()
dyson.setup_platform(self.hass, None, add_devices, discovery_info={})
assert add_devices.called
def test_setup_component_with_invalid_devices(self):
"""Test setup component with invalid devices."""
devices = [
None,
"foo_bar"
]
self.hass.data[dyson.DYSON_DEVICES] = devices
add_devices = mock.MagicMock()
dyson.setup_platform(self.hass, None, add_devices, discovery_info={})
add_devices.assert_called_with([])
def test_setup_component(self):
"""Test setup component with devices."""
device_fan = _get_device_heat_on()
device_non_fan = _get_device_off()
def _add_device(devices):
assert len(devices) == 1
assert devices[0].name == "Device_name"
self.hass.data[dyson.DYSON_DEVICES] = [device_fan, device_non_fan]
dyson.setup_platform(self.hass, None, _add_device)
def test_dyson_set_temperature(self):
"""Test set climate temperature."""
device = _get_device_heat_on()
device.temp_unit = TEMP_CELSIUS
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert not entity.should_poll
# Without target temp.
kwargs = {}
entity.set_temperature(**kwargs)
set_config = device.set_configuration
set_config.assert_not_called()
kwargs = {ATTR_TEMPERATURE: 23}
entity.set_temperature(**kwargs)
set_config = device.set_configuration
set_config.assert_called_with(
heat_mode=HeatMode.HEAT_ON,
heat_target=HeatTarget.celsius(23))
# Should clip the target temperature between 1 and 37 inclusive.
kwargs = {ATTR_TEMPERATURE: 50}
entity.set_temperature(**kwargs)
set_config = device.set_configuration
set_config.assert_called_with(
heat_mode=HeatMode.HEAT_ON,
heat_target=HeatTarget.celsius(37))
kwargs = {ATTR_TEMPERATURE: -5}
entity.set_temperature(**kwargs)
set_config = device.set_configuration
set_config.assert_called_with(
heat_mode=HeatMode.HEAT_ON,
heat_target=HeatTarget.celsius(1))
def test_dyson_set_temperature_when_cooling_mode(self):
"""Test set climate temperature when heating is off."""
device = _get_device_cool()
device.temp_unit = TEMP_CELSIUS
entity = dyson.DysonPureHotCoolLinkDevice(device)
entity.schedule_update_ha_state = mock.Mock()
kwargs = {ATTR_TEMPERATURE: 23}
entity.set_temperature(**kwargs)
set_config = device.set_configuration
set_config.assert_called_with(
heat_mode=HeatMode.HEAT_ON,
heat_target=HeatTarget.celsius(23))
def test_dyson_set_fan_mode(self):
"""Test set fan mode."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert not entity.should_poll
entity.set_fan_mode(dyson.FAN_FOCUS)
set_config = device.set_configuration
set_config.assert_called_with(focus_mode=FocusMode.FOCUS_ON)
entity.set_fan_mode(dyson.FAN_DIFFUSE)
set_config = device.set_configuration
set_config.assert_called_with(focus_mode=FocusMode.FOCUS_OFF)
def test_dyson_fan_modes(self):
"""Test get fan list."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert len(entity.fan_modes) == 2
assert dyson.FAN_FOCUS in entity.fan_modes
assert dyson.FAN_DIFFUSE in entity.fan_modes
def test_dyson_fan_mode_focus(self):
"""Test fan focus mode."""
device = _get_device_focus()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.fan_mode == dyson.FAN_FOCUS
def test_dyson_fan_mode_diffuse(self):
"""Test fan diffuse mode."""
device = _get_device_diffuse()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.fan_mode == dyson.FAN_DIFFUSE
def test_dyson_set_hvac_mode(self):
"""Test set operation mode."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert not entity.should_poll
entity.set_hvac_mode(dyson.HVAC_MODE_HEAT)
set_config = device.set_configuration
set_config.assert_called_with(heat_mode=HeatMode.HEAT_ON)
entity.set_hvac_mode(dyson.HVAC_MODE_COOL)
set_config = device.set_configuration
set_config.assert_called_with(heat_mode=HeatMode.HEAT_OFF)
def test_dyson_operation_list(self):
"""Test get operation list."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert len(entity.hvac_modes) == 2
assert dyson.HVAC_MODE_HEAT in entity.hvac_modes
assert dyson.HVAC_MODE_COOL in entity.hvac_modes
def test_dyson_heat_off(self):
"""Test turn off heat."""
device = _get_device_heat_off()
entity = dyson.DysonPureHotCoolLinkDevice(device)
entity.set_hvac_mode(dyson.HVAC_MODE_COOL)
set_config = device.set_configuration
set_config.assert_called_with(heat_mode=HeatMode.HEAT_OFF)
def test_dyson_heat_on(self):
"""Test turn on heat."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
entity.set_hvac_mode(dyson.HVAC_MODE_HEAT)
set_config = device.set_configuration
set_config.assert_called_with(heat_mode=HeatMode.HEAT_ON)
def test_dyson_heat_value_on(self):
"""Test get heat value on."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.hvac_mode == dyson.HVAC_MODE_HEAT
def test_dyson_heat_value_off(self):
"""Test get heat value off."""
device = _get_device_cool()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.hvac_mode == dyson.HVAC_MODE_COOL
def test_dyson_heat_value_idle(self):
"""Test get heat value idle."""
device = _get_device_heat_off()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.hvac_mode == dyson.HVAC_MODE_HEAT
assert entity.hvac_action == dyson.CURRENT_HVAC_IDLE
def test_on_message(self):
"""Test when message is received."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
entity.schedule_update_ha_state = mock.Mock()
entity.on_message(MockDysonState())
entity.schedule_update_ha_state.assert_called_with()
def test_general_properties(self):
"""Test properties of entity."""
device = _get_device_with_no_state()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.should_poll is False
assert entity.supported_features == dyson.SUPPORT_FLAGS
assert entity.temperature_unit == TEMP_CELSIUS
def test_property_current_humidity(self):
"""Test properties of current humidity."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.current_humidity == 53
def test_property_current_humidity_with_invalid_env_state(self):
"""Test properties of current humidity with invalid env state."""
device = _get_device_off()
device.environmental_state.humidity = 0
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.current_humidity is None
def test_property_current_humidity_without_env_state(self):
"""Test properties of current humidity without env state."""
device = _get_device_with_no_state()
entity = dyson.DysonPureHotCoolLinkDevice(device)
assert entity.current_humidity is None
def test_property_current_temperature(self):
"""Test properties of current temperature."""
device = _get_device_heat_on()
entity = dyson.DysonPureHotCoolLinkDevice(device)
# Result should be in celsius, hence then subtraction of 273.
assert entity.current_temperature == 289 - 273
def test_property_target_temperature(self):
"""Test properties of target temperature."""
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