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

1087 lines
37 KiB
Python

"""The tests for the generic_thermostat."""
import datetime
from asynctest import mock
import pytest
import pytz
import voluptuous as vol
from homeassistant.components import input_boolean, switch
from homeassistant.components.climate.const import (
ATTR_PRESET_MODE, DOMAIN, HVAC_MODE_COOL, HVAC_MODE_HEAT, HVAC_MODE_OFF,
PRESET_AWAY)
from homeassistant.const import (
ATTR_TEMPERATURE, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_OFF, STATE_ON,
TEMP_CELSIUS, TEMP_FAHRENHEIT)
import homeassistant.core as ha
from homeassistant.core import (
DOMAIN as HASS_DOMAIN, CoreState, State, callback)
from homeassistant.setup import async_setup_component
from homeassistant.util.unit_system import METRIC_SYSTEM
from tests.common import assert_setup_component, mock_restore_cache
from tests.components.climate import common
ENTITY = 'climate.test'
ENT_SENSOR = 'sensor.test'
ENT_SWITCH = 'switch.test'
HEAT_ENTITY = 'climate.test_heat'
COOL_ENTITY = 'climate.test_cool'
ATTR_AWAY_MODE = 'away_mode'
MIN_TEMP = 3.0
MAX_TEMP = 65.0
TARGET_TEMP = 42.0
COLD_TOLERANCE = 0.5
HOT_TOLERANCE = 0.5
async def test_setup_missing_conf(hass):
"""Test set up heat_control with missing config values."""
config = {
'platform': 'generic_thermostat',
'name': 'test',
'target_sensor': ENT_SENSOR
}
with assert_setup_component(0):
await async_setup_component(hass, 'climate', {
'climate': config})
async def test_valid_conf(hass):
"""Test set up generic_thermostat with valid config values."""
assert await async_setup_component(hass, 'climate', {
'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR
}})
@pytest.fixture
def setup_comp_1(hass):
"""Initialize components."""
hass.config.units = METRIC_SYSTEM
assert hass.loop.run_until_complete(
async_setup_component(hass, 'homeassistant', {})
)
async def test_heater_input_boolean(hass, setup_comp_1):
"""Test heater switching input_boolean."""
heater_switch = 'input_boolean.test'
assert await async_setup_component(hass, input_boolean.DOMAIN, {
'input_boolean': {'test': None}})
assert await async_setup_component(hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'heater': heater_switch,
'target_sensor': ENT_SENSOR,
'initial_hvac_mode': HVAC_MODE_HEAT
}})
assert STATE_OFF == \
hass.states.get(heater_switch).state
_setup_sensor(hass, 18)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 23)
assert STATE_ON == \
hass.states.get(heater_switch).state
async def test_heater_switch(hass, setup_comp_1):
"""Test heater switching test switch."""
platform = getattr(hass.components, 'test.switch')
platform.init()
switch_1 = platform.DEVICES[1]
assert await async_setup_component(hass, switch.DOMAIN, {'switch': {
'platform': 'test'}})
heater_switch = switch_1.entity_id
assert await async_setup_component(hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'heater': heater_switch,
'target_sensor': ENT_SENSOR,
'initial_hvac_mode': HVAC_MODE_HEAT
}})
await hass.async_block_till_done()
assert STATE_OFF == \
hass.states.get(heater_switch).state
_setup_sensor(hass, 18)
await common.async_set_temperature(hass, 23)
await hass.async_block_till_done()
assert STATE_ON == \
hass.states.get(heater_switch).state
def _setup_sensor(hass, temp):
"""Set up the test sensor."""
hass.states.async_set(ENT_SENSOR, temp)
@pytest.fixture
def setup_comp_2(hass):
"""Initialize components."""
hass.config.units = METRIC_SYSTEM
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 2,
'hot_tolerance': 4,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'away_temp': 16,
'initial_hvac_mode': HVAC_MODE_HEAT
}}))
async def test_setup_defaults_to_unknown(hass):
"""Test the setting of defaults to unknown."""
hass.config.units = METRIC_SYSTEM
await async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 2,
'hot_tolerance': 4,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'away_temp': 16
}})
assert HVAC_MODE_OFF == hass.states.get(ENTITY).state
async def test_default_setup_params(hass, setup_comp_2):
"""Test the setup with default parameters."""
state = hass.states.get(ENTITY)
assert 7 == state.attributes.get('min_temp')
assert 35 == state.attributes.get('max_temp')
assert 7 == state.attributes.get('temperature')
async def test_get_hvac_modes(hass, setup_comp_2):
"""Test that the operation list returns the correct modes."""
state = hass.states.get(ENTITY)
modes = state.attributes.get('hvac_modes')
assert [HVAC_MODE_HEAT, HVAC_MODE_OFF] == modes
async def test_set_target_temp(hass, setup_comp_2):
"""Test the setting of the target temperature."""
await common.async_set_temperature(hass, 30)
state = hass.states.get(ENTITY)
assert 30.0 == state.attributes.get('temperature')
with pytest.raises(vol.Invalid):
await common.async_set_temperature(hass, None)
state = hass.states.get(ENTITY)
assert 30.0 == state.attributes.get('temperature')
async def test_set_away_mode(hass, setup_comp_2):
"""Test the setting away mode."""
await common.async_set_temperature(hass, 23)
await common.async_set_preset_mode(hass, PRESET_AWAY)
state = hass.states.get(ENTITY)
assert 16 == state.attributes.get('temperature')
async def test_set_away_mode_and_restore_prev_temp(hass, setup_comp_2):
"""Test the setting and removing away mode.
Verify original temperature is restored.
"""
await common.async_set_temperature(hass, 23)
await common.async_set_preset_mode(hass, PRESET_AWAY)
state = hass.states.get(ENTITY)
assert 16 == state.attributes.get('temperature')
await common.async_set_preset_mode(hass, None)
state = hass.states.get(ENTITY)
assert 23 == state.attributes.get('temperature')
async def test_set_away_mode_twice_and_restore_prev_temp(hass, setup_comp_2):
"""Test the setting away mode twice in a row.
Verify original temperature is restored.
"""
await common.async_set_temperature(hass, 23)
await common.async_set_preset_mode(hass, PRESET_AWAY)
await common.async_set_preset_mode(hass, PRESET_AWAY)
state = hass.states.get(ENTITY)
assert 16 == state.attributes.get('temperature')
await common.async_set_preset_mode(hass, None)
state = hass.states.get(ENTITY)
assert 23 == state.attributes.get('temperature')
async def test_sensor_bad_value(hass, setup_comp_2):
"""Test sensor that have None as state."""
state = hass.states.get(ENTITY)
temp = state.attributes.get('current_temperature')
_setup_sensor(hass, None)
await hass.async_block_till_done()
state = hass.states.get(ENTITY)
assert temp == state.attributes.get('current_temperature')
async def test_set_target_temp_heater_on(hass, setup_comp_2):
"""Test if target temperature turn heater on."""
calls = _setup_switch(hass, False)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 30)
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_set_target_temp_heater_off(hass, setup_comp_2):
"""Test if target temperature turn heater off."""
calls = _setup_switch(hass, True)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 25)
assert 2 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_heater_on_within_tolerance(hass, setup_comp_2):
"""Test if temperature change doesn't turn on within tolerance."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 29)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_heater_on_outside_tolerance(hass, setup_comp_2):
"""Test if temperature change turn heater on outside cold tolerance."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 27)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_heater_off_within_tolerance(hass, setup_comp_2):
"""Test if temperature change doesn't turn off within tolerance."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 33)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_heater_off_outside_tolerance(hass, setup_comp_2):
"""Test if temperature change turn heater off outside hot tolerance."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 35)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_running_when_hvac_mode_is_off(hass, setup_comp_2):
"""Test that the switch turns off when enabled is set False."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_no_state_change_when_hvac_mode_off(hass, setup_comp_2):
"""Test that the switch doesn't turn on when enabled is False."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 30)
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_hvac_mode_heat(hass, setup_comp_2):
"""Test change mode from OFF to HEAT.
Switch turns on when temp below setpoint and mode changes.
"""
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
calls = _setup_switch(hass, False)
await common.async_set_hvac_mode(hass, HVAC_MODE_HEAT)
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
def _setup_switch(hass, is_on):
"""Set up the test switch."""
hass.states.async_set(ENT_SWITCH, STATE_ON if is_on else STATE_OFF)
calls = []
@callback
def log_call(call):
"""Log service calls."""
calls.append(call)
hass.services.async_register(ha.DOMAIN, SERVICE_TURN_ON, log_call)
hass.services.async_register(ha.DOMAIN, SERVICE_TURN_OFF, log_call)
return calls
@pytest.fixture
def setup_comp_3(hass):
"""Initialize components."""
hass.config.temperature_unit = TEMP_CELSIUS
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 2,
'hot_tolerance': 4,
'away_temp': 30,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'ac_mode': True,
'initial_hvac_mode': HVAC_MODE_COOL,
}}))
async def test_set_target_temp_ac_off(hass, setup_comp_3):
"""Test if target temperature turn ac off."""
calls = _setup_switch(hass, True)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 30)
assert 2 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_turn_away_mode_on_cooling(hass, setup_comp_3):
"""Test the setting away mode when cooling."""
_setup_switch(hass, True)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 19)
await common.async_set_preset_mode(hass, PRESET_AWAY)
state = hass.states.get(ENTITY)
assert 30 == state.attributes.get('temperature')
async def test_hvac_mode_cool(hass, setup_comp_3):
"""Test change mode from OFF to COOL.
Switch turns on when temp below setpoint and mode changes.
"""
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
calls = _setup_switch(hass, False)
await common.async_set_hvac_mode(hass, HVAC_MODE_COOL)
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_set_target_temp_ac_on(hass, setup_comp_3):
"""Test if target temperature turn ac on."""
calls = _setup_switch(hass, False)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 25)
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_ac_off_within_tolerance(hass, setup_comp_3):
"""Test if temperature change doesn't turn ac off within tolerance."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 29.8)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_set_temp_change_ac_off_outside_tolerance(hass, setup_comp_3):
"""Test if temperature change turn ac off."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 27)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_ac_on_within_tolerance(hass, setup_comp_3):
"""Test if temperature change doesn't turn ac on within tolerance."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 25.2)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_ac_on_outside_tolerance(hass, setup_comp_3):
"""Test if temperature change turn ac on."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_running_when_operating_mode_is_off_2(hass, setup_comp_3):
"""Test that the switch turns off when enabled is set False."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_no_state_change_when_operation_mode_off_2(hass, setup_comp_3):
"""Test that the switch doesn't turn on when enabled is False."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 30)
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
_setup_sensor(hass, 35)
await hass.async_block_till_done()
assert 0 == len(calls)
@pytest.fixture
def setup_comp_4(hass):
"""Initialize components."""
hass.config.temperature_unit = TEMP_CELSIUS
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 0.3,
'hot_tolerance': 0.3,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'ac_mode': True,
'min_cycle_duration': datetime.timedelta(minutes=10),
'initial_hvac_mode': HVAC_MODE_COOL,
}}))
async def test_temp_change_ac_trigger_on_not_long_enough(hass, setup_comp_4):
"""Test if temperature change turn ac on."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_ac_trigger_on_long_enough(hass, setup_comp_4):
"""Test if temperature change turn ac on."""
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11,
tzinfo=datetime.timezone.utc)
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
return_value=fake_changed):
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_ac_trigger_off_not_long_enough(hass, setup_comp_4):
"""Test if temperature change turn ac on."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_ac_trigger_off_long_enough(hass, setup_comp_4):
"""Test if temperature change turn ac on."""
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11,
tzinfo=datetime.timezone.utc)
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
return_value=fake_changed):
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_mode_change_ac_trigger_off_not_long_enough(hass, setup_comp_4):
"""Test if mode change turns ac off despite minimum cycle."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 0 == len(calls)
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
assert 1 == len(calls)
call = calls[0]
assert 'homeassistant' == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_mode_change_ac_trigger_on_not_long_enough(hass, setup_comp_4):
"""Test if mode change turns ac on despite minimum cycle."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 0 == len(calls)
await common.async_set_hvac_mode(hass, HVAC_MODE_HEAT)
assert 1 == len(calls)
call = calls[0]
assert 'homeassistant' == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
@pytest.fixture
def setup_comp_5(hass):
"""Initialize components."""
hass.config.temperature_unit = TEMP_CELSIUS
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 0.3,
'hot_tolerance': 0.3,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'ac_mode': True,
'min_cycle_duration': datetime.timedelta(minutes=10),
'initial_hvac_mode': HVAC_MODE_COOL,
}}))
async def test_temp_change_ac_trigger_on_not_long_enough_2(hass, setup_comp_5):
"""Test if temperature change turn ac on."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_ac_trigger_on_long_enough_2(hass, setup_comp_5):
"""Test if temperature change turn ac on."""
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11,
tzinfo=datetime.timezone.utc)
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
return_value=fake_changed):
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_ac_trigger_off_not_long_enough_2(
hass, setup_comp_5):
"""Test if temperature change turn ac on."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_ac_trigger_off_long_enough_2(hass, setup_comp_5):
"""Test if temperature change turn ac on."""
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11,
tzinfo=datetime.timezone.utc)
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
return_value=fake_changed):
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_mode_change_ac_trigger_off_not_long_enough_2(
hass, setup_comp_5):
"""Test if mode change turns ac off despite minimum cycle."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 0 == len(calls)
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
assert 1 == len(calls)
call = calls[0]
assert 'homeassistant' == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_mode_change_ac_trigger_on_not_long_enough_2(hass, setup_comp_5):
"""Test if mode change turns ac on despite minimum cycle."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 0 == len(calls)
await common.async_set_hvac_mode(hass, HVAC_MODE_HEAT)
assert 1 == len(calls)
call = calls[0]
assert 'homeassistant' == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
@pytest.fixture
def setup_comp_6(hass):
"""Initialize components."""
hass.config.temperature_unit = TEMP_CELSIUS
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 0.3,
'hot_tolerance': 0.3,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'min_cycle_duration': datetime.timedelta(minutes=10),
'initial_hvac_mode': HVAC_MODE_HEAT,
}}))
async def test_temp_change_heater_trigger_off_not_long_enough(
hass, setup_comp_6):
"""Test if temp change doesn't turn heater off because of time."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_heater_trigger_on_not_long_enough(
hass, setup_comp_6):
"""Test if temp change doesn't turn heater on because of time."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 0 == len(calls)
async def test_temp_change_heater_trigger_on_long_enough(hass, setup_comp_6):
"""Test if temperature change turn heater on after min cycle."""
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11,
tzinfo=datetime.timezone.utc)
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
return_value=fake_changed):
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_heater_trigger_off_long_enough(hass, setup_comp_6):
"""Test if temperature change turn heater off after min cycle."""
fake_changed = datetime.datetime(1918, 11, 11, 11, 11, 11,
tzinfo=datetime.timezone.utc)
with mock.patch('homeassistant.helpers.condition.dt_util.utcnow',
return_value=fake_changed):
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_mode_change_heater_trigger_off_not_long_enough(
hass, setup_comp_6):
"""Test if mode change turns heater off despite minimum cycle."""
calls = _setup_switch(hass, True)
await common.async_set_temperature(hass, 25)
_setup_sensor(hass, 30)
await hass.async_block_till_done()
assert 0 == len(calls)
await common.async_set_hvac_mode(hass, HVAC_MODE_OFF)
assert 1 == len(calls)
call = calls[0]
assert 'homeassistant' == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_mode_change_heater_trigger_on_not_long_enough(
hass, setup_comp_6):
"""Test if mode change turns heater on despite minimum cycle."""
calls = _setup_switch(hass, False)
await common.async_set_temperature(hass, 30)
_setup_sensor(hass, 25)
await hass.async_block_till_done()
assert 0 == len(calls)
await common.async_set_hvac_mode(hass, HVAC_MODE_HEAT)
assert 1 == len(calls)
call = calls[0]
assert 'homeassistant' == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
@pytest.fixture
def setup_comp_7(hass):
"""Initialize components."""
hass.config.temperature_unit = TEMP_CELSIUS
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 0.3,
'hot_tolerance': 0.3,
'heater': ENT_SWITCH,
'target_temp': 25,
'target_sensor': ENT_SENSOR,
'ac_mode': True,
'min_cycle_duration': datetime.timedelta(minutes=15),
'keep_alive': datetime.timedelta(minutes=10),
'initial_hvac_mode': HVAC_MODE_COOL,
}}))
async def test_temp_change_ac_trigger_on_long_enough_3(hass, setup_comp_7):
"""Test if turn on signal is sent at keep-alive intervals."""
calls = _setup_switch(hass, True)
await hass.async_block_till_done()
_setup_sensor(hass, 30)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 25)
test_time = datetime.datetime.now(pytz.UTC)
_send_time_changed(hass, test_time)
await hass.async_block_till_done()
assert 0 == len(calls)
_send_time_changed(hass, test_time + datetime.timedelta(minutes=5))
await hass.async_block_till_done()
assert 0 == len(calls)
_send_time_changed(hass, test_time + datetime.timedelta(minutes=10))
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_ac_trigger_off_long_enough_3(hass, setup_comp_7):
"""Test if turn on signal is sent at keep-alive intervals."""
calls = _setup_switch(hass, False)
await hass.async_block_till_done()
_setup_sensor(hass, 20)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 25)
test_time = datetime.datetime.now(pytz.UTC)
_send_time_changed(hass, test_time)
await hass.async_block_till_done()
assert 0 == len(calls)
_send_time_changed(hass, test_time + datetime.timedelta(minutes=5))
await hass.async_block_till_done()
assert 0 == len(calls)
_send_time_changed(hass, test_time + datetime.timedelta(minutes=10))
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
def _send_time_changed(hass, now):
"""Send a time changed event."""
hass.bus.async_fire(ha.EVENT_TIME_CHANGED, {ha.ATTR_NOW: now})
@pytest.fixture
def setup_comp_8(hass):
"""Initialize components."""
hass.config.temperature_unit = TEMP_CELSIUS
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 0.3,
'hot_tolerance': 0.3,
'target_temp': 25,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'min_cycle_duration': datetime.timedelta(minutes=15),
'keep_alive': datetime.timedelta(minutes=10),
'initial_hvac_mode': HVAC_MODE_HEAT,
}}))
async def test_temp_change_heater_trigger_on_long_enough_2(hass, setup_comp_8):
"""Test if turn on signal is sent at keep-alive intervals."""
calls = _setup_switch(hass, True)
await hass.async_block_till_done()
_setup_sensor(hass, 20)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 25)
test_time = datetime.datetime.now(pytz.UTC)
_send_time_changed(hass, test_time)
await hass.async_block_till_done()
assert 0 == len(calls)
_send_time_changed(hass, test_time + datetime.timedelta(minutes=5))
await hass.async_block_till_done()
assert 0 == len(calls)
_send_time_changed(hass, test_time + datetime.timedelta(minutes=10))
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_ON == call.service
assert ENT_SWITCH == call.data['entity_id']
async def test_temp_change_heater_trigger_off_long_enough_2(
hass, setup_comp_8):
"""Test if turn on signal is sent at keep-alive intervals."""
calls = _setup_switch(hass, False)
await hass.async_block_till_done()
_setup_sensor(hass, 30)
await hass.async_block_till_done()
await common.async_set_temperature(hass, 25)
test_time = datetime.datetime.now(pytz.UTC)
_send_time_changed(hass, test_time)
await hass.async_block_till_done()
assert 0 == len(calls)
_send_time_changed(hass, test_time + datetime.timedelta(minutes=5))
await hass.async_block_till_done()
assert 0 == len(calls)
_send_time_changed(hass, test_time + datetime.timedelta(minutes=10))
await hass.async_block_till_done()
assert 1 == len(calls)
call = calls[0]
assert HASS_DOMAIN == call.domain
assert SERVICE_TURN_OFF == call.service
assert ENT_SWITCH == call.data['entity_id']
@pytest.fixture
def setup_comp_9(hass):
"""Initialize components."""
hass.config.temperature_unit = TEMP_FAHRENHEIT
assert hass.loop.run_until_complete(async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 0.3,
'hot_tolerance': 0.3,
'target_temp': 25,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'min_cycle_duration': datetime.timedelta(minutes=15),
'keep_alive': datetime.timedelta(minutes=10),
'precision': 0.1
}}))
async def test_precision(hass, setup_comp_9):
"""Test that setting precision to tenths works as intended."""
await common.async_set_temperature(hass, 23.27)
state = hass.states.get(ENTITY)
assert 23.3 == state.attributes.get('temperature')
async def test_custom_setup_params(hass):
"""Test the setup with custom parameters."""
result = await async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'min_temp': MIN_TEMP,
'max_temp': MAX_TEMP,
'target_temp': TARGET_TEMP
}})
assert result
state = hass.states.get(ENTITY)
assert state.attributes.get('min_temp') == MIN_TEMP
assert state.attributes.get('max_temp') == MAX_TEMP
assert state.attributes.get('temperature') == TARGET_TEMP
async def test_restore_state(hass):
"""Ensure states are restored on startup."""
mock_restore_cache(hass, (
State(
'climate.test_thermostat', HVAC_MODE_OFF,
{ATTR_TEMPERATURE: "20", ATTR_PRESET_MODE: PRESET_AWAY}
),
))
hass.state = CoreState.starting
await async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test_thermostat',
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'away_temp': 14,
}})
state = hass.states.get('climate.test_thermostat')
assert(state.attributes[ATTR_TEMPERATURE] == 20)
assert(state.attributes[ATTR_PRESET_MODE] == PRESET_AWAY)
assert(state.state == HVAC_MODE_OFF)
async def test_no_restore_state(hass):
"""Ensure states are restored on startup if they exist.
Allows for graceful reboot.
"""
mock_restore_cache(hass, (
State(
'climate.test_thermostat', HVAC_MODE_OFF,
{ATTR_TEMPERATURE: "20", ATTR_PRESET_MODE: PRESET_AWAY}
),
))
hass.state = CoreState.starting
await async_setup_component(
hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test_thermostat',
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'target_temp': 22
}})
state = hass.states.get('climate.test_thermostat')
assert(state.attributes[ATTR_TEMPERATURE] == 22)
assert(state.state == HVAC_MODE_OFF)
async def test_restore_state_uncoherence_case(hass):
"""
Test restore from a strange state.
- Turn the generic thermostat off
- Restart HA and restore state from DB
"""
_mock_restore_cache(hass, temperature=20)
calls = _setup_switch(hass, False)
_setup_sensor(hass, 15)
await _setup_climate(hass, )
await hass.async_block_till_done()
state = hass.states.get(ENTITY)
assert 20 == state.attributes[ATTR_TEMPERATURE]
assert HVAC_MODE_OFF == state.state
assert 0 == len(calls)
calls = _setup_switch(hass, False)
await hass.async_block_till_done()
state = hass.states.get(ENTITY)
assert HVAC_MODE_OFF == state.state
async def _setup_climate(hass):
assert await async_setup_component(hass, DOMAIN, {'climate': {
'platform': 'generic_thermostat',
'name': 'test',
'cold_tolerance': 2,
'hot_tolerance': 4,
'away_temp': 30,
'heater': ENT_SWITCH,
'target_sensor': ENT_SENSOR,
'ac_mode': True
}})
def _mock_restore_cache(hass, temperature=20, hvac_mode=HVAC_MODE_OFF):
mock_restore_cache(hass, (
State(ENTITY, hvac_mode, {
ATTR_TEMPERATURE: str(temperature),
ATTR_PRESET_MODE: PRESET_AWAY}),
))