mirror of
https://github.com/home-assistant/core.git
synced 2025-06-05 05:37:04 +00:00

Add unit symbol constants Initial unit system object Import more constants Pydoc for unit system file Import constants for configuration validation Unit system validation method Typing for constants Inches are valid lengths too Typings Change base class to dict - needed for remote api call serialization Validation Use dictionary keys Defined unit systems Update location util to use metric instead of us fahrenheit Update constant imports Import defined unit systems Update configuration to use unit system Update schema to use unit system Update constants Add imports to core for unit system and distance Type for config Default unit system Convert distance from HASS instance Update temperature conversion to use unit system Update temperature conversion Set unit system based on configuration Set info unit system Return unit system dictionary with config dictionary Auto discover unit system Update location test for use metric Update forecast unit system Update mold indicator unit system Update thermostat unit system Update thermostat demo test Unit tests around unit system Update test common hass configuration Update configuration unit tests There should always be a unit system! Update core unit tests Constants typing Linting issues Remove unused import Update fitbit sensor to use application unit system Update google travel time to use application unit system Update configuration example Update dht sensor Update DHT temperature conversion to use the utility function Update swagger config Update my sensors metric flag Update hvac component temperature conversion HVAC conversion for temperature Pull unit from sensor type map Pull unit from sensor type map Update the temper sensor unit Update yWeather sensor unit Update hvac demo unit test Set unit test config unit system to metric Use hass unit system length for default in proximity Use the name of the system instead of temperature Use constants from const Unused import Forecasted temperature Fix calculation in case furthest distance is greater than 1000000 units Remove unneeded constants Set default length to km or miles Use constants Linting doesn't like importing just for typing Fix reference Test is expecting meters - set config to meters Use constant Use constant PyDoc for unit test Should be not in Rename to units Change unit system to be an object - not a dictionary Return tuple in conversion Move convert to temperature util Temperature conversion is now in unit system Update imports Rename to units Units is now an object Use temperature util conversion Unit system is now an object Validate and convert unit system config Return the scalar value in template distance Test is expecting meters Update unit tests around unit system Distance util returns tuple Fix location info test Set units Update unit tests Convert distance DOH Pull out the scalar from the vector Linting I really hate python linting Linting again BLARG Unit test documentation Unit test around is metric flag Break ternary statement into if/else blocks Don't use dictionary - use members is metric flag Rename constants Use is metric flag Move constants to CONST file Move to const file Raise error if unit is not expected Typing No need to return unit since only performing conversion if it can work Use constants Line wrapping Raise error if invalid value Remove subscripts from conversion as they are no longer returned as tuples No longer tuples No longer tuples Check for numeric type Fix string format to use correct variable Typing Assert errors raised Remove subscript Only convert temperature if we know the unit If no unit of measurement set - default to HASS config Convert only if we know the unit Remove subscription Fix not in clause Linting fixes Wants a boolean Clearer if-block Check if the key is in the config first Missed a couple expecting tuples Backwards compatibility No like-y ternary! Error handling around state setting Pretty unit system configuration validation More tuple crap Use is metric flag Error handling around min/max temp Explode if no unit Pull unit from config Celsius has a decimal Unused import Check if it's a temperature before we try to convert it to a temperature Linting says too many statements - combine lat/long in a fairly reasonable manner Backwards compatibility unit test Better doc
300 lines
11 KiB
Python
300 lines
11 KiB
Python
"""The tests for the heat control thermostat."""
|
|
import unittest
|
|
|
|
from homeassistant.bootstrap import _setup_component
|
|
from homeassistant.const import (
|
|
ATTR_UNIT_OF_MEASUREMENT,
|
|
SERVICE_TURN_OFF,
|
|
SERVICE_TURN_ON,
|
|
STATE_ON,
|
|
STATE_OFF,
|
|
TEMP_CELSIUS,
|
|
)
|
|
from homeassistant.helpers.unit_system import METRIC_SYSTEM
|
|
from homeassistant.components import thermostat
|
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
ENTITY = 'thermostat.test'
|
|
ENT_SENSOR = 'sensor.test'
|
|
ENT_SWITCH = 'switch.test'
|
|
MIN_TEMP = 3.0
|
|
MAX_TEMP = 65.0
|
|
TARGET_TEMP = 42.0
|
|
|
|
|
|
class TestSetupThermostatHeatControl(unittest.TestCase):
|
|
"""Test the Heat Control thermostat with custom config."""
|
|
|
|
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 down everything that was started."""
|
|
self.hass.stop()
|
|
|
|
def test_setup_missing_conf(self):
|
|
"""Test set up heat_control with missing config values."""
|
|
config = {
|
|
'name': 'test',
|
|
'target_sensor': ENT_SENSOR
|
|
}
|
|
self.assertFalse(_setup_component(self.hass, 'thermostat', {
|
|
'thermostat': config}))
|
|
|
|
def test_valid_conf(self):
|
|
"""Test set up heat_control with valid config values."""
|
|
self.assertTrue(_setup_component(self.hass, 'thermostat',
|
|
{'thermostat': {
|
|
'platform': 'heat_control',
|
|
'name': 'test',
|
|
'heater': ENT_SWITCH,
|
|
'target_sensor': ENT_SENSOR}}))
|
|
|
|
def test_setup_with_sensor(self):
|
|
"""Test set up heat_control with sensor to trigger update at init."""
|
|
self.hass.states.set(ENT_SENSOR, 22.0, {
|
|
ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS
|
|
})
|
|
thermostat.setup(self.hass, {'thermostat': {
|
|
'platform': 'heat_control',
|
|
'name': 'test',
|
|
'heater': ENT_SWITCH,
|
|
'target_sensor': ENT_SENSOR
|
|
}})
|
|
state = self.hass.states.get(ENTITY)
|
|
self.assertEqual(
|
|
TEMP_CELSIUS, state.attributes.get('unit_of_measurement'))
|
|
self.assertEqual(22.0, state.attributes.get('current_temperature'))
|
|
|
|
|
|
class TestThermostatHeatControl(unittest.TestCase):
|
|
"""Test the Heat Control thermostat."""
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
"""Setup things to be run when tests are started."""
|
|
self.hass = get_test_home_assistant()
|
|
self.hass.config.units = METRIC_SYSTEM
|
|
thermostat.setup(self.hass, {'thermostat': {
|
|
'platform': 'heat_control',
|
|
'name': 'test',
|
|
'heater': ENT_SWITCH,
|
|
'target_sensor': ENT_SENSOR
|
|
}})
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
|
"""Stop down everything that was started."""
|
|
self.hass.stop()
|
|
|
|
def test_setup_defaults_to_unknown(self):
|
|
"""Test the setting of defaults to unknown."""
|
|
self.assertEqual('unknown', self.hass.states.get(ENTITY).state)
|
|
|
|
def test_default_setup_params(self):
|
|
"""Test the setup with default parameters."""
|
|
state = self.hass.states.get(ENTITY)
|
|
self.assertEqual(7, state.attributes.get('min_temp'))
|
|
self.assertEqual(35, state.attributes.get('max_temp'))
|
|
self.assertEqual(None, state.attributes.get('temperature'))
|
|
|
|
def test_custom_setup_params(self):
|
|
"""Test the setup with custom parameters."""
|
|
thermostat.setup(self.hass, {'thermostat': {
|
|
'platform': 'heat_control',
|
|
'name': 'test',
|
|
'heater': ENT_SWITCH,
|
|
'target_sensor': ENT_SENSOR,
|
|
'min_temp': MIN_TEMP,
|
|
'max_temp': MAX_TEMP,
|
|
'target_temp': TARGET_TEMP
|
|
}})
|
|
state = self.hass.states.get(ENTITY)
|
|
self.assertEqual(MIN_TEMP, state.attributes.get('min_temp'))
|
|
self.assertEqual(MAX_TEMP, state.attributes.get('max_temp'))
|
|
self.assertEqual(TARGET_TEMP, state.attributes.get('temperature'))
|
|
self.assertEqual(str(TARGET_TEMP), self.hass.states.get(ENTITY).state)
|
|
|
|
def test_set_target_temp(self):
|
|
"""Test the setting of the target temperature."""
|
|
thermostat.set_temperature(self.hass, 30)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual('30.0', self.hass.states.get(ENTITY).state)
|
|
|
|
def test_sensor_bad_unit(self):
|
|
"""Test sensor that have bad unit."""
|
|
self._setup_sensor(22.0, unit='bad_unit')
|
|
self.hass.pool.block_till_done()
|
|
state = self.hass.states.get(ENTITY)
|
|
self.assertEqual(None, state.attributes.get('unit_of_measurement'))
|
|
self.assertEqual(None, state.attributes.get('current_temperature'))
|
|
|
|
def test_sensor_bad_value(self):
|
|
"""Test sensor that have None as state."""
|
|
self._setup_sensor(None)
|
|
self.hass.pool.block_till_done()
|
|
state = self.hass.states.get(ENTITY)
|
|
self.assertEqual(None, state.attributes.get('unit_of_measurement'))
|
|
self.assertEqual(None, state.attributes.get('current_temperature'))
|
|
|
|
def test_set_target_temp_heater_on(self):
|
|
"""Test if target temperature turn heater on."""
|
|
self._setup_switch(False)
|
|
self._setup_sensor(25)
|
|
self.hass.pool.block_till_done()
|
|
thermostat.set_temperature(self.hass, 30)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
call = self.calls[0]
|
|
self.assertEqual('switch', call.domain)
|
|
self.assertEqual(SERVICE_TURN_ON, call.service)
|
|
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
|
|
|
def test_set_target_temp_heater_off(self):
|
|
"""Test if target temperature turn heater off."""
|
|
self._setup_switch(True)
|
|
self._setup_sensor(30)
|
|
self.hass.pool.block_till_done()
|
|
thermostat.set_temperature(self.hass, 25)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
call = self.calls[0]
|
|
self.assertEqual('switch', call.domain)
|
|
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
|
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
|
|
|
def test_set_temp_change_heater_on(self):
|
|
"""Test if temperature change turn heater on."""
|
|
self._setup_switch(False)
|
|
thermostat.set_temperature(self.hass, 30)
|
|
self.hass.pool.block_till_done()
|
|
self._setup_sensor(25)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
call = self.calls[0]
|
|
self.assertEqual('switch', call.domain)
|
|
self.assertEqual(SERVICE_TURN_ON, call.service)
|
|
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
|
|
|
def test_temp_change_heater_off(self):
|
|
"""Test if temperature change turn heater off."""
|
|
self._setup_switch(True)
|
|
thermostat.set_temperature(self.hass, 25)
|
|
self.hass.pool.block_till_done()
|
|
self._setup_sensor(30)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
call = self.calls[0]
|
|
self.assertEqual('switch', call.domain)
|
|
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
|
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
|
|
|
def _setup_sensor(self, temp, unit=TEMP_CELSIUS):
|
|
"""Setup the test sensor."""
|
|
self.hass.states.set(ENT_SENSOR, temp, {
|
|
ATTR_UNIT_OF_MEASUREMENT: unit
|
|
})
|
|
|
|
def _setup_switch(self, is_on):
|
|
"""Setup the test switch."""
|
|
self.hass.states.set(ENT_SWITCH, STATE_ON if is_on else STATE_OFF)
|
|
self.calls = []
|
|
|
|
def log_call(call):
|
|
"""Log service calls."""
|
|
self.calls.append(call)
|
|
|
|
self.hass.services.register('switch', SERVICE_TURN_ON, log_call)
|
|
self.hass.services.register('switch', SERVICE_TURN_OFF, log_call)
|
|
|
|
|
|
class TestThermostatHeatControlACMode(unittest.TestCase):
|
|
"""Test the Heat Control thermostat."""
|
|
|
|
def setUp(self): # pylint: disable=invalid-name
|
|
"""Setup things to be run when tests are started."""
|
|
self.hass = get_test_home_assistant()
|
|
self.hass.config.temperature_unit = TEMP_CELSIUS
|
|
thermostat.setup(self.hass, {'thermostat': {
|
|
'platform': 'heat_control',
|
|
'name': 'test',
|
|
'heater': ENT_SWITCH,
|
|
'target_sensor': ENT_SENSOR,
|
|
'ac_mode': True
|
|
}})
|
|
|
|
def tearDown(self): # pylint: disable=invalid-name
|
|
"""Stop down everything that was started."""
|
|
self.hass.stop()
|
|
|
|
def test_set_target_temp_ac_off(self):
|
|
"""Test if target temperature turn ac off."""
|
|
self._setup_switch(True)
|
|
self._setup_sensor(25)
|
|
self.hass.pool.block_till_done()
|
|
thermostat.set_temperature(self.hass, 30)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
call = self.calls[0]
|
|
self.assertEqual('switch', call.domain)
|
|
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
|
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
|
|
|
def test_set_target_temp_ac_on(self):
|
|
"""Test if target temperature turn ac on."""
|
|
self._setup_switch(False)
|
|
self._setup_sensor(30)
|
|
self.hass.pool.block_till_done()
|
|
thermostat.set_temperature(self.hass, 25)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
call = self.calls[0]
|
|
self.assertEqual('switch', call.domain)
|
|
self.assertEqual(SERVICE_TURN_ON, call.service)
|
|
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
|
|
|
def test_set_temp_change_ac_off(self):
|
|
"""Test if temperature change turn ac off."""
|
|
self._setup_switch(True)
|
|
thermostat.set_temperature(self.hass, 30)
|
|
self.hass.pool.block_till_done()
|
|
self._setup_sensor(25)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
call = self.calls[0]
|
|
self.assertEqual('switch', call.domain)
|
|
self.assertEqual(SERVICE_TURN_OFF, call.service)
|
|
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
|
|
|
def test_temp_change_ac_on(self):
|
|
"""Test if temperature change turn ac on."""
|
|
self._setup_switch(False)
|
|
thermostat.set_temperature(self.hass, 25)
|
|
self.hass.pool.block_till_done()
|
|
self._setup_sensor(30)
|
|
self.hass.pool.block_till_done()
|
|
self.assertEqual(1, len(self.calls))
|
|
call = self.calls[0]
|
|
self.assertEqual('switch', call.domain)
|
|
self.assertEqual(SERVICE_TURN_ON, call.service)
|
|
self.assertEqual(ENT_SWITCH, call.data['entity_id'])
|
|
|
|
def _setup_sensor(self, temp, unit=TEMP_CELSIUS):
|
|
"""Setup the test sensor."""
|
|
self.hass.states.set(ENT_SENSOR, temp, {
|
|
ATTR_UNIT_OF_MEASUREMENT: unit
|
|
})
|
|
|
|
def _setup_switch(self, is_on):
|
|
"""Setup the test switch."""
|
|
self.hass.states.set(ENT_SWITCH, STATE_ON if is_on else STATE_OFF)
|
|
self.calls = []
|
|
|
|
def log_call(call):
|
|
"""Log service calls."""
|
|
self.calls.append(call)
|
|
|
|
self.hass.services.register('switch', SERVICE_TURN_ON, log_call)
|
|
self.hass.services.register('switch', SERVICE_TURN_OFF, log_call)
|