diff --git a/homeassistant/components/alarm_control_panel/__init__.py b/homeassistant/components/alarm_control_panel/__init__.py index 63977ed88c7..c5110a2ad5a 100644 --- a/homeassistant/components/alarm_control_panel/__init__.py +++ b/homeassistant/components/alarm_control_panel/__init__.py @@ -14,7 +14,6 @@ from homeassistant.const import ( ATTR_CODE, ATTR_CODE_FORMAT, ATTR_ENTITY_ID, SERVICE_ALARM_TRIGGER, SERVICE_ALARM_DISARM, SERVICE_ALARM_ARM_HOME, SERVICE_ALARM_ARM_AWAY, SERVICE_ALARM_ARM_NIGHT, SERVICE_ALARM_ARM_CUSTOM_BYPASS) -from homeassistant.loader import bind_hass from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity @@ -32,78 +31,6 @@ ALARM_SERVICE_SCHEMA = vol.Schema({ }) -@bind_hass -def alarm_disarm(hass, code=None, entity_id=None): - """Send the alarm the command for disarm.""" - data = {} - if code: - data[ATTR_CODE] = code - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_ALARM_DISARM, data) - - -@bind_hass -def alarm_arm_home(hass, code=None, entity_id=None): - """Send the alarm the command for arm home.""" - data = {} - if code: - data[ATTR_CODE] = code - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_ALARM_ARM_HOME, data) - - -@bind_hass -def alarm_arm_away(hass, code=None, entity_id=None): - """Send the alarm the command for arm away.""" - data = {} - if code: - data[ATTR_CODE] = code - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_ALARM_ARM_AWAY, data) - - -@bind_hass -def alarm_arm_night(hass, code=None, entity_id=None): - """Send the alarm the command for arm night.""" - data = {} - if code: - data[ATTR_CODE] = code - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_ALARM_ARM_NIGHT, data) - - -@bind_hass -def alarm_trigger(hass, code=None, entity_id=None): - """Send the alarm the command for trigger.""" - data = {} - if code: - data[ATTR_CODE] = code - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_ALARM_TRIGGER, data) - - -@bind_hass -def alarm_arm_custom_bypass(hass, code=None, entity_id=None): - """Send the alarm the command for arm custom bypass.""" - data = {} - if code: - data[ATTR_CODE] = code - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_ALARM_ARM_CUSTOM_BYPASS, data) - - @asyncio.coroutine def async_setup(hass, config): """Track states and offer events for sensors.""" diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 43fd4cedb88..e657409ea01 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -115,34 +115,6 @@ def is_on(hass, entity_id): return hass.states.is_state(entity_id, STATE_ON) -@bind_hass -def turn_on(hass, entity_id=None): - """Turn on specified automation or all.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - hass.services.call(DOMAIN, SERVICE_TURN_ON, data) - - -@bind_hass -def turn_off(hass, entity_id=None): - """Turn off specified automation or all.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) - - -@bind_hass -def toggle(hass, entity_id=None): - """Toggle specified automation or all.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - hass.services.call(DOMAIN, SERVICE_TOGGLE, data) - - -@bind_hass -def trigger(hass, entity_id=None): - """Trigger specified automation or all.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - hass.services.call(DOMAIN, SERVICE_TRIGGER, data) - - @bind_hass def reload(hass): """Reload the automation from config.""" diff --git a/homeassistant/components/camera/__init__.py b/homeassistant/components/camera/__init__.py index a8a486013d4..b32236e499d 100644 --- a/homeassistant/components/camera/__init__.py +++ b/homeassistant/components/camera/__init__.py @@ -83,62 +83,6 @@ class Image: content = attr.ib(type=bytes) -@bind_hass -def turn_off(hass, entity_id=None): - """Turn off camera.""" - hass.add_job(async_turn_off, hass, entity_id) - - -@bind_hass -async def async_turn_off(hass, entity_id=None): - """Turn off camera.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data) - - -@bind_hass -def turn_on(hass, entity_id=None): - """Turn on camera.""" - hass.add_job(async_turn_on, hass, entity_id) - - -@bind_hass -async def async_turn_on(hass, entity_id=None): - """Turn on camera, and set operation mode.""" - data = {} - if entity_id is not None: - data[ATTR_ENTITY_ID] = entity_id - - await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data) - - -@bind_hass -def enable_motion_detection(hass, entity_id=None): - """Enable Motion Detection.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else None - hass.async_add_job(hass.services.async_call( - DOMAIN, SERVICE_ENABLE_MOTION, data)) - - -@bind_hass -def disable_motion_detection(hass, entity_id=None): - """Disable Motion Detection.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else None - hass.async_add_job(hass.services.async_call( - DOMAIN, SERVICE_DISABLE_MOTION, data)) - - -@bind_hass -@callback -def async_snapshot(hass, filename, entity_id=None): - """Make a snapshot from a camera.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - data[ATTR_FILENAME] = filename - - hass.async_add_job(hass.services.async_call( - DOMAIN, SERVICE_SNAPSHOT, data)) - - @bind_hass async def async_get_image(hass, entity_id, timeout=10): """Fetch an image from a camera entity.""" diff --git a/homeassistant/components/climate/__init__.py b/homeassistant/components/climate/__init__.py index a3273f67cc2..98483c454bc 100644 --- a/homeassistant/components/climate/__init__.py +++ b/homeassistant/components/climate/__init__.py @@ -10,7 +10,6 @@ import functools as ft import voluptuous as vol -from homeassistant.loader import bind_hass from homeassistant.helpers.temperature import display_temp as show_temp from homeassistant.util.temperature import convert as convert_temperature from homeassistant.helpers.entity_component import EntityComponent @@ -20,7 +19,7 @@ import homeassistant.helpers.config_validation as cv from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_TEMPERATURE, SERVICE_TURN_ON, SERVICE_TURN_OFF, STATE_ON, STATE_OFF, STATE_UNKNOWN, TEMP_CELSIUS, PRECISION_WHOLE, - PRECISION_TENTHS, ) + PRECISION_TENTHS) DEFAULT_MIN_TEMP = 7 DEFAULT_MAX_TEMP = 35 @@ -142,107 +141,6 @@ SET_SWING_MODE_SCHEMA = vol.Schema({ }) -@bind_hass -def set_away_mode(hass, away_mode, entity_id=None): - """Turn all or specified climate devices away mode on.""" - data = { - ATTR_AWAY_MODE: away_mode - } - - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_SET_AWAY_MODE, data) - - -@bind_hass -def set_hold_mode(hass, hold_mode, entity_id=None): - """Set new hold mode.""" - data = { - ATTR_HOLD_MODE: hold_mode - } - - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_SET_HOLD_MODE, data) - - -@bind_hass -def set_aux_heat(hass, aux_heat, entity_id=None): - """Turn all or specified climate devices auxiliary heater on.""" - data = { - ATTR_AUX_HEAT: aux_heat - } - - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_SET_AUX_HEAT, data) - - -@bind_hass -def set_temperature(hass, temperature=None, entity_id=None, - target_temp_high=None, target_temp_low=None, - operation_mode=None): - """Set new target temperature.""" - kwargs = { - key: value for key, value in [ - (ATTR_TEMPERATURE, temperature), - (ATTR_TARGET_TEMP_HIGH, target_temp_high), - (ATTR_TARGET_TEMP_LOW, target_temp_low), - (ATTR_ENTITY_ID, entity_id), - (ATTR_OPERATION_MODE, operation_mode) - ] if value is not None - } - _LOGGER.debug("set_temperature start data=%s", kwargs) - hass.services.call(DOMAIN, SERVICE_SET_TEMPERATURE, kwargs) - - -@bind_hass -def set_humidity(hass, humidity, entity_id=None): - """Set new target humidity.""" - data = {ATTR_HUMIDITY: humidity} - - if entity_id is not None: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_SET_HUMIDITY, data) - - -@bind_hass -def set_fan_mode(hass, fan, entity_id=None): - """Set all or specified climate devices fan mode on.""" - data = {ATTR_FAN_MODE: fan} - - if entity_id: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_SET_FAN_MODE, data) - - -@bind_hass -def set_operation_mode(hass, operation_mode, entity_id=None): - """Set new target operation mode.""" - data = {ATTR_OPERATION_MODE: operation_mode} - - if entity_id is not None: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_SET_OPERATION_MODE, data) - - -@bind_hass -def set_swing_mode(hass, swing_mode, entity_id=None): - """Set new target swing mode.""" - data = {ATTR_SWING_MODE: swing_mode} - - if entity_id is not None: - data[ATTR_ENTITY_ID] = entity_id - - hass.services.call(DOMAIN, SERVICE_SET_SWING_MODE, data) - - async def async_setup(hass, config): """Set up climate devices.""" component = hass.data[DOMAIN] = \ diff --git a/homeassistant/components/counter/__init__.py b/homeassistant/components/counter/__init__.py index d720819a0ab..9ef4d4374ce 100644 --- a/homeassistant/components/counter/__init__.py +++ b/homeassistant/components/counter/__init__.py @@ -9,12 +9,10 @@ import logging import voluptuous as vol from homeassistant.const import ATTR_ENTITY_ID, CONF_ICON, CONF_NAME -from homeassistant.core import callback import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.restore_state import async_get_last_state -from homeassistant.loader import bind_hass _LOGGER = logging.getLogger(__name__) @@ -51,48 +49,6 @@ CONFIG_SCHEMA = vol.Schema({ }, extra=vol.ALLOW_EXTRA) -@bind_hass -def increment(hass, entity_id): - """Increment a counter.""" - hass.add_job(async_increment, hass, entity_id) - - -@callback -@bind_hass -def async_increment(hass, entity_id): - """Increment a counter.""" - hass.async_add_job(hass.services.async_call( - DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id})) - - -@bind_hass -def decrement(hass, entity_id): - """Decrement a counter.""" - hass.add_job(async_decrement, hass, entity_id) - - -@callback -@bind_hass -def async_decrement(hass, entity_id): - """Decrement a counter.""" - hass.async_add_job(hass.services.async_call( - DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id})) - - -@bind_hass -def reset(hass, entity_id): - """Reset a counter.""" - hass.add_job(async_reset, hass, entity_id) - - -@callback -@bind_hass -def async_reset(hass, entity_id): - """Reset a counter.""" - hass.async_add_job(hass.services.async_call( - DOMAIN, SERVICE_RESET, {ATTR_ENTITY_ID: entity_id})) - - async def async_setup(hass, config): """Set up the counters.""" component = EntityComponent(_LOGGER, DOMAIN, hass) diff --git a/homeassistant/components/fan/__init__.py b/homeassistant/components/fan/__init__.py index f2704e84bc5..ec18637f065 100644 --- a/homeassistant/components/fan/__init__.py +++ b/homeassistant/components/fan/__init__.py @@ -98,77 +98,6 @@ def is_on(hass, entity_id: str = None) -> bool: return state.attributes[ATTR_SPEED] not in [SPEED_OFF, STATE_UNKNOWN] -@bind_hass -def turn_on(hass, entity_id: str = None, speed: str = None) -> None: - """Turn all or specified fan on.""" - data = { - key: value for key, value in [ - (ATTR_ENTITY_ID, entity_id), - (ATTR_SPEED, speed), - ] if value is not None - } - - hass.services.call(DOMAIN, SERVICE_TURN_ON, data) - - -@bind_hass -def turn_off(hass, entity_id: str = None) -> None: - """Turn all or specified fan off.""" - data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} - - hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) - - -@bind_hass -def toggle(hass, entity_id: str = None) -> None: - """Toggle all or specified fans.""" - data = { - ATTR_ENTITY_ID: entity_id - } - - hass.services.call(DOMAIN, SERVICE_TOGGLE, data) - - -@bind_hass -def oscillate(hass, entity_id: str = None, - should_oscillate: bool = True) -> None: - """Set oscillation on all or specified fan.""" - data = { - key: value for key, value in [ - (ATTR_ENTITY_ID, entity_id), - (ATTR_OSCILLATING, should_oscillate), - ] if value is not None - } - - hass.services.call(DOMAIN, SERVICE_OSCILLATE, data) - - -@bind_hass -def set_speed(hass, entity_id: str = None, speed: str = None) -> None: - """Set speed for all or specified fan.""" - data = { - key: value for key, value in [ - (ATTR_ENTITY_ID, entity_id), - (ATTR_SPEED, speed), - ] if value is not None - } - - hass.services.call(DOMAIN, SERVICE_SET_SPEED, data) - - -@bind_hass -def set_direction(hass, entity_id: str = None, direction: str = None) -> None: - """Set direction for all or specified fan.""" - data = { - key: value for key, value in [ - (ATTR_ENTITY_ID, entity_id), - (ATTR_DIRECTION, direction), - ] if value is not None - } - - hass.services.call(DOMAIN, SERVICE_SET_DIRECTION, data) - - @asyncio.coroutine def async_setup(hass, config: dict): """Expose fan control via statemachine and services.""" diff --git a/tests/components/alarm_control_panel/common.py b/tests/components/alarm_control_panel/common.py new file mode 100644 index 00000000000..cf2de857076 --- /dev/null +++ b/tests/components/alarm_control_panel/common.py @@ -0,0 +1,83 @@ +"""Collection of helper methods. + +All containing methods are legacy helpers that should not be used by new +components. Instead call the service directly. +""" +from homeassistant.components.alarm_control_panel import DOMAIN +from homeassistant.const import ( + ATTR_CODE, ATTR_ENTITY_ID, SERVICE_ALARM_TRIGGER, + SERVICE_ALARM_DISARM, SERVICE_ALARM_ARM_HOME, SERVICE_ALARM_ARM_AWAY, + SERVICE_ALARM_ARM_NIGHT, SERVICE_ALARM_ARM_CUSTOM_BYPASS) +from homeassistant.loader import bind_hass + + +@bind_hass +def alarm_disarm(hass, code=None, entity_id=None): + """Send the alarm the command for disarm.""" + data = {} + if code: + data[ATTR_CODE] = code + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_ALARM_DISARM, data) + + +@bind_hass +def alarm_arm_home(hass, code=None, entity_id=None): + """Send the alarm the command for arm home.""" + data = {} + if code: + data[ATTR_CODE] = code + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_ALARM_ARM_HOME, data) + + +@bind_hass +def alarm_arm_away(hass, code=None, entity_id=None): + """Send the alarm the command for arm away.""" + data = {} + if code: + data[ATTR_CODE] = code + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_ALARM_ARM_AWAY, data) + + +@bind_hass +def alarm_arm_night(hass, code=None, entity_id=None): + """Send the alarm the command for arm night.""" + data = {} + if code: + data[ATTR_CODE] = code + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_ALARM_ARM_NIGHT, data) + + +@bind_hass +def alarm_trigger(hass, code=None, entity_id=None): + """Send the alarm the command for trigger.""" + data = {} + if code: + data[ATTR_CODE] = code + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_ALARM_TRIGGER, data) + + +@bind_hass +def alarm_arm_custom_bypass(hass, code=None, entity_id=None): + """Send the alarm the command for arm custom bypass.""" + data = {} + if code: + data[ATTR_CODE] = code + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_ALARM_ARM_CUSTOM_BYPASS, data) diff --git a/tests/components/alarm_control_panel/test_manual.py b/tests/components/alarm_control_panel/test_manual.py index 29f630093d9..02085a44b47 100644 --- a/tests/components/alarm_control_panel/test_manual.py +++ b/tests/components/alarm_control_panel/test_manual.py @@ -14,6 +14,7 @@ from homeassistant.components import alarm_control_panel import homeassistant.util.dt as dt_util from tests.common import fire_time_changed, get_test_home_assistant +from tests.components.alarm_control_panel import common CODE = 'HELLO_CODE' @@ -53,7 +54,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, CODE) + common.alarm_arm_home(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_HOME, @@ -76,7 +77,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, CODE, entity_id) + common.alarm_arm_home(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -111,7 +112,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, CODE + '2') + common.alarm_arm_home(self.hass, CODE + '2') self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -134,7 +135,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE, entity_id) + common.alarm_arm_away(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, @@ -160,7 +161,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, 'abc') + common.alarm_arm_home(self.hass, 'abc') self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -183,7 +184,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -218,7 +219,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE + '2') + common.alarm_arm_away(self.hass, CODE + '2') self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -241,7 +242,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_night(self.hass, CODE) + common.alarm_arm_night(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_NIGHT, @@ -264,7 +265,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_night(self.hass, CODE, entity_id) + common.alarm_arm_night(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -284,7 +285,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): assert state.state == STATE_ALARM_ARMED_NIGHT # Do not go to the pending state when updating to the same state - alarm_control_panel.alarm_arm_night(self.hass, CODE, entity_id) + common.alarm_arm_night(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_NIGHT, @@ -307,7 +308,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_night(self.hass, CODE + '2') + common.alarm_arm_night(self.hass, CODE + '2') self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -329,7 +330,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -362,13 +363,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -402,7 +403,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -425,7 +426,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -448,7 +449,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -496,13 +497,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -540,13 +541,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -584,13 +585,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -640,13 +641,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -687,7 +688,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_arm_home(self.hass) + common.alarm_arm_home(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -717,7 +718,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_arm_away(self.hass) + common.alarm_arm_away(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -747,7 +748,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_arm_night(self.hass) + common.alarm_arm_night(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -779,7 +780,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -820,7 +821,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -855,7 +856,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -881,7 +882,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -915,7 +916,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -947,13 +948,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE, entity_id) + common.alarm_arm_away(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -985,13 +986,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE, entity_id) + common.alarm_arm_away(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -1006,7 +1007,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -1037,13 +1038,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id) + common.alarm_disarm(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -1075,13 +1076,13 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id) + common.alarm_disarm(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -1117,19 +1118,19 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, 'def') + common.alarm_arm_home(self.hass, 'def') self.hass.block_till_done() state = self.hass.states.get(entity_id) self.assertEqual(STATE_ALARM_ARMED_HOME, state.state) - alarm_control_panel.alarm_disarm(self.hass, 'def') + common.alarm_disarm(self.hass, 'def') self.hass.block_till_done() state = self.hass.states.get(entity_id) self.assertEqual(STATE_ALARM_ARMED_HOME, state.state) - alarm_control_panel.alarm_disarm(self.hass, 'abc') + common.alarm_disarm(self.hass, 'abc') self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -1152,7 +1153,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_custom_bypass(self.hass, CODE) + common.alarm_arm_custom_bypass(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_CUSTOM_BYPASS, @@ -1175,7 +1176,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_custom_bypass(self.hass, CODE, entity_id) + common.alarm_arm_custom_bypass(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -1211,7 +1212,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_custom_bypass(self.hass, CODE + '2') + common.alarm_arm_custom_bypass(self.hass, CODE + '2') self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -1232,7 +1233,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_arm_custom_bypass(self.hass) + common.alarm_arm_custom_bypass(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -1271,7 +1272,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -1281,7 +1282,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): self.assertEqual(STATE_ALARM_ARMED_AWAY, state.attributes['post_pending_state']) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -1300,7 +1301,7 @@ class TestAlarmControlPanelManual(unittest.TestCase): state = self.hass.states.get(entity_id) self.assertEqual(STATE_ALARM_ARMED_AWAY, state.state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) diff --git a/tests/components/alarm_control_panel/test_manual_mqtt.py b/tests/components/alarm_control_panel/test_manual_mqtt.py index 0158381b526..4e2ec6a9489 100644 --- a/tests/components/alarm_control_panel/test_manual_mqtt.py +++ b/tests/components/alarm_control_panel/test_manual_mqtt.py @@ -13,6 +13,7 @@ import homeassistant.util.dt as dt_util from tests.common import ( fire_time_changed, get_test_home_assistant, mock_mqtt_component, fire_mqtt_message, assert_setup_component) +from tests.components.alarm_control_panel import common CODE = 'HELLO_CODE' @@ -70,7 +71,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, CODE) + common.alarm_arm_home(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_HOME, @@ -95,7 +96,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, CODE, entity_id) + common.alarm_arm_home(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -132,7 +133,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, CODE + '2') + common.alarm_arm_home(self.hass, CODE + '2') self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -157,7 +158,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE, entity_id) + common.alarm_arm_away(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, @@ -185,7 +186,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, 'abc') + common.alarm_arm_home(self.hass, 'abc') self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -210,7 +211,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -247,7 +248,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE + '2') + common.alarm_arm_away(self.hass, CODE + '2') self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -272,7 +273,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_night(self.hass, CODE, entity_id) + common.alarm_arm_night(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_NIGHT, @@ -297,7 +298,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_night(self.hass, CODE) + common.alarm_arm_night(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -317,7 +318,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.hass.states.get(entity_id).state) # Do not go to the pending state when updating to the same state - alarm_control_panel.alarm_arm_night(self.hass, CODE, entity_id) + common.alarm_arm_night(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_NIGHT, @@ -342,7 +343,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_night(self.hass, CODE + '2') + common.alarm_arm_night(self.hass, CODE + '2') self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -366,7 +367,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -401,13 +402,13 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -443,7 +444,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -468,7 +469,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -493,7 +494,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -539,7 +540,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -576,7 +577,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -604,7 +605,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -640,7 +641,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -674,13 +675,13 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE, entity_id) + common.alarm_arm_away(self.hass, CODE, entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -695,7 +696,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_TRIGGERED, @@ -728,13 +729,13 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id) + common.alarm_disarm(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_DISARMED, @@ -768,13 +769,13 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_disarm(self.hass, entity_id=entity_id) + common.alarm_disarm(self.hass, entity_id=entity_id) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -812,13 +813,13 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -858,13 +859,13 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -904,13 +905,13 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -962,13 +963,13 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() self.assertEqual(STATE_ALARM_ARMED_AWAY, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -1011,7 +1012,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_arm_home(self.hass) + common.alarm_arm_home(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -1043,7 +1044,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_arm_away(self.hass) + common.alarm_arm_away(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -1075,7 +1076,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_arm_night(self.hass) + common.alarm_arm_night(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -1109,7 +1110,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): entity_id = 'alarm_control_panel.test' - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -1159,7 +1160,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_away(self.hass, CODE) + common.alarm_arm_away(self.hass, CODE) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -1169,7 +1170,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_ARMED_AWAY, state.attributes['post_pending_state']) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -1188,7 +1189,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): state = self.hass.states.get(entity_id) self.assertEqual(STATE_ALARM_ARMED_AWAY, state.state) - alarm_control_panel.alarm_trigger(self.hass, entity_id=entity_id) + common.alarm_trigger(self.hass, entity_id=entity_id) self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -1230,19 +1231,19 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_arm_home(self.hass, 'def') + common.alarm_arm_home(self.hass, 'def') self.hass.block_till_done() state = self.hass.states.get(entity_id) self.assertEqual(STATE_ALARM_ARMED_HOME, state.state) - alarm_control_panel.alarm_disarm(self.hass, 'def') + common.alarm_disarm(self.hass, 'def') self.hass.block_till_done() state = self.hass.states.get(entity_id) self.assertEqual(STATE_ALARM_ARMED_HOME, state.state) - alarm_control_panel.alarm_disarm(self.hass, 'abc') + common.alarm_disarm(self.hass, 'abc') self.hass.block_till_done() state = self.hass.states.get(entity_id) @@ -1368,7 +1369,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.assertEqual(STATE_ALARM_DISARMED, self.hass.states.get(entity_id).state) - alarm_control_panel.alarm_trigger(self.hass) + common.alarm_trigger(self.hass) self.hass.block_till_done() self.assertEqual(STATE_ALARM_PENDING, @@ -1401,7 +1402,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.mock_publish.async_publish.reset_mock() # Arm in home mode - alarm_control_panel.alarm_arm_home(self.hass) + common.alarm_arm_home(self.hass) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'alarm/state', STATE_ALARM_PENDING, 0, True) @@ -1417,7 +1418,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.mock_publish.async_publish.reset_mock() # Arm in away mode - alarm_control_panel.alarm_arm_away(self.hass) + common.alarm_arm_away(self.hass) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'alarm/state', STATE_ALARM_PENDING, 0, True) @@ -1433,7 +1434,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.mock_publish.async_publish.reset_mock() # Arm in night mode - alarm_control_panel.alarm_arm_night(self.hass) + common.alarm_arm_night(self.hass) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'alarm/state', STATE_ALARM_PENDING, 0, True) @@ -1449,7 +1450,7 @@ class TestAlarmControlPanelManualMqtt(unittest.TestCase): self.mock_publish.async_publish.reset_mock() # Disarm - alarm_control_panel.alarm_disarm(self.hass) + common.alarm_disarm(self.hass) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'alarm/state', STATE_ALARM_DISARMED, 0, True) diff --git a/tests/components/alarm_control_panel/test_mqtt.py b/tests/components/alarm_control_panel/test_mqtt.py index 3a68b3cee44..b77767980a7 100644 --- a/tests/components/alarm_control_panel/test_mqtt.py +++ b/tests/components/alarm_control_panel/test_mqtt.py @@ -12,6 +12,7 @@ from homeassistant.components.mqtt.discovery import async_start from tests.common import ( mock_mqtt_component, async_fire_mqtt_message, fire_mqtt_message, get_test_home_assistant, assert_setup_component) +from tests.components.alarm_control_panel import common CODE = 'HELLO_CODE' @@ -105,7 +106,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase): } }) - alarm_control_panel.alarm_arm_home(self.hass) + common.alarm_arm_home(self.hass) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'alarm/command', 'ARM_HOME', 0, False) @@ -123,7 +124,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase): }) call_count = self.mock_publish.call_count - alarm_control_panel.alarm_arm_home(self.hass, 'abcd') + common.alarm_arm_home(self.hass, 'abcd') self.hass.block_till_done() self.assertEqual(call_count, self.mock_publish.call_count) @@ -138,7 +139,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase): } }) - alarm_control_panel.alarm_arm_away(self.hass) + common.alarm_arm_away(self.hass) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'alarm/command', 'ARM_AWAY', 0, False) @@ -156,7 +157,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase): }) call_count = self.mock_publish.call_count - alarm_control_panel.alarm_arm_away(self.hass, 'abcd') + common.alarm_arm_away(self.hass, 'abcd') self.hass.block_till_done() self.assertEqual(call_count, self.mock_publish.call_count) @@ -171,7 +172,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase): } }) - alarm_control_panel.alarm_disarm(self.hass) + common.alarm_disarm(self.hass) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'alarm/command', 'DISARM', 0, False) @@ -189,7 +190,7 @@ class TestAlarmControlPanelMQTT(unittest.TestCase): }) call_count = self.mock_publish.call_count - alarm_control_panel.alarm_disarm(self.hass, 'abcd') + common.alarm_disarm(self.hass, 'abcd') self.hass.block_till_done() self.assertEqual(call_count, self.mock_publish.call_count) diff --git a/tests/components/automation/common.py b/tests/components/automation/common.py new file mode 100644 index 00000000000..4c8f91849aa --- /dev/null +++ b/tests/components/automation/common.py @@ -0,0 +1,53 @@ +"""Collection of helper methods. + +All containing methods are legacy helpers that should not be used by new +components. Instead call the service directly. +""" +from homeassistant.components.automation import DOMAIN, SERVICE_TRIGGER +from homeassistant.const import ( + ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE, + SERVICE_RELOAD) +from homeassistant.loader import bind_hass + + +@bind_hass +def turn_on(hass, entity_id=None): + """Turn on specified automation or all.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} + hass.services.call(DOMAIN, SERVICE_TURN_ON, data) + + +@bind_hass +def turn_off(hass, entity_id=None): + """Turn off specified automation or all.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} + hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) + + +@bind_hass +def toggle(hass, entity_id=None): + """Toggle specified automation or all.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} + hass.services.call(DOMAIN, SERVICE_TOGGLE, data) + + +@bind_hass +def trigger(hass, entity_id=None): + """Trigger specified automation or all.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} + hass.services.call(DOMAIN, SERVICE_TRIGGER, data) + + +@bind_hass +def reload(hass): + """Reload the automation from config.""" + hass.services.call(DOMAIN, SERVICE_RELOAD) + + +@bind_hass +def async_reload(hass): + """Reload the automation from config. + + Returns a coroutine object. + """ + return hass.services.async_call(DOMAIN, SERVICE_RELOAD) diff --git a/tests/components/automation/test_event.py b/tests/components/automation/test_event.py index 6e16c03f2dc..09d237013b0 100644 --- a/tests/components/automation/test_event.py +++ b/tests/components/automation/test_event.py @@ -6,6 +6,7 @@ from homeassistant.setup import setup_component import homeassistant.components.automation as automation from tests.common import get_test_home_assistant, mock_component +from tests.components.automation import common # pylint: disable=invalid-name @@ -50,7 +51,7 @@ class TestAutomationEvent(unittest.TestCase): self.assertEqual(1, len(self.calls)) assert self.calls[0].context is context - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() self.hass.bus.fire('test_event') @@ -75,7 +76,7 @@ class TestAutomationEvent(unittest.TestCase): self.hass.block_till_done() self.assertEqual(1, len(self.calls)) - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() self.hass.bus.fire('test_event') diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index c3bd6c224af..3bcbc7da04f 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -15,6 +15,7 @@ import homeassistant.util.dt as dt_util from tests.common import ( assert_setup_component, get_test_home_assistant, fire_time_changed, mock_service, async_mock_service, mock_restore_cache) +from tests.components.automation import common # pylint: disable=invalid-name @@ -363,7 +364,7 @@ class TestAutomation(unittest.TestCase): self.hass.block_till_done() assert len(self.calls) == 1 - automation.turn_off(self.hass, entity_id) + common.turn_off(self.hass, entity_id) self.hass.block_till_done() assert not automation.is_on(self.hass, entity_id) @@ -371,7 +372,7 @@ class TestAutomation(unittest.TestCase): self.hass.block_till_done() assert len(self.calls) == 1 - automation.toggle(self.hass, entity_id) + common.toggle(self.hass, entity_id) self.hass.block_till_done() assert automation.is_on(self.hass, entity_id) @@ -379,17 +380,17 @@ class TestAutomation(unittest.TestCase): self.hass.block_till_done() assert len(self.calls) == 2 - automation.trigger(self.hass, entity_id) + common.trigger(self.hass, entity_id) self.hass.block_till_done() assert len(self.calls) == 3 - automation.turn_off(self.hass, entity_id) + common.turn_off(self.hass, entity_id) self.hass.block_till_done() - automation.trigger(self.hass, entity_id) + common.trigger(self.hass, entity_id) self.hass.block_till_done() assert len(self.calls) == 4 - automation.turn_on(self.hass, entity_id) + common.turn_on(self.hass, entity_id) self.hass.block_till_done() assert automation.is_on(self.hass, entity_id) @@ -439,7 +440,7 @@ class TestAutomation(unittest.TestCase): }}): with patch('homeassistant.config.find_config_file', return_value=''): - automation.reload(self.hass) + common.reload(self.hass) self.hass.block_till_done() # De-flake ?! self.hass.block_till_done() @@ -489,7 +490,7 @@ class TestAutomation(unittest.TestCase): return_value={automation.DOMAIN: 'not valid'}): with patch('homeassistant.config.find_config_file', return_value=''): - automation.reload(self.hass) + common.reload(self.hass) self.hass.block_till_done() assert self.hass.states.get('automation.hello') is None @@ -527,7 +528,7 @@ class TestAutomation(unittest.TestCase): side_effect=HomeAssistantError('bla')): with patch('homeassistant.config.find_config_file', return_value=''): - automation.reload(self.hass) + common.reload(self.hass) self.hass.block_till_done() assert self.hass.states.get('automation.hello') is not None diff --git a/tests/components/automation/test_litejet.py b/tests/components/automation/test_litejet.py index ca6f7796cfc..3d88174708b 100644 --- a/tests/components/automation/test_litejet.py +++ b/tests/components/automation/test_litejet.py @@ -7,9 +7,10 @@ from datetime import timedelta from homeassistant import setup import homeassistant.util.dt as dt_util from homeassistant.components import litejet -from tests.common import (fire_time_changed, get_test_home_assistant) import homeassistant.components.automation as automation +from tests.common import (fire_time_changed, get_test_home_assistant) + _LOGGER = logging.getLogger(__name__) ENTITY_SWITCH = 'switch.mock_switch_1' diff --git a/tests/components/automation/test_mqtt.py b/tests/components/automation/test_mqtt.py index 8ec5351af94..29a53467c4f 100644 --- a/tests/components/automation/test_mqtt.py +++ b/tests/components/automation/test_mqtt.py @@ -7,6 +7,7 @@ import homeassistant.components.automation as automation from tests.common import ( mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_component) +from tests.components.automation import common # pylint: disable=invalid-name @@ -56,7 +57,7 @@ class TestAutomationMQTT(unittest.TestCase): self.assertEqual('mqtt - test-topic - { "hello": "world" } - world', self.calls[0].data['some']) - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() fire_mqtt_message(self.hass, 'test-topic', 'test_payload') self.hass.block_till_done() diff --git a/tests/components/automation/test_numeric_state.py b/tests/components/automation/test_numeric_state.py index af95bc0ff02..183d1f4a5f9 100644 --- a/tests/components/automation/test_numeric_state.py +++ b/tests/components/automation/test_numeric_state.py @@ -11,6 +11,7 @@ import homeassistant.util.dt as dt_util from tests.common import ( get_test_home_assistant, mock_component, fire_time_changed, assert_setup_component) +from tests.components.automation import common # pylint: disable=invalid-name @@ -57,7 +58,7 @@ class TestAutomationNumericState(unittest.TestCase): # Set above 12 so the automation will fire again self.hass.states.set('test.entity', 12) - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() self.hass.states.set('test.entity', 9) self.hass.block_till_done() @@ -775,7 +776,7 @@ class TestAutomationNumericState(unittest.TestCase): self.hass.states.set('test.entity_1', 9) self.hass.states.set('test.entity_2', 9) self.hass.block_till_done() - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10)) diff --git a/tests/components/automation/test_state.py b/tests/components/automation/test_state.py index 274980fabc0..15c6353b234 100644 --- a/tests/components/automation/test_state.py +++ b/tests/components/automation/test_state.py @@ -12,6 +12,7 @@ import homeassistant.components.automation as automation from tests.common import ( fire_time_changed, get_test_home_assistant, assert_setup_component, mock_component) +from tests.components.automation import common # pylint: disable=invalid-name @@ -68,7 +69,7 @@ class TestAutomationState(unittest.TestCase): 'state - test.entity - hello - world - None', self.calls[0].data['some']) - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() self.hass.states.set('test.entity', 'planet') self.hass.block_till_done() @@ -370,7 +371,7 @@ class TestAutomationState(unittest.TestCase): self.hass.states.set('test.entity_1', 'world') self.hass.states.set('test.entity_2', 'world') self.hass.block_till_done() - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, dt_util.utcnow() + timedelta(seconds=10)) diff --git a/tests/components/automation/test_sun.py b/tests/components/automation/test_sun.py index 4556b7cbe45..ad8709fdf36 100644 --- a/tests/components/automation/test_sun.py +++ b/tests/components/automation/test_sun.py @@ -12,6 +12,7 @@ import homeassistant.util.dt as dt_util from tests.common import ( fire_time_changed, get_test_home_assistant, mock_component) +from tests.components.automation import common # pylint: disable=invalid-name @@ -57,7 +58,7 @@ class TestAutomationSun(unittest.TestCase): } }) - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, trigger_time) @@ -66,7 +67,7 @@ class TestAutomationSun(unittest.TestCase): with patch('homeassistant.util.dt.utcnow', return_value=now): - automation.turn_on(self.hass) + common.turn_on(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, trigger_time) diff --git a/tests/components/automation/test_template.py b/tests/components/automation/test_template.py index e9c763ccc73..4fec0e707a9 100644 --- a/tests/components/automation/test_template.py +++ b/tests/components/automation/test_template.py @@ -7,6 +7,7 @@ import homeassistant.components.automation as automation from tests.common import ( get_test_home_assistant, assert_setup_component, mock_component) +from tests.components.automation import common # pylint: disable=invalid-name @@ -49,7 +50,7 @@ class TestAutomationTemplate(unittest.TestCase): self.hass.block_till_done() self.assertEqual(1, len(self.calls)) - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() self.hass.states.set('test.entity', 'planet') diff --git a/tests/components/automation/test_time.py b/tests/components/automation/test_time.py index 5f928cf92a0..dcb723d725e 100644 --- a/tests/components/automation/test_time.py +++ b/tests/components/automation/test_time.py @@ -11,6 +11,7 @@ import homeassistant.components.automation as automation from tests.common import ( fire_time_changed, get_test_home_assistant, assert_setup_component, mock_component) +from tests.components.automation import common # pylint: disable=invalid-name @@ -52,7 +53,7 @@ class TestAutomationTime(unittest.TestCase): self.hass.block_till_done() self.assertEqual(1, len(self.calls)) - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() fire_time_changed(self.hass, dt_util.utcnow().replace(hour=0)) diff --git a/tests/components/automation/test_zone.py b/tests/components/automation/test_zone.py index d146278a997..795f55a3e0b 100644 --- a/tests/components/automation/test_zone.py +++ b/tests/components/automation/test_zone.py @@ -6,6 +6,7 @@ from homeassistant.setup import setup_component from homeassistant.components import automation, zone from tests.common import get_test_home_assistant, mock_component +from tests.components.automation import common # pylint: disable=invalid-name @@ -87,7 +88,7 @@ class TestAutomationZone(unittest.TestCase): }) self.hass.block_till_done() - automation.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() self.hass.states.set('test.entity', 'hello', { diff --git a/tests/components/camera/common.py b/tests/components/camera/common.py new file mode 100644 index 00000000000..71193f1a99d --- /dev/null +++ b/tests/components/camera/common.py @@ -0,0 +1,68 @@ +"""Collection of helper methods. + +All containing methods are legacy helpers that should not be used by new +components. Instead call the service directly. +""" +from homeassistant.components.camera import ( + ATTR_FILENAME, DOMAIN, SERVICE_DISABLE_MOTION, SERVICE_ENABLE_MOTION, + SERVICE_SNAPSHOT) +from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TURN_OFF, \ + SERVICE_TURN_ON +from homeassistant.core import callback +from homeassistant.loader import bind_hass + + +@bind_hass +def turn_off(hass, entity_id=None): + """Turn off camera.""" + hass.add_job(async_turn_off, hass, entity_id) + + +@bind_hass +async def async_turn_off(hass, entity_id=None): + """Turn off camera.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} + await hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data) + + +@bind_hass +def turn_on(hass, entity_id=None): + """Turn on camera.""" + hass.add_job(async_turn_on, hass, entity_id) + + +@bind_hass +async def async_turn_on(hass, entity_id=None): + """Turn on camera, and set operation mode.""" + data = {} + if entity_id is not None: + data[ATTR_ENTITY_ID] = entity_id + + await hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data) + + +@bind_hass +def enable_motion_detection(hass, entity_id=None): + """Enable Motion Detection.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else None + hass.async_add_job(hass.services.async_call( + DOMAIN, SERVICE_ENABLE_MOTION, data)) + + +@bind_hass +def disable_motion_detection(hass, entity_id=None): + """Disable Motion Detection.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else None + hass.async_add_job(hass.services.async_call( + DOMAIN, SERVICE_DISABLE_MOTION, data)) + + +@bind_hass +@callback +def async_snapshot(hass, filename, entity_id=None): + """Make a snapshot from a camera.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} + data[ATTR_FILENAME] = filename + + hass.async_add_job(hass.services.async_call( + DOMAIN, SERVICE_SNAPSHOT, data)) diff --git a/tests/components/camera/test_demo.py b/tests/components/camera/test_demo.py index 63c70ddc6ca..f6e2513380c 100644 --- a/tests/components/camera/test_demo.py +++ b/tests/components/camera/test_demo.py @@ -8,6 +8,8 @@ from homeassistant.components.camera import STATE_STREAMING, STATE_IDLE from homeassistant.exceptions import HomeAssistantError from homeassistant.setup import async_setup_component +from tests.components.camera import common + @pytest.fixture def demo_camera(hass): @@ -37,12 +39,12 @@ async def test_init_state_is_streaming(hass, demo_camera): async def test_turn_on_state_back_to_streaming(hass, demo_camera): """After turn on state back to streaming.""" assert demo_camera.state == STATE_STREAMING - await camera.async_turn_off(hass, demo_camera.entity_id) + await common.async_turn_off(hass, demo_camera.entity_id) await hass.async_block_till_done() assert demo_camera.state == STATE_IDLE - await camera.async_turn_on(hass, demo_camera.entity_id) + await common.async_turn_on(hass, demo_camera.entity_id) await hass.async_block_till_done() assert demo_camera.state == STATE_STREAMING @@ -50,7 +52,7 @@ async def test_turn_on_state_back_to_streaming(hass, demo_camera): async def test_turn_off_image(hass, demo_camera): """After turn off, Demo camera raise error.""" - await camera.async_turn_off(hass, demo_camera.entity_id) + await common.async_turn_off(hass, demo_camera.entity_id) await hass.async_block_till_done() with pytest.raises(HomeAssistantError) as error: @@ -61,7 +63,7 @@ async def test_turn_off_image(hass, demo_camera): async def test_turn_off_invalid_camera(hass, demo_camera): """Turn off non-exist camera should quietly fail.""" assert demo_camera.state == STATE_STREAMING - await camera.async_turn_off(hass, 'camera.invalid_camera') + await common.async_turn_off(hass, 'camera.invalid_camera') await hass.async_block_till_done() assert demo_camera.state == STATE_STREAMING @@ -81,7 +83,7 @@ async def test_motion_detection(hass): assert not state.attributes.get('motion_detection') # Call service to turn on motion detection - camera.enable_motion_detection(hass, 'camera.demo_camera') + common.enable_motion_detection(hass, 'camera.demo_camera') await hass.async_block_till_done() # Check if state has been updated. diff --git a/tests/components/camera/test_init.py b/tests/components/camera/test_init.py index 053fa6d29dc..2129e39a43c 100644 --- a/tests/components/camera/test_init.py +++ b/tests/components/camera/test_init.py @@ -14,6 +14,7 @@ from homeassistant.util.async_ import run_coroutine_threadsafe from tests.common import ( get_test_home_assistant, get_test_instance_port, assert_setup_component, mock_coro) +from tests.components.camera import common @pytest.fixture @@ -126,7 +127,7 @@ def test_snapshot_service(hass, mock_camera): with patch('homeassistant.components.camera.open', mopen, create=True), \ patch.object(hass.config, 'is_allowed_path', return_value=True): - hass.components.camera.async_snapshot('/tmp/bla') + common.async_snapshot(hass, '/tmp/bla') yield from hass.async_block_till_done() mock_write = mopen().write diff --git a/tests/components/climate/common.py b/tests/components/climate/common.py new file mode 100644 index 00000000000..4ac6f553091 --- /dev/null +++ b/tests/components/climate/common.py @@ -0,0 +1,115 @@ +"""Collection of helper methods. + +All containing methods are legacy helpers that should not be used by new +components. Instead call the service directly. +""" +from homeassistant.components.climate import ( + _LOGGER, ATTR_AUX_HEAT, ATTR_AWAY_MODE, ATTR_FAN_MODE, ATTR_HOLD_MODE, + ATTR_HUMIDITY, ATTR_OPERATION_MODE, ATTR_SWING_MODE, ATTR_TARGET_TEMP_HIGH, + ATTR_TARGET_TEMP_LOW, DOMAIN, SERVICE_SET_AWAY_MODE, SERVICE_SET_HOLD_MODE, + SERVICE_SET_AUX_HEAT, SERVICE_SET_TEMPERATURE, SERVICE_SET_HUMIDITY, + SERVICE_SET_FAN_MODE, SERVICE_SET_OPERATION_MODE, SERVICE_SET_SWING_MODE) +from homeassistant.const import ( + ATTR_ENTITY_ID, ATTR_TEMPERATURE) +from homeassistant.loader import bind_hass + + +@bind_hass +def set_away_mode(hass, away_mode, entity_id=None): + """Turn all or specified climate devices away mode on.""" + data = { + ATTR_AWAY_MODE: away_mode + } + + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_SET_AWAY_MODE, data) + + +@bind_hass +def set_hold_mode(hass, hold_mode, entity_id=None): + """Set new hold mode.""" + data = { + ATTR_HOLD_MODE: hold_mode + } + + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_SET_HOLD_MODE, data) + + +@bind_hass +def set_aux_heat(hass, aux_heat, entity_id=None): + """Turn all or specified climate devices auxiliary heater on.""" + data = { + ATTR_AUX_HEAT: aux_heat + } + + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_SET_AUX_HEAT, data) + + +@bind_hass +def set_temperature(hass, temperature=None, entity_id=None, + target_temp_high=None, target_temp_low=None, + operation_mode=None): + """Set new target temperature.""" + kwargs = { + key: value for key, value in [ + (ATTR_TEMPERATURE, temperature), + (ATTR_TARGET_TEMP_HIGH, target_temp_high), + (ATTR_TARGET_TEMP_LOW, target_temp_low), + (ATTR_ENTITY_ID, entity_id), + (ATTR_OPERATION_MODE, operation_mode) + ] if value is not None + } + _LOGGER.debug("set_temperature start data=%s", kwargs) + hass.services.call(DOMAIN, SERVICE_SET_TEMPERATURE, kwargs) + + +@bind_hass +def set_humidity(hass, humidity, entity_id=None): + """Set new target humidity.""" + data = {ATTR_HUMIDITY: humidity} + + if entity_id is not None: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_SET_HUMIDITY, data) + + +@bind_hass +def set_fan_mode(hass, fan, entity_id=None): + """Set all or specified climate devices fan mode on.""" + data = {ATTR_FAN_MODE: fan} + + if entity_id: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_SET_FAN_MODE, data) + + +@bind_hass +def set_operation_mode(hass, operation_mode, entity_id=None): + """Set new target operation mode.""" + data = {ATTR_OPERATION_MODE: operation_mode} + + if entity_id is not None: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_SET_OPERATION_MODE, data) + + +@bind_hass +def set_swing_mode(hass, swing_mode, entity_id=None): + """Set new target swing mode.""" + data = {ATTR_SWING_MODE: swing_mode} + + if entity_id is not None: + data[ATTR_ENTITY_ID] = entity_id + + hass.services.call(DOMAIN, SERVICE_SET_SWING_MODE, data) diff --git a/tests/components/climate/test_demo.py b/tests/components/climate/test_demo.py index 0cd6d288536..4990a8a6998 100644 --- a/tests/components/climate/test_demo.py +++ b/tests/components/climate/test_demo.py @@ -8,6 +8,7 @@ from homeassistant.setup import setup_component from homeassistant.components import climate from tests.common import get_test_home_assistant +from tests.components.climate import common ENTITY_CLIMATE = 'climate.hvac' @@ -56,7 +57,7 @@ class TestDemoClimate(unittest.TestCase): """Test setting the target temperature without required attribute.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(21, state.attributes.get('temperature')) - climate.set_temperature(self.hass, None, ENTITY_CLIMATE) + common.set_temperature(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() self.assertEqual(21, state.attributes.get('temperature')) @@ -64,7 +65,7 @@ class TestDemoClimate(unittest.TestCase): """Test the setting of the target temperature.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(21, state.attributes.get('temperature')) - climate.set_temperature(self.hass, 30, ENTITY_CLIMATE) + common.set_temperature(self.hass, 30, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(30.0, state.attributes.get('temperature')) @@ -73,7 +74,7 @@ class TestDemoClimate(unittest.TestCase): """Test the setting of the target temperature.""" state = self.hass.states.get(ENTITY_HEATPUMP) self.assertEqual(20, state.attributes.get('temperature')) - climate.set_temperature(self.hass, 21, ENTITY_HEATPUMP) + common.set_temperature(self.hass, 21, ENTITY_HEATPUMP) self.hass.block_till_done() state = self.hass.states.get(ENTITY_HEATPUMP) self.assertEqual(21.0, state.attributes.get('temperature')) @@ -84,8 +85,8 @@ class TestDemoClimate(unittest.TestCase): self.assertEqual(None, state.attributes.get('temperature')) self.assertEqual(21.0, state.attributes.get('target_temp_low')) self.assertEqual(24.0, state.attributes.get('target_temp_high')) - climate.set_temperature(self.hass, target_temp_high=25, - target_temp_low=20, entity_id=ENTITY_ECOBEE) + common.set_temperature(self.hass, target_temp_high=25, + target_temp_low=20, entity_id=ENTITY_ECOBEE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_ECOBEE) self.assertEqual(None, state.attributes.get('temperature')) @@ -98,9 +99,9 @@ class TestDemoClimate(unittest.TestCase): self.assertEqual(None, state.attributes.get('temperature')) self.assertEqual(21.0, state.attributes.get('target_temp_low')) self.assertEqual(24.0, state.attributes.get('target_temp_high')) - climate.set_temperature(self.hass, temperature=None, - entity_id=ENTITY_ECOBEE, target_temp_low=None, - target_temp_high=None) + common.set_temperature(self.hass, temperature=None, + entity_id=ENTITY_ECOBEE, target_temp_low=None, + target_temp_high=None) self.hass.block_till_done() state = self.hass.states.get(ENTITY_ECOBEE) self.assertEqual(None, state.attributes.get('temperature')) @@ -111,7 +112,7 @@ class TestDemoClimate(unittest.TestCase): """Test setting the target humidity without required attribute.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(67, state.attributes.get('humidity')) - climate.set_humidity(self.hass, None, ENTITY_CLIMATE) + common.set_humidity(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(67, state.attributes.get('humidity')) @@ -120,7 +121,7 @@ class TestDemoClimate(unittest.TestCase): """Test the setting of the target humidity.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(67, state.attributes.get('humidity')) - climate.set_humidity(self.hass, 64, ENTITY_CLIMATE) + common.set_humidity(self.hass, 64, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(64.0, state.attributes.get('humidity')) @@ -129,7 +130,7 @@ class TestDemoClimate(unittest.TestCase): """Test setting fan mode without required attribute.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("On High", state.attributes.get('fan_mode')) - climate.set_fan_mode(self.hass, None, ENTITY_CLIMATE) + common.set_fan_mode(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("On High", state.attributes.get('fan_mode')) @@ -138,7 +139,7 @@ class TestDemoClimate(unittest.TestCase): """Test setting of new fan mode.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("On High", state.attributes.get('fan_mode')) - climate.set_fan_mode(self.hass, "On Low", ENTITY_CLIMATE) + common.set_fan_mode(self.hass, "On Low", ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("On Low", state.attributes.get('fan_mode')) @@ -147,7 +148,7 @@ class TestDemoClimate(unittest.TestCase): """Test setting swing mode without required attribute.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("Off", state.attributes.get('swing_mode')) - climate.set_swing_mode(self.hass, None, ENTITY_CLIMATE) + common.set_swing_mode(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("Off", state.attributes.get('swing_mode')) @@ -156,7 +157,7 @@ class TestDemoClimate(unittest.TestCase): """Test setting of new swing mode.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("Off", state.attributes.get('swing_mode')) - climate.set_swing_mode(self.hass, "Auto", ENTITY_CLIMATE) + common.set_swing_mode(self.hass, "Auto", ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("Auto", state.attributes.get('swing_mode')) @@ -169,7 +170,7 @@ class TestDemoClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("cool", state.attributes.get('operation_mode')) self.assertEqual("cool", state.state) - climate.set_operation_mode(self.hass, None, ENTITY_CLIMATE) + common.set_operation_mode(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("cool", state.attributes.get('operation_mode')) @@ -180,7 +181,7 @@ class TestDemoClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("cool", state.attributes.get('operation_mode')) self.assertEqual("cool", state.state) - climate.set_operation_mode(self.hass, "heat", ENTITY_CLIMATE) + common.set_operation_mode(self.hass, "heat", ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("heat", state.attributes.get('operation_mode')) @@ -190,41 +191,41 @@ class TestDemoClimate(unittest.TestCase): """Test setting the away mode without required attribute.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('on', state.attributes.get('away_mode')) - climate.set_away_mode(self.hass, None, ENTITY_CLIMATE) + common.set_away_mode(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() self.assertEqual('on', state.attributes.get('away_mode')) def test_set_away_mode_on(self): """Test setting the away mode on/true.""" - climate.set_away_mode(self.hass, True, ENTITY_CLIMATE) + common.set_away_mode(self.hass, True, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('on', state.attributes.get('away_mode')) def test_set_away_mode_off(self): """Test setting the away mode off/false.""" - climate.set_away_mode(self.hass, False, ENTITY_CLIMATE) + common.set_away_mode(self.hass, False, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('away_mode')) def test_set_hold_mode_home(self): """Test setting the hold mode home.""" - climate.set_hold_mode(self.hass, 'home', ENTITY_ECOBEE) + common.set_hold_mode(self.hass, 'home', ENTITY_ECOBEE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_ECOBEE) self.assertEqual('home', state.attributes.get('hold_mode')) def test_set_hold_mode_away(self): """Test setting the hold mode away.""" - climate.set_hold_mode(self.hass, 'away', ENTITY_ECOBEE) + common.set_hold_mode(self.hass, 'away', ENTITY_ECOBEE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_ECOBEE) self.assertEqual('away', state.attributes.get('hold_mode')) def test_set_hold_mode_none(self): """Test setting the hold mode off/false.""" - climate.set_hold_mode(self.hass, 'off', ENTITY_ECOBEE) + common.set_hold_mode(self.hass, 'off', ENTITY_ECOBEE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_ECOBEE) self.assertEqual('off', state.attributes.get('hold_mode')) @@ -233,20 +234,20 @@ class TestDemoClimate(unittest.TestCase): """Test setting the auxiliary heater without required attribute.""" state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('aux_heat')) - climate.set_aux_heat(self.hass, None, ENTITY_CLIMATE) + common.set_aux_heat(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() self.assertEqual('off', state.attributes.get('aux_heat')) def test_set_aux_heat_on(self): """Test setting the axillary heater on/true.""" - climate.set_aux_heat(self.hass, True, ENTITY_CLIMATE) + common.set_aux_heat(self.hass, True, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('on', state.attributes.get('aux_heat')) def test_set_aux_heat_off(self): """Test setting the auxiliary heater off/false.""" - climate.set_aux_heat(self.hass, False, ENTITY_CLIMATE) + common.set_aux_heat(self.hass, False, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('aux_heat')) diff --git a/tests/components/climate/test_generic_thermostat.py b/tests/components/climate/test_generic_thermostat.py index c4e07705230..47ec621aeb5 100644 --- a/tests/components/climate/test_generic_thermostat.py +++ b/tests/components/climate/test_generic_thermostat.py @@ -25,6 +25,7 @@ from homeassistant.components.climate import STATE_HEAT, STATE_COOL import homeassistant.components as comps from tests.common import (assert_setup_component, get_test_home_assistant, mock_restore_cache) +from tests.components.climate import common ENTITY = 'climate.test' @@ -108,7 +109,7 @@ class TestGenericThermostatHeaterSwitching(unittest.TestCase): self._setup_sensor(18) self.hass.block_till_done() - climate.set_temperature(self.hass, 23) + common.set_temperature(self.hass, 23) self.hass.block_till_done() self.assertEqual(STATE_ON, @@ -135,7 +136,7 @@ class TestGenericThermostatHeaterSwitching(unittest.TestCase): self._setup_sensor(18) self.hass.block_till_done() - climate.set_temperature(self.hass, 23) + common.set_temperature(self.hass, 23) self.hass.block_till_done() self.assertEqual(STATE_ON, @@ -186,20 +187,20 @@ class TestClimateGenericThermostat(unittest.TestCase): def test_set_target_temp(self): """Test the setting of the target temperature.""" - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() state = self.hass.states.get(ENTITY) self.assertEqual(30.0, state.attributes.get('temperature')) - climate.set_temperature(self.hass, None) + common.set_temperature(self.hass, None) self.hass.block_till_done() state = self.hass.states.get(ENTITY) self.assertEqual(30.0, state.attributes.get('temperature')) def test_set_away_mode(self): """Test the setting away mode.""" - climate.set_temperature(self.hass, 23) + common.set_temperature(self.hass, 23) self.hass.block_till_done() - climate.set_away_mode(self.hass, True) + common.set_away_mode(self.hass, True) self.hass.block_till_done() state = self.hass.states.get(ENTITY) self.assertEqual(16, state.attributes.get('temperature')) @@ -209,13 +210,13 @@ class TestClimateGenericThermostat(unittest.TestCase): Verify original temperature is restored. """ - climate.set_temperature(self.hass, 23) + common.set_temperature(self.hass, 23) self.hass.block_till_done() - climate.set_away_mode(self.hass, True) + common.set_away_mode(self.hass, True) self.hass.block_till_done() state = self.hass.states.get(ENTITY) self.assertEqual(16, state.attributes.get('temperature')) - climate.set_away_mode(self.hass, False) + common.set_away_mode(self.hass, False) self.hass.block_till_done() state = self.hass.states.get(ENTITY) self.assertEqual(23, state.attributes.get('temperature')) @@ -236,7 +237,7 @@ class TestClimateGenericThermostat(unittest.TestCase): self._setup_switch(False) self._setup_sensor(25) self.hass.block_till_done() - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self.assertEqual(1, len(self.calls)) call = self.calls[0] @@ -249,7 +250,7 @@ class TestClimateGenericThermostat(unittest.TestCase): self._setup_switch(True) self._setup_sensor(30) self.hass.block_till_done() - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() self.assertEqual(2, len(self.calls)) call = self.calls[0] @@ -260,7 +261,7 @@ class TestClimateGenericThermostat(unittest.TestCase): def test_temp_change_heater_on_within_tolerance(self): """Test if temperature change doesn't turn on within tolerance.""" self._setup_switch(False) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(29) self.hass.block_till_done() @@ -269,7 +270,7 @@ class TestClimateGenericThermostat(unittest.TestCase): def test_temp_change_heater_on_outside_tolerance(self): """Test if temperature change turn heater on outside cold tolerance.""" self._setup_switch(False) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(27) self.hass.block_till_done() @@ -282,7 +283,7 @@ class TestClimateGenericThermostat(unittest.TestCase): def test_temp_change_heater_off_within_tolerance(self): """Test if temperature change doesn't turn off within tolerance.""" self._setup_switch(True) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(33) self.hass.block_till_done() @@ -291,7 +292,7 @@ class TestClimateGenericThermostat(unittest.TestCase): def test_temp_change_heater_off_outside_tolerance(self): """Test if temperature change turn heater off outside hot tolerance.""" self._setup_switch(True) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(35) self.hass.block_till_done() @@ -304,9 +305,9 @@ class TestClimateGenericThermostat(unittest.TestCase): def test_running_when_operating_mode_is_off(self): """Test that the switch turns off when enabled is set False.""" self._setup_switch(True) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() - climate.set_operation_mode(self.hass, STATE_OFF) + common.set_operation_mode(self.hass, STATE_OFF) self.hass.block_till_done() self.assertEqual(1, len(self.calls)) call = self.calls[0] @@ -317,9 +318,9 @@ class TestClimateGenericThermostat(unittest.TestCase): def test_no_state_change_when_operation_mode_off(self): """Test that the switch doesn't turn on when enabled is False.""" self._setup_switch(False) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() - climate.set_operation_mode(self.hass, STATE_OFF) + common.set_operation_mode(self.hass, STATE_OFF) self.hass.block_till_done() self._setup_sensor(25) self.hass.block_till_done() @@ -328,7 +329,7 @@ class TestClimateGenericThermostat(unittest.TestCase): @mock.patch('logging.Logger.error') def test_invalid_operating_mode(self, log_mock): """Test error handling for invalid operation mode.""" - climate.set_operation_mode(self.hass, 'invalid mode') + common.set_operation_mode(self.hass, 'invalid mode') self.hass.block_till_done() self.assertEqual(log_mock.call_count, 1) @@ -337,12 +338,12 @@ class TestClimateGenericThermostat(unittest.TestCase): Switch turns on when temp below setpoint and mode changes. """ - climate.set_operation_mode(self.hass, STATE_OFF) - climate.set_temperature(self.hass, 30) + common.set_operation_mode(self.hass, STATE_OFF) + common.set_temperature(self.hass, 30) self._setup_sensor(25) self.hass.block_till_done() self._setup_switch(False) - climate.set_operation_mode(self.hass, climate.STATE_HEAT) + common.set_operation_mode(self.hass, climate.STATE_HEAT) self.hass.block_till_done() self.assertEqual(1, len(self.calls)) call = self.calls[0] @@ -395,7 +396,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): self._setup_switch(True) self._setup_sensor(25) self.hass.block_till_done() - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self.assertEqual(2, len(self.calls)) call = self.calls[0] @@ -407,9 +408,9 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): """Test the setting away mode when cooling.""" self._setup_sensor(25) self.hass.block_till_done() - climate.set_temperature(self.hass, 19) + common.set_temperature(self.hass, 19) self.hass.block_till_done() - climate.set_away_mode(self.hass, True) + common.set_away_mode(self.hass, True) self.hass.block_till_done() state = self.hass.states.get(ENTITY) self.assertEqual(30, state.attributes.get('temperature')) @@ -419,12 +420,12 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): Switch turns on when temp below setpoint and mode changes. """ - climate.set_operation_mode(self.hass, STATE_OFF) - climate.set_temperature(self.hass, 25) + common.set_operation_mode(self.hass, STATE_OFF) + common.set_temperature(self.hass, 25) self._setup_sensor(30) self.hass.block_till_done() self._setup_switch(False) - climate.set_operation_mode(self.hass, climate.STATE_COOL) + common.set_operation_mode(self.hass, climate.STATE_COOL) self.hass.block_till_done() self.assertEqual(1, len(self.calls)) call = self.calls[0] @@ -437,7 +438,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): self._setup_switch(False) self._setup_sensor(30) self.hass.block_till_done() - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() self.assertEqual(1, len(self.calls)) call = self.calls[0] @@ -448,7 +449,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): def test_temp_change_ac_off_within_tolerance(self): """Test if temperature change doesn't turn ac off within tolerance.""" self._setup_switch(True) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(29.8) self.hass.block_till_done() @@ -457,7 +458,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): def test_set_temp_change_ac_off_outside_tolerance(self): """Test if temperature change turn ac off.""" self._setup_switch(True) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(27) self.hass.block_till_done() @@ -470,7 +471,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): def test_temp_change_ac_on_within_tolerance(self): """Test if temperature change doesn't turn ac on within tolerance.""" self._setup_switch(False) - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() self._setup_sensor(25.2) self.hass.block_till_done() @@ -479,7 +480,7 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): def test_temp_change_ac_on_outside_tolerance(self): """Test if temperature change turn ac on.""" self._setup_switch(False) - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() self._setup_sensor(30) self.hass.block_till_done() @@ -492,9 +493,9 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): def test_running_when_operating_mode_is_off(self): """Test that the switch turns off when enabled is set False.""" self._setup_switch(True) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() - climate.set_operation_mode(self.hass, STATE_OFF) + common.set_operation_mode(self.hass, STATE_OFF) self.hass.block_till_done() self.assertEqual(1, len(self.calls)) call = self.calls[0] @@ -505,9 +506,9 @@ class TestClimateGenericThermostatACMode(unittest.TestCase): def test_no_state_change_when_operation_mode_off(self): """Test that the switch doesn't turn on when enabled is False.""" self._setup_switch(False) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() - climate.set_operation_mode(self.hass, STATE_OFF) + common.set_operation_mode(self.hass, STATE_OFF) self.hass.block_till_done() self._setup_sensor(35) self.hass.block_till_done() @@ -556,7 +557,7 @@ class TestClimateGenericThermostatACModeMinCycle(unittest.TestCase): def test_temp_change_ac_trigger_on_not_long_enough(self): """Test if temperature change turn ac on.""" self._setup_switch(False) - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() self._setup_sensor(30) self.hass.block_till_done() @@ -569,7 +570,7 @@ class TestClimateGenericThermostatACModeMinCycle(unittest.TestCase): with mock.patch('homeassistant.helpers.condition.dt_util.utcnow', return_value=fake_changed): self._setup_switch(False) - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() self._setup_sensor(30) self.hass.block_till_done() @@ -582,7 +583,7 @@ class TestClimateGenericThermostatACModeMinCycle(unittest.TestCase): def test_temp_change_ac_trigger_off_not_long_enough(self): """Test if temperature change turn ac on.""" self._setup_switch(True) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(25) self.hass.block_till_done() @@ -595,7 +596,7 @@ class TestClimateGenericThermostatACModeMinCycle(unittest.TestCase): with mock.patch('homeassistant.helpers.condition.dt_util.utcnow', return_value=fake_changed): self._setup_switch(True) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(25) self.hass.block_till_done() @@ -647,7 +648,7 @@ class TestClimateGenericThermostatMinCycle(unittest.TestCase): def test_temp_change_heater_trigger_off_not_long_enough(self): """Test if temp change doesn't turn heater off because of time.""" self._setup_switch(True) - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() self._setup_sensor(30) self.hass.block_till_done() @@ -656,7 +657,7 @@ class TestClimateGenericThermostatMinCycle(unittest.TestCase): def test_temp_change_heater_trigger_on_not_long_enough(self): """Test if temp change doesn't turn heater on because of time.""" self._setup_switch(False) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(25) self.hass.block_till_done() @@ -669,7 +670,7 @@ class TestClimateGenericThermostatMinCycle(unittest.TestCase): with mock.patch('homeassistant.helpers.condition.dt_util.utcnow', return_value=fake_changed): self._setup_switch(False) - climate.set_temperature(self.hass, 30) + common.set_temperature(self.hass, 30) self.hass.block_till_done() self._setup_sensor(25) self.hass.block_till_done() @@ -686,7 +687,7 @@ class TestClimateGenericThermostatMinCycle(unittest.TestCase): with mock.patch('homeassistant.helpers.condition.dt_util.utcnow', return_value=fake_changed): self._setup_switch(True) - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() self._setup_sensor(30) self.hass.block_till_done() @@ -743,7 +744,7 @@ class TestClimateGenericThermostatACKeepAlive(unittest.TestCase): self.hass.block_till_done() self._setup_sensor(30) self.hass.block_till_done() - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() test_time = datetime.datetime.now(pytz.UTC) self._send_time_changed(test_time) @@ -766,7 +767,7 @@ class TestClimateGenericThermostatACKeepAlive(unittest.TestCase): self.hass.block_till_done() self._setup_sensor(20) self.hass.block_till_done() - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() test_time = datetime.datetime.now(pytz.UTC) self._send_time_changed(test_time) @@ -833,7 +834,7 @@ class TestClimateGenericThermostatKeepAlive(unittest.TestCase): self.hass.block_till_done() self._setup_sensor(20) self.hass.block_till_done() - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() test_time = datetime.datetime.now(pytz.UTC) self._send_time_changed(test_time) @@ -856,7 +857,7 @@ class TestClimateGenericThermostatKeepAlive(unittest.TestCase): self.hass.block_till_done() self._setup_sensor(30) self.hass.block_till_done() - climate.set_temperature(self.hass, 25) + common.set_temperature(self.hass, 25) self.hass.block_till_done() test_time = datetime.datetime.now(pytz.UTC) self._send_time_changed(test_time) @@ -926,7 +927,7 @@ class TestClimateGenericThermostatTurnOnOff(unittest.TestCase): def test_turn_on_when_off(self): """Test if climate.turn_on turns on a turned off device.""" - climate.set_operation_mode(self.hass, STATE_OFF) + common.set_operation_mode(self.hass, STATE_OFF) self.hass.block_till_done() self.hass.services.call('climate', SERVICE_TURN_ON) self.hass.block_till_done() @@ -939,8 +940,8 @@ class TestClimateGenericThermostatTurnOnOff(unittest.TestCase): def test_turn_on_when_on(self): """Test if climate.turn_on does nothing to a turned on device.""" - climate.set_operation_mode(self.hass, STATE_HEAT, self.HEAT_ENTITY) - climate.set_operation_mode(self.hass, STATE_COOL, self.COOL_ENTITY) + common.set_operation_mode(self.hass, STATE_HEAT, self.HEAT_ENTITY) + common.set_operation_mode(self.hass, STATE_COOL, self.COOL_ENTITY) self.hass.block_till_done() self.hass.services.call('climate', SERVICE_TURN_ON) self.hass.block_till_done() @@ -953,8 +954,8 @@ class TestClimateGenericThermostatTurnOnOff(unittest.TestCase): def test_turn_off_when_on(self): """Test if climate.turn_off turns off a turned on device.""" - climate.set_operation_mode(self.hass, STATE_HEAT, self.HEAT_ENTITY) - climate.set_operation_mode(self.hass, STATE_COOL, self.COOL_ENTITY) + common.set_operation_mode(self.hass, STATE_HEAT, self.HEAT_ENTITY) + common.set_operation_mode(self.hass, STATE_COOL, self.COOL_ENTITY) self.hass.block_till_done() self.hass.services.call('climate', SERVICE_TURN_OFF) self.hass.block_till_done() @@ -967,7 +968,7 @@ class TestClimateGenericThermostatTurnOnOff(unittest.TestCase): def test_turn_off_when_off(self): """Test if climate.turn_off does nothing to a turned off device.""" - climate.set_operation_mode(self.hass, STATE_OFF) + common.set_operation_mode(self.hass, STATE_OFF) self.hass.block_till_done() self.hass.services.call('climate', SERVICE_TURN_OFF) self.hass.block_till_done() diff --git a/tests/components/climate/test_mqtt.py b/tests/components/climate/test_mqtt.py index 6c43c4d9dbe..1fdf72e34ba 100644 --- a/tests/components/climate/test_mqtt.py +++ b/tests/components/climate/test_mqtt.py @@ -16,6 +16,7 @@ from homeassistant.components.mqtt.discovery import async_start from tests.common import (get_test_home_assistant, mock_mqtt_component, async_fire_mqtt_message, fire_mqtt_message, mock_component) +from tests.components.climate import common ENTITY_CLIMATE = 'climate.test' @@ -90,7 +91,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('operation_mode')) self.assertEqual("off", state.state) - climate.set_operation_mode(self.hass, None, ENTITY_CLIMATE) + common.set_operation_mode(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('operation_mode')) @@ -103,7 +104,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('operation_mode')) self.assertEqual("off", state.state) - climate.set_operation_mode(self.hass, "cool", ENTITY_CLIMATE) + common.set_operation_mode(self.hass, "cool", ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("cool", state.attributes.get('operation_mode')) @@ -121,7 +122,7 @@ class TestMQTTClimate(unittest.TestCase): self.assertEqual("off", state.attributes.get('operation_mode')) self.assertEqual("off", state.state) - climate.set_operation_mode(self.hass, "cool", ENTITY_CLIMATE) + common.set_operation_mode(self.hass, "cool", ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('operation_mode')) @@ -148,7 +149,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('operation_mode')) self.assertEqual("off", state.state) - climate.set_operation_mode(self.hass, "on", ENTITY_CLIMATE) + common.set_operation_mode(self.hass, "on", ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("on", state.attributes.get('operation_mode')) @@ -159,7 +160,7 @@ class TestMQTTClimate(unittest.TestCase): ]) self.mock_publish.async_publish.reset_mock() - climate.set_operation_mode(self.hass, "off", ENTITY_CLIMATE) + common.set_operation_mode(self.hass, "off", ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('operation_mode')) @@ -176,7 +177,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("low", state.attributes.get('fan_mode')) - climate.set_fan_mode(self.hass, None, ENTITY_CLIMATE) + common.set_fan_mode(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("low", state.attributes.get('fan_mode')) @@ -190,7 +191,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("low", state.attributes.get('fan_mode')) - climate.set_fan_mode(self.hass, 'high', ENTITY_CLIMATE) + common.set_fan_mode(self.hass, 'high', ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("low", state.attributes.get('fan_mode')) @@ -211,7 +212,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("low", state.attributes.get('fan_mode')) - climate.set_fan_mode(self.hass, 'high', ENTITY_CLIMATE) + common.set_fan_mode(self.hass, 'high', ENTITY_CLIMATE) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'fan-mode-topic', 'high', 0, False) @@ -224,7 +225,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('swing_mode')) - climate.set_swing_mode(self.hass, None, ENTITY_CLIMATE) + common.set_swing_mode(self.hass, None, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('swing_mode')) @@ -238,7 +239,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('swing_mode')) - climate.set_swing_mode(self.hass, 'on', ENTITY_CLIMATE) + common.set_swing_mode(self.hass, 'on', ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('swing_mode')) @@ -259,7 +260,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual("off", state.attributes.get('swing_mode')) - climate.set_swing_mode(self.hass, 'on', ENTITY_CLIMATE) + common.set_swing_mode(self.hass, 'on', ENTITY_CLIMATE) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'swing-mode-topic', 'on', 0, False) @@ -272,15 +273,15 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(21, state.attributes.get('temperature')) - climate.set_operation_mode(self.hass, 'heat', ENTITY_CLIMATE) + common.set_operation_mode(self.hass, 'heat', ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('heat', state.attributes.get('operation_mode')) self.mock_publish.async_publish.assert_called_once_with( 'mode-topic', 'heat', 0, False) self.mock_publish.async_publish.reset_mock() - climate.set_temperature(self.hass, temperature=47, - entity_id=ENTITY_CLIMATE) + common.set_temperature(self.hass, temperature=47, + entity_id=ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(47, state.attributes.get('temperature')) @@ -289,9 +290,9 @@ class TestMQTTClimate(unittest.TestCase): # also test directly supplying the operation mode to set_temperature self.mock_publish.async_publish.reset_mock() - climate.set_temperature(self.hass, temperature=21, - operation_mode="cool", - entity_id=ENTITY_CLIMATE) + common.set_temperature(self.hass, temperature=21, + operation_mode="cool", + entity_id=ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('cool', state.attributes.get('operation_mode')) @@ -310,10 +311,10 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(21, state.attributes.get('temperature')) - climate.set_operation_mode(self.hass, 'heat', ENTITY_CLIMATE) + common.set_operation_mode(self.hass, 'heat', ENTITY_CLIMATE) self.hass.block_till_done() - climate.set_temperature(self.hass, temperature=47, - entity_id=ENTITY_CLIMATE) + common.set_temperature(self.hass, temperature=47, + entity_id=ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(21, state.attributes.get('temperature')) @@ -349,7 +350,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('away_mode')) - climate.set_away_mode(self.hass, True, ENTITY_CLIMATE) + common.set_away_mode(self.hass, True, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('away_mode')) @@ -379,7 +380,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('away_mode')) - climate.set_away_mode(self.hass, True, ENTITY_CLIMATE) + common.set_away_mode(self.hass, True, ENTITY_CLIMATE) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'away-mode-topic', 'AN', 0, False) @@ -387,7 +388,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('on', state.attributes.get('away_mode')) - climate.set_away_mode(self.hass, False, ENTITY_CLIMATE) + common.set_away_mode(self.hass, False, ENTITY_CLIMATE) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'away-mode-topic', 'AUS', 0, False) @@ -403,7 +404,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(None, state.attributes.get('hold_mode')) - climate.set_hold_mode(self.hass, 'on', ENTITY_CLIMATE) + common.set_hold_mode(self.hass, 'on', ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(None, state.attributes.get('hold_mode')) @@ -424,7 +425,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual(None, state.attributes.get('hold_mode')) - climate.set_hold_mode(self.hass, 'on', ENTITY_CLIMATE) + common.set_hold_mode(self.hass, 'on', ENTITY_CLIMATE) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'hold-topic', 'on', 0, False) @@ -432,7 +433,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('on', state.attributes.get('hold_mode')) - climate.set_hold_mode(self.hass, 'off', ENTITY_CLIMATE) + common.set_hold_mode(self.hass, 'off', ENTITY_CLIMATE) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'hold-topic', 'off', 0, False) @@ -448,7 +449,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('aux_heat')) - climate.set_aux_heat(self.hass, True, ENTITY_CLIMATE) + common.set_aux_heat(self.hass, True, ENTITY_CLIMATE) self.hass.block_till_done() state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('aux_heat')) @@ -474,7 +475,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('off', state.attributes.get('aux_heat')) - climate.set_aux_heat(self.hass, True, ENTITY_CLIMATE) + common.set_aux_heat(self.hass, True, ENTITY_CLIMATE) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'aux-topic', 'ON', 0, False) @@ -482,7 +483,7 @@ class TestMQTTClimate(unittest.TestCase): state = self.hass.states.get(ENTITY_CLIMATE) self.assertEqual('on', state.attributes.get('aux_heat')) - climate.set_aux_heat(self.hass, False, ENTITY_CLIMATE) + common.set_aux_heat(self.hass, False, ENTITY_CLIMATE) self.hass.block_till_done() self.mock_publish.async_publish.assert_called_once_with( 'aux-topic', 'OFF', 0, False) diff --git a/tests/components/counter/common.py b/tests/components/counter/common.py new file mode 100644 index 00000000000..36d09979d0d --- /dev/null +++ b/tests/components/counter/common.py @@ -0,0 +1,52 @@ +"""Collection of helper methods. + +All containing methods are legacy helpers that should not be used by new +components. Instead call the service directly. +""" +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.components.counter import ( + DOMAIN, SERVICE_DECREMENT, SERVICE_INCREMENT, SERVICE_RESET) +from homeassistant.core import callback +from homeassistant.loader import bind_hass + + +@bind_hass +def increment(hass, entity_id): + """Increment a counter.""" + hass.add_job(async_increment, hass, entity_id) + + +@callback +@bind_hass +def async_increment(hass, entity_id): + """Increment a counter.""" + hass.async_add_job(hass.services.async_call( + DOMAIN, SERVICE_INCREMENT, {ATTR_ENTITY_ID: entity_id})) + + +@bind_hass +def decrement(hass, entity_id): + """Decrement a counter.""" + hass.add_job(async_decrement, hass, entity_id) + + +@callback +@bind_hass +def async_decrement(hass, entity_id): + """Decrement a counter.""" + hass.async_add_job(hass.services.async_call( + DOMAIN, SERVICE_DECREMENT, {ATTR_ENTITY_ID: entity_id})) + + +@bind_hass +def reset(hass, entity_id): + """Reset a counter.""" + hass.add_job(async_reset, hass, entity_id) + + +@callback +@bind_hass +def async_reset(hass, entity_id): + """Reset a counter.""" + hass.async_add_job(hass.services.async_call( + DOMAIN, SERVICE_RESET, {ATTR_ENTITY_ID: entity_id})) diff --git a/tests/components/counter/test_init.py b/tests/components/counter/test_init.py index af36c1c8f95..e5e0ee594ac 100644 --- a/tests/components/counter/test_init.py +++ b/tests/components/counter/test_init.py @@ -7,11 +7,11 @@ import logging from homeassistant.core import CoreState, State, Context from homeassistant.setup import setup_component, async_setup_component from homeassistant.components.counter import ( - DOMAIN, decrement, increment, reset, CONF_INITIAL, CONF_STEP, CONF_NAME, - CONF_ICON) + DOMAIN, CONF_INITIAL, CONF_STEP, CONF_NAME, CONF_ICON) from homeassistant.const import (ATTR_ICON, ATTR_FRIENDLY_NAME) from tests.common import (get_test_home_assistant, mock_restore_cache) +from tests.components.counter.common import decrement, increment, reset _LOGGER = logging.getLogger(__name__) diff --git a/tests/components/fan/common.py b/tests/components/fan/common.py new file mode 100644 index 00000000000..d4caf28be5b --- /dev/null +++ b/tests/components/fan/common.py @@ -0,0 +1,82 @@ +"""Collection of helper methods. + +All containing methods are legacy helpers that should not be used by new +components. Instead call the service directly. +""" +from homeassistant.components.fan import ( + ATTR_DIRECTION, ATTR_SPEED, ATTR_OSCILLATING, DOMAIN, + SERVICE_OSCILLATE, SERVICE_SET_DIRECTION, SERVICE_SET_SPEED) +from homeassistant.const import ( + ATTR_ENTITY_ID, SERVICE_TURN_ON, SERVICE_TOGGLE, SERVICE_TURN_OFF) +from homeassistant.loader import bind_hass + + +@bind_hass +def turn_on(hass, entity_id: str = None, speed: str = None) -> None: + """Turn all or specified fan on.""" + data = { + key: value for key, value in [ + (ATTR_ENTITY_ID, entity_id), + (ATTR_SPEED, speed), + ] if value is not None + } + + hass.services.call(DOMAIN, SERVICE_TURN_ON, data) + + +@bind_hass +def turn_off(hass, entity_id: str = None) -> None: + """Turn all or specified fan off.""" + data = {ATTR_ENTITY_ID: entity_id} if entity_id else {} + + hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) + + +@bind_hass +def toggle(hass, entity_id: str = None) -> None: + """Toggle all or specified fans.""" + data = { + ATTR_ENTITY_ID: entity_id + } + + hass.services.call(DOMAIN, SERVICE_TOGGLE, data) + + +@bind_hass +def oscillate(hass, entity_id: str = None, + should_oscillate: bool = True) -> None: + """Set oscillation on all or specified fan.""" + data = { + key: value for key, value in [ + (ATTR_ENTITY_ID, entity_id), + (ATTR_OSCILLATING, should_oscillate), + ] if value is not None + } + + hass.services.call(DOMAIN, SERVICE_OSCILLATE, data) + + +@bind_hass +def set_speed(hass, entity_id: str = None, speed: str = None) -> None: + """Set speed for all or specified fan.""" + data = { + key: value for key, value in [ + (ATTR_ENTITY_ID, entity_id), + (ATTR_SPEED, speed), + ] if value is not None + } + + hass.services.call(DOMAIN, SERVICE_SET_SPEED, data) + + +@bind_hass +def set_direction(hass, entity_id: str = None, direction: str = None) -> None: + """Set direction for all or specified fan.""" + data = { + key: value for key, value in [ + (ATTR_ENTITY_ID, entity_id), + (ATTR_DIRECTION, direction), + ] if value is not None + } + + hass.services.call(DOMAIN, SERVICE_SET_DIRECTION, data) diff --git a/tests/components/fan/test_demo.py b/tests/components/fan/test_demo.py index 69680fb1cfd..48704ca4464 100644 --- a/tests/components/fan/test_demo.py +++ b/tests/components/fan/test_demo.py @@ -7,6 +7,7 @@ from homeassistant.components import fan from homeassistant.const import STATE_OFF, STATE_ON from tests.common import get_test_home_assistant +from tests.components.fan import common FAN_ENTITY_ID = 'fan.living_room_fan' @@ -34,11 +35,11 @@ class TestDemoFan(unittest.TestCase): """Test turning on the device.""" self.assertEqual(STATE_OFF, self.get_entity().state) - fan.turn_on(self.hass, FAN_ENTITY_ID) + common.turn_on(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertNotEqual(STATE_OFF, self.get_entity().state) - fan.turn_on(self.hass, FAN_ENTITY_ID, fan.SPEED_HIGH) + common.turn_on(self.hass, FAN_ENTITY_ID, fan.SPEED_HIGH) self.hass.block_till_done() self.assertEqual(STATE_ON, self.get_entity().state) self.assertEqual(fan.SPEED_HIGH, @@ -48,11 +49,11 @@ class TestDemoFan(unittest.TestCase): """Test turning off the device.""" self.assertEqual(STATE_OFF, self.get_entity().state) - fan.turn_on(self.hass, FAN_ENTITY_ID) + common.turn_on(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertNotEqual(STATE_OFF, self.get_entity().state) - fan.turn_off(self.hass, FAN_ENTITY_ID) + common.turn_off(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertEqual(STATE_OFF, self.get_entity().state) @@ -60,11 +61,11 @@ class TestDemoFan(unittest.TestCase): """Test turning off all fans.""" self.assertEqual(STATE_OFF, self.get_entity().state) - fan.turn_on(self.hass, FAN_ENTITY_ID) + common.turn_on(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertNotEqual(STATE_OFF, self.get_entity().state) - fan.turn_off(self.hass) + common.turn_off(self.hass) self.hass.block_till_done() self.assertEqual(STATE_OFF, self.get_entity().state) @@ -72,7 +73,7 @@ class TestDemoFan(unittest.TestCase): """Test setting the direction of the device.""" self.assertEqual(STATE_OFF, self.get_entity().state) - fan.set_direction(self.hass, FAN_ENTITY_ID, fan.DIRECTION_REVERSE) + common.set_direction(self.hass, FAN_ENTITY_ID, fan.DIRECTION_REVERSE) self.hass.block_till_done() self.assertEqual(fan.DIRECTION_REVERSE, self.get_entity().attributes.get('direction')) @@ -81,7 +82,7 @@ class TestDemoFan(unittest.TestCase): """Test setting the speed of the device.""" self.assertEqual(STATE_OFF, self.get_entity().state) - fan.set_speed(self.hass, FAN_ENTITY_ID, fan.SPEED_LOW) + common.set_speed(self.hass, FAN_ENTITY_ID, fan.SPEED_LOW) self.hass.block_till_done() self.assertEqual(fan.SPEED_LOW, self.get_entity().attributes.get('speed')) @@ -90,11 +91,11 @@ class TestDemoFan(unittest.TestCase): """Test oscillating the fan.""" self.assertFalse(self.get_entity().attributes.get('oscillating')) - fan.oscillate(self.hass, FAN_ENTITY_ID, True) + common.oscillate(self.hass, FAN_ENTITY_ID, True) self.hass.block_till_done() self.assertTrue(self.get_entity().attributes.get('oscillating')) - fan.oscillate(self.hass, FAN_ENTITY_ID, False) + common.oscillate(self.hass, FAN_ENTITY_ID, False) self.hass.block_till_done() self.assertFalse(self.get_entity().attributes.get('oscillating')) @@ -102,6 +103,6 @@ class TestDemoFan(unittest.TestCase): """Test is on service call.""" self.assertFalse(fan.is_on(self.hass, FAN_ENTITY_ID)) - fan.turn_on(self.hass, FAN_ENTITY_ID) + common.turn_on(self.hass, FAN_ENTITY_ID) self.hass.block_till_done() self.assertTrue(fan.is_on(self.hass, FAN_ENTITY_ID)) diff --git a/tests/components/fan/test_template.py b/tests/components/fan/test_template.py index e229083069d..09d3603e004 100644 --- a/tests/components/fan/test_template.py +++ b/tests/components/fan/test_template.py @@ -3,7 +3,6 @@ import logging from homeassistant.core import callback from homeassistant import setup -import homeassistant.components as components from homeassistant.const import STATE_ON, STATE_OFF from homeassistant.components.fan import ( ATTR_SPEED, ATTR_OSCILLATING, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH, @@ -11,6 +10,8 @@ from homeassistant.components.fan import ( from tests.common import ( get_test_home_assistant, assert_setup_component) +from tests.components.fan import common + _LOGGER = logging.getLogger(__name__) @@ -288,7 +289,7 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # verify @@ -296,7 +297,7 @@ class TestTemplateFan: self._verify(STATE_ON, None, None, None) # Turn off fan - components.fan.turn_off(self.hass, _TEST_FAN) + common.turn_off(self.hass, _TEST_FAN) self.hass.block_till_done() # verify @@ -308,7 +309,7 @@ class TestTemplateFan: self._register_components() # Turn on fan with high speed - components.fan.turn_on(self.hass, _TEST_FAN, SPEED_HIGH) + common.turn_on(self.hass, _TEST_FAN, SPEED_HIGH) self.hass.block_till_done() # verify @@ -321,11 +322,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's speed to high - components.fan.set_speed(self.hass, _TEST_FAN, SPEED_HIGH) + common.set_speed(self.hass, _TEST_FAN, SPEED_HIGH) self.hass.block_till_done() # verify @@ -333,7 +334,7 @@ class TestTemplateFan: self._verify(STATE_ON, SPEED_HIGH, None, None) # Set fan's speed to medium - components.fan.set_speed(self.hass, _TEST_FAN, SPEED_MEDIUM) + common.set_speed(self.hass, _TEST_FAN, SPEED_MEDIUM) self.hass.block_till_done() # verify @@ -345,11 +346,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's speed to 'invalid' - components.fan.set_speed(self.hass, _TEST_FAN, 'invalid') + common.set_speed(self.hass, _TEST_FAN, 'invalid') self.hass.block_till_done() # verify speed is unchanged @@ -361,11 +362,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's speed to high - components.fan.set_speed(self.hass, _TEST_FAN, SPEED_HIGH) + common.set_speed(self.hass, _TEST_FAN, SPEED_HIGH) self.hass.block_till_done() # verify @@ -373,7 +374,7 @@ class TestTemplateFan: self._verify(STATE_ON, SPEED_HIGH, None, None) # Set fan's speed to 'invalid' - components.fan.set_speed(self.hass, _TEST_FAN, 'invalid') + common.set_speed(self.hass, _TEST_FAN, 'invalid') self.hass.block_till_done() # verify speed is unchanged @@ -385,11 +386,11 @@ class TestTemplateFan: self._register_components(['1', '2', '3']) # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's speed to '1' - components.fan.set_speed(self.hass, _TEST_FAN, '1') + common.set_speed(self.hass, _TEST_FAN, '1') self.hass.block_till_done() # verify @@ -397,7 +398,7 @@ class TestTemplateFan: self._verify(STATE_ON, '1', None, None) # Set fan's speed to 'medium' which is invalid - components.fan.set_speed(self.hass, _TEST_FAN, SPEED_MEDIUM) + common.set_speed(self.hass, _TEST_FAN, SPEED_MEDIUM) self.hass.block_till_done() # verify that speed is unchanged @@ -409,11 +410,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's osc to True - components.fan.oscillate(self.hass, _TEST_FAN, True) + common.oscillate(self.hass, _TEST_FAN, True) self.hass.block_till_done() # verify @@ -421,7 +422,7 @@ class TestTemplateFan: self._verify(STATE_ON, None, True, None) # Set fan's osc to False - components.fan.oscillate(self.hass, _TEST_FAN, False) + common.oscillate(self.hass, _TEST_FAN, False) self.hass.block_till_done() # verify @@ -433,11 +434,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's osc to 'invalid' - components.fan.oscillate(self.hass, _TEST_FAN, 'invalid') + common.oscillate(self.hass, _TEST_FAN, 'invalid') self.hass.block_till_done() # verify @@ -449,11 +450,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's osc to True - components.fan.oscillate(self.hass, _TEST_FAN, True) + common.oscillate(self.hass, _TEST_FAN, True) self.hass.block_till_done() # verify @@ -461,7 +462,7 @@ class TestTemplateFan: self._verify(STATE_ON, None, True, None) # Set fan's osc to False - components.fan.oscillate(self.hass, _TEST_FAN, None) + common.oscillate(self.hass, _TEST_FAN, None) self.hass.block_till_done() # verify osc is unchanged @@ -473,11 +474,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's direction to forward - components.fan.set_direction(self.hass, _TEST_FAN, DIRECTION_FORWARD) + common.set_direction(self.hass, _TEST_FAN, DIRECTION_FORWARD) self.hass.block_till_done() # verify @@ -486,7 +487,7 @@ class TestTemplateFan: self._verify(STATE_ON, None, None, DIRECTION_FORWARD) # Set fan's direction to reverse - components.fan.set_direction(self.hass, _TEST_FAN, DIRECTION_REVERSE) + common.set_direction(self.hass, _TEST_FAN, DIRECTION_REVERSE) self.hass.block_till_done() # verify @@ -499,11 +500,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's direction to 'invalid' - components.fan.set_direction(self.hass, _TEST_FAN, 'invalid') + common.set_direction(self.hass, _TEST_FAN, 'invalid') self.hass.block_till_done() # verify direction is unchanged @@ -515,11 +516,11 @@ class TestTemplateFan: self._register_components() # Turn on fan - components.fan.turn_on(self.hass, _TEST_FAN) + common.turn_on(self.hass, _TEST_FAN) self.hass.block_till_done() # Set fan's direction to forward - components.fan.set_direction(self.hass, _TEST_FAN, DIRECTION_FORWARD) + common.set_direction(self.hass, _TEST_FAN, DIRECTION_FORWARD) self.hass.block_till_done() # verify @@ -528,7 +529,7 @@ class TestTemplateFan: self._verify(STATE_ON, None, None, DIRECTION_FORWARD) # Set fan's direction to 'invalid' - components.fan.set_direction(self.hass, _TEST_FAN, 'invalid') + common.set_direction(self.hass, _TEST_FAN, 'invalid') self.hass.block_till_done() # verify direction is unchanged