Remove service helper (5) (#16917)

* Update switch

* Update script

* Update light

* Fix tests

* Fix config/script hook

* Async_create_task

* Fix flux switch
This commit is contained in:
cdce8p 2018-09-27 23:13:11 +02:00 committed by Paulus Schoutsen
parent a7248d4574
commit 70b901017f
27 changed files with 385 additions and 316 deletions

View file

@ -2,7 +2,8 @@
import asyncio import asyncio
from homeassistant.components.config import EditKeyBasedConfigView from homeassistant.components.config import EditKeyBasedConfigView
from homeassistant.components.script import SCRIPT_ENTRY_SCHEMA, async_reload from homeassistant.components.script import DOMAIN, SCRIPT_ENTRY_SCHEMA
from homeassistant.const import SERVICE_RELOAD
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -12,8 +13,12 @@ CONFIG_PATH = 'scripts.yaml'
@asyncio.coroutine @asyncio.coroutine
def async_setup(hass): def async_setup(hass):
"""Set up the script config API.""" """Set up the script config API."""
async def hook(hass):
"""post_write_hook for Config View that reloads scripts."""
await hass.services.async_call(DOMAIN, SERVICE_RELOAD)
hass.http.register_view(EditKeyBasedConfigView( hass.http.register_view(EditKeyBasedConfigView(
'script', 'config', CONFIG_PATH, cv.slug, SCRIPT_ENTRY_SCHEMA, 'script', 'config', CONFIG_PATH, cv.slug, SCRIPT_ENTRY_SCHEMA,
post_write_hook=async_reload post_write_hook=hook
)) ))
return True return True

View file

@ -12,7 +12,9 @@ import voluptuous as vol
from homeassistant.core import callback from homeassistant.core import callback
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.const import STATE_HOME, STATE_NOT_HOME from homeassistant.const import (
SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_HOME, STATE_NOT_HOME)
from homeassistant.components.light import DOMAIN as DOMAIN_LIGHT
from homeassistant.helpers.event import ( from homeassistant.helpers.event import (
async_track_point_in_utc_time, async_track_state_change) async_track_point_in_utc_time, async_track_state_change)
from homeassistant.helpers.sun import is_up, get_astral_event_next from homeassistant.helpers.sun import is_up, get_astral_event_next
@ -86,9 +88,12 @@ def async_setup(hass, config):
"""Turn on lights.""" """Turn on lights."""
if not device_tracker.is_on() or light.is_on(light_id): if not device_tracker.is_on() or light.is_on(light_id):
return return
light.async_turn_on(light_id, hass.async_create_task(
hass.services.async_call(
DOMAIN_LIGHT, SERVICE_TURN_ON, dict(
entity_id=light_id,
transition=LIGHT_TRANSITION_TIME.seconds, transition=LIGHT_TRANSITION_TIME.seconds,
profile=light_profile) profile=light_profile)))
def async_turn_on_factory(light_id): def async_turn_on_factory(light_id):
"""Generate turn on callbacks as factory.""" """Generate turn on callbacks as factory."""
@ -138,7 +143,10 @@ def async_setup(hass, config):
# Do we need lights? # Do we need lights?
if light_needed: if light_needed:
logger.info("Home coming event for %s. Turning lights on", entity) logger.info("Home coming event for %s. Turning lights on", entity)
light.async_turn_on(light_ids, profile=light_profile) hass.async_create_task(
hass.services.async_call(
DOMAIN_LIGHT, SERVICE_TURN_ON,
dict(entity_id=light_ids, profile=light_profile)))
# Are we in the time span were we would turn on the lights # Are we in the time span were we would turn on the lights
# if someone would be home? # if someone would be home?
@ -151,7 +159,10 @@ def async_setup(hass, config):
# when the fading in started and turn it on if so # when the fading in started and turn it on if so
for index, light_id in enumerate(light_ids): for index, light_id in enumerate(light_ids):
if now > start_point + index * LIGHT_TRANSITION_TIME: if now > start_point + index * LIGHT_TRANSITION_TIME:
light.async_turn_on(light_id) hass.async_create_task(
hass.services.async_call(
DOMAIN_LIGHT, SERVICE_TURN_ON,
dict(entity_id=light_id)))
else: else:
# If this light didn't happen to be turned on yet so # If this light didn't happen to be turned on yet so
@ -173,7 +184,9 @@ def async_setup(hass, config):
logger.info( logger.info(
"Everyone has left but there are lights on. Turning them off") "Everyone has left but there are lights on. Turning them off")
light.async_turn_off(light_ids) hass.async_create_task(
hass.services.async_call(
DOMAIN_LIGHT, SERVICE_TURN_OFF, dict(entity_id=light_ids)))
async_track_state_change( async_track_state_change(
hass, device_group, turn_off_lights_when_all_leave, hass, device_group, turn_off_lights_when_all_leave,

View file

@ -17,7 +17,6 @@ from homeassistant.components.group import \
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON, ATTR_ENTITY_ID, SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON,
STATE_ON) STATE_ON)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
@ -142,90 +141,6 @@ def is_on(hass, entity_id=None):
return hass.states.is_state(entity_id, STATE_ON) return hass.states.is_state(entity_id, STATE_ON)
@bind_hass
def turn_on(hass, entity_id=None, transition=None, brightness=None,
brightness_pct=None, rgb_color=None, xy_color=None, hs_color=None,
color_temp=None, kelvin=None, white_value=None,
profile=None, flash=None, effect=None, color_name=None):
"""Turn all or specified light on."""
hass.add_job(
async_turn_on, hass, entity_id, transition, brightness, brightness_pct,
rgb_color, xy_color, hs_color, color_temp, kelvin, white_value,
profile, flash, effect, color_name)
@callback
@bind_hass
def async_turn_on(hass, entity_id=None, transition=None, brightness=None,
brightness_pct=None, rgb_color=None, xy_color=None,
hs_color=None, color_temp=None, kelvin=None,
white_value=None, profile=None, flash=None, effect=None,
color_name=None):
"""Turn all or specified light on."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_PROFILE, profile),
(ATTR_TRANSITION, transition),
(ATTR_BRIGHTNESS, brightness),
(ATTR_BRIGHTNESS_PCT, brightness_pct),
(ATTR_RGB_COLOR, rgb_color),
(ATTR_XY_COLOR, xy_color),
(ATTR_HS_COLOR, hs_color),
(ATTR_COLOR_TEMP, color_temp),
(ATTR_KELVIN, kelvin),
(ATTR_WHITE_VALUE, white_value),
(ATTR_FLASH, flash),
(ATTR_EFFECT, effect),
(ATTR_COLOR_NAME, color_name),
] if value is not None
}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
@bind_hass
def turn_off(hass, entity_id=None, transition=None):
"""Turn all or specified light off."""
hass.add_job(async_turn_off, hass, entity_id, transition)
@callback
@bind_hass
def async_turn_off(hass, entity_id=None, transition=None):
"""Turn all or specified light off."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
] if value is not None
}
hass.async_add_job(hass.services.async_call(
DOMAIN, SERVICE_TURN_OFF, data))
@callback
@bind_hass
def async_toggle(hass, entity_id=None, transition=None):
"""Toggle all or specified light."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
] if value is not None
}
hass.async_add_job(hass.services.async_call(
DOMAIN, SERVICE_TOGGLE, data))
@bind_hass
def toggle(hass, entity_id=None, transition=None):
"""Toggle all or specified light."""
hass.add_job(async_toggle, hass, entity_id, transition)
def preprocess_turn_on_alternatives(params): def preprocess_turn_on_alternatives(params):
"""Process extra data for turn light on request.""" """Process extra data for turn light on request."""
profile = Profiles.get(params.pop(ATTR_PROFILE, None)) profile = Profiles.get(params.pop(ATTR_PROFILE, None))

View file

@ -15,7 +15,6 @@ import voluptuous as vol
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON,
SERVICE_TOGGLE, SERVICE_RELOAD, STATE_ON, CONF_ALIAS) SERVICE_TOGGLE, SERVICE_RELOAD, STATE_ON, CONF_ALIAS)
from homeassistant.core import split_entity_id
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
@ -62,41 +61,6 @@ def is_on(hass, entity_id):
return hass.states.is_state(entity_id, STATE_ON) return hass.states.is_state(entity_id, STATE_ON)
@bind_hass
def turn_on(hass, entity_id, variables=None, context=None):
"""Turn script on."""
_, object_id = split_entity_id(entity_id)
hass.services.call(DOMAIN, object_id, variables, context=context)
@bind_hass
def turn_off(hass, entity_id):
"""Turn script on."""
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
@bind_hass
def toggle(hass, entity_id):
"""Toggle the script."""
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
@bind_hass
def reload(hass):
"""Reload script component."""
hass.services.call(DOMAIN, SERVICE_RELOAD)
@bind_hass
def async_reload(hass):
"""Reload the scripts from config.
Returns a coroutine object.
"""
return hass.services.async_call(DOMAIN, SERVICE_RELOAD)
async def async_setup(hass, config): async def async_setup(hass, config):
"""Load the scripts from the configuration.""" """Load the scripts from the configuration."""
component = EntityComponent( component = EntityComponent(

View file

@ -9,7 +9,6 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.core import callback
from homeassistant.loader import bind_hass from homeassistant.loader import bind_hass
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
@ -56,42 +55,6 @@ def is_on(hass, entity_id=None):
return hass.states.is_state(entity_id, STATE_ON) return hass.states.is_state(entity_id, STATE_ON)
@bind_hass
def turn_on(hass, entity_id=None):
"""Turn all or specified switch on."""
hass.add_job(async_turn_on, hass, entity_id)
@callback
@bind_hass
def async_turn_on(hass, entity_id=None):
"""Turn all or specified switch on."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
@bind_hass
def turn_off(hass, entity_id=None):
"""Turn all or specified switch off."""
hass.add_job(async_turn_off, hass, entity_id)
@callback
@bind_hass
def async_turn_off(hass, entity_id=None):
"""Turn all or specified switch off."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.async_add_job(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data))
@bind_hass
def toggle(hass, entity_id=None):
"""Toggle all or specified switch."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.services.call(DOMAIN, SERVICE_TOGGLE, data)
async def async_setup(hass, config): async def async_setup(hass, config):
"""Track states and offer events for switches.""" """Track states and offer events for switches."""
component = hass.data[DOMAIN] = EntityComponent( component = hass.data[DOMAIN] = EntityComponent(

View file

@ -13,10 +13,10 @@ import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.components.light import ( from homeassistant.components.light import (
is_on, turn_on, VALID_TRANSITION, ATTR_TRANSITION) is_on, DOMAIN as LIGHT_DOMAIN, VALID_TRANSITION, ATTR_TRANSITION)
from homeassistant.components.switch import DOMAIN, SwitchDevice from homeassistant.components.switch import DOMAIN, SwitchDevice
from homeassistant.const import ( from homeassistant.const import (
CONF_NAME, CONF_PLATFORM, CONF_LIGHTS, CONF_MODE) CONF_NAME, CONF_PLATFORM, CONF_LIGHTS, CONF_MODE, SERVICE_TURN_ON)
from homeassistant.helpers.event import track_time_change from homeassistant.helpers.event import track_time_change
from homeassistant.helpers.sun import get_astral_event_date from homeassistant.helpers.sun import get_astral_event_date
from homeassistant.util import slugify from homeassistant.util import slugify
@ -69,30 +69,36 @@ def set_lights_xy(hass, lights, x_val, y_val, brightness, transition):
"""Set color of array of lights.""" """Set color of array of lights."""
for light in lights: for light in lights:
if is_on(hass, light): if is_on(hass, light):
turn_on(hass, light, hass.services.call(
LIGHT_DOMAIN, SERVICE_TURN_ON, dict(
xy_color=[x_val, y_val], xy_color=[x_val, y_val],
brightness=brightness, brightness=brightness,
transition=transition, transition=transition,
white_value=brightness) white_value=brightness,
entity_id=light))
def set_lights_temp(hass, lights, mired, brightness, transition): def set_lights_temp(hass, lights, mired, brightness, transition):
"""Set color of array of lights.""" """Set color of array of lights."""
for light in lights: for light in lights:
if is_on(hass, light): if is_on(hass, light):
turn_on(hass, light, hass.services.call(
LIGHT_DOMAIN, SERVICE_TURN_ON, dict(
color_temp=int(mired), color_temp=int(mired),
brightness=brightness, brightness=brightness,
transition=transition) transition=transition,
entity_id=light))
def set_lights_rgb(hass, lights, rgb, transition): def set_lights_rgb(hass, lights, rgb, transition):
"""Set color of array of lights.""" """Set color of array of lights."""
for light in lights: for light in lights:
if is_on(hass, light): if is_on(hass, light):
turn_on(hass, light, hass.services.call(
LIGHT_DOMAIN, SERVICE_TURN_ON, dict(
rgb_color=rgb, rgb_color=rgb,
transition=transition) transition=transition,
entity_id=light))
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):

View file

@ -0,0 +1,97 @@
"""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.light import (
ATTR_BRIGHTNESS, ATTR_BRIGHTNESS_PCT, ATTR_COLOR_NAME, ATTR_COLOR_TEMP,
ATTR_EFFECT, ATTR_FLASH, ATTR_HS_COLOR, ATTR_KELVIN, ATTR_PROFILE,
ATTR_RGB_COLOR, ATTR_TRANSITION, ATTR_WHITE_VALUE, ATTR_XY_COLOR, DOMAIN)
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON)
from homeassistant.core import callback
from homeassistant.loader import bind_hass
@bind_hass
def turn_on(hass, entity_id=None, transition=None, brightness=None,
brightness_pct=None, rgb_color=None, xy_color=None, hs_color=None,
color_temp=None, kelvin=None, white_value=None,
profile=None, flash=None, effect=None, color_name=None):
"""Turn all or specified light on."""
hass.add_job(
async_turn_on, hass, entity_id, transition, brightness, brightness_pct,
rgb_color, xy_color, hs_color, color_temp, kelvin, white_value,
profile, flash, effect, color_name)
@callback
@bind_hass
def async_turn_on(hass, entity_id=None, transition=None, brightness=None,
brightness_pct=None, rgb_color=None, xy_color=None,
hs_color=None, color_temp=None, kelvin=None,
white_value=None, profile=None, flash=None, effect=None,
color_name=None):
"""Turn all or specified light on."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_PROFILE, profile),
(ATTR_TRANSITION, transition),
(ATTR_BRIGHTNESS, brightness),
(ATTR_BRIGHTNESS_PCT, brightness_pct),
(ATTR_RGB_COLOR, rgb_color),
(ATTR_XY_COLOR, xy_color),
(ATTR_HS_COLOR, hs_color),
(ATTR_COLOR_TEMP, color_temp),
(ATTR_KELVIN, kelvin),
(ATTR_WHITE_VALUE, white_value),
(ATTR_FLASH, flash),
(ATTR_EFFECT, effect),
(ATTR_COLOR_NAME, color_name),
] if value is not None
}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
@bind_hass
def turn_off(hass, entity_id=None, transition=None):
"""Turn all or specified light off."""
hass.add_job(async_turn_off, hass, entity_id, transition)
@callback
@bind_hass
def async_turn_off(hass, entity_id=None, transition=None):
"""Turn all or specified light off."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
] if value is not None
}
hass.async_add_job(hass.services.async_call(
DOMAIN, SERVICE_TURN_OFF, data))
@bind_hass
def toggle(hass, entity_id=None, transition=None):
"""Toggle all or specified light."""
hass.add_job(async_toggle, hass, entity_id, transition)
@callback
@bind_hass
def async_toggle(hass, entity_id=None, transition=None):
"""Toggle all or specified light."""
data = {
key: value for key, value in [
(ATTR_ENTITY_ID, entity_id),
(ATTR_TRANSITION, transition),
] if value is not None
}
hass.async_add_job(hass.services.async_call(
DOMAIN, SERVICE_TOGGLE, data))

View file

@ -4,6 +4,8 @@ import pytest
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from homeassistant.components import light from homeassistant.components import light
from tests.components.light import common
ENTITY_LIGHT = 'light.bed_light' ENTITY_LIGHT = 'light.bed_light'
@ -18,7 +20,7 @@ def setup_comp(hass):
async def test_state_attributes(hass): async def test_state_attributes(hass):
"""Test light state attributes.""" """Test light state attributes."""
light.async_turn_on( common.async_turn_on(
hass, ENTITY_LIGHT, xy_color=(.4, .4), brightness=25) hass, ENTITY_LIGHT, xy_color=(.4, .4), brightness=25)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(ENTITY_LIGHT) state = hass.states.get(ENTITY_LIGHT)
@ -27,7 +29,7 @@ async def test_state_attributes(hass):
assert 25 == state.attributes.get(light.ATTR_BRIGHTNESS) assert 25 == state.attributes.get(light.ATTR_BRIGHTNESS)
assert (255, 234, 164) == state.attributes.get(light.ATTR_RGB_COLOR) assert (255, 234, 164) == state.attributes.get(light.ATTR_RGB_COLOR)
assert 'rainbow' == state.attributes.get(light.ATTR_EFFECT) assert 'rainbow' == state.attributes.get(light.ATTR_EFFECT)
light.async_turn_on( common.async_turn_on(
hass, ENTITY_LIGHT, rgb_color=(251, 253, 255), hass, ENTITY_LIGHT, rgb_color=(251, 253, 255),
white_value=254) white_value=254)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -35,14 +37,14 @@ async def test_state_attributes(hass):
assert 254 == state.attributes.get(light.ATTR_WHITE_VALUE) assert 254 == state.attributes.get(light.ATTR_WHITE_VALUE)
assert (250, 252, 255) == state.attributes.get(light.ATTR_RGB_COLOR) assert (250, 252, 255) == state.attributes.get(light.ATTR_RGB_COLOR)
assert (0.319, 0.326) == state.attributes.get(light.ATTR_XY_COLOR) assert (0.319, 0.326) == state.attributes.get(light.ATTR_XY_COLOR)
light.async_turn_on(hass, ENTITY_LIGHT, color_temp=400, effect='none') common.async_turn_on(hass, ENTITY_LIGHT, color_temp=400, effect='none')
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(ENTITY_LIGHT) state = hass.states.get(ENTITY_LIGHT)
assert 400 == state.attributes.get(light.ATTR_COLOR_TEMP) assert 400 == state.attributes.get(light.ATTR_COLOR_TEMP)
assert 153 == state.attributes.get(light.ATTR_MIN_MIREDS) assert 153 == state.attributes.get(light.ATTR_MIN_MIREDS)
assert 500 == state.attributes.get(light.ATTR_MAX_MIREDS) assert 500 == state.attributes.get(light.ATTR_MAX_MIREDS)
assert 'none' == state.attributes.get(light.ATTR_EFFECT) assert 'none' == state.attributes.get(light.ATTR_EFFECT)
light.async_turn_on(hass, ENTITY_LIGHT, kelvin=3000, brightness_pct=50) common.async_turn_on(hass, ENTITY_LIGHT, kelvin=3000, brightness_pct=50)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(ENTITY_LIGHT) state = hass.states.get(ENTITY_LIGHT)
assert 333 == state.attributes.get(light.ATTR_COLOR_TEMP) assert 333 == state.attributes.get(light.ATTR_COLOR_TEMP)

View file

@ -3,10 +3,11 @@ from unittest.mock import MagicMock
import asynctest import asynctest
from homeassistant.components import light
from homeassistant.components.light import group from homeassistant.components.light import group
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.components.light import common
async def test_default_state(hass): async def test_default_state(hass):
"""Test light group default state.""" """Test light group default state."""
@ -300,28 +301,28 @@ async def test_service_calls(hass):
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get('light.light_group').state == 'on' assert hass.states.get('light.light_group').state == 'on'
light.async_toggle(hass, 'light.light_group') common.async_toggle(hass, 'light.light_group')
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get('light.bed_light').state == 'off' assert hass.states.get('light.bed_light').state == 'off'
assert hass.states.get('light.ceiling_lights').state == 'off' assert hass.states.get('light.ceiling_lights').state == 'off'
assert hass.states.get('light.kitchen_lights').state == 'off' assert hass.states.get('light.kitchen_lights').state == 'off'
light.async_turn_on(hass, 'light.light_group') common.async_turn_on(hass, 'light.light_group')
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get('light.bed_light').state == 'on' assert hass.states.get('light.bed_light').state == 'on'
assert hass.states.get('light.ceiling_lights').state == 'on' assert hass.states.get('light.ceiling_lights').state == 'on'
assert hass.states.get('light.kitchen_lights').state == 'on' assert hass.states.get('light.kitchen_lights').state == 'on'
light.async_turn_off(hass, 'light.light_group') common.async_turn_off(hass, 'light.light_group')
await hass.async_block_till_done() await hass.async_block_till_done()
assert hass.states.get('light.bed_light').state == 'off' assert hass.states.get('light.bed_light').state == 'off'
assert hass.states.get('light.ceiling_lights').state == 'off' assert hass.states.get('light.ceiling_lights').state == 'off'
assert hass.states.get('light.kitchen_lights').state == 'off' assert hass.states.get('light.kitchen_lights').state == 'off'
light.async_turn_on(hass, 'light.light_group', brightness=128, common.async_turn_on(hass, 'light.light_group', brightness=128,
effect='Random', rgb_color=(42, 255, 255)) effect='Random', rgb_color=(42, 255, 255))
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -15,6 +15,7 @@ from homeassistant.helpers.intent import IntentHandleError
from tests.common import ( from tests.common import (
async_mock_service, mock_service, get_test_home_assistant, mock_storage) async_mock_service, mock_service, get_test_home_assistant, mock_storage)
from tests.components.light import common
class TestLight(unittest.TestCase): class TestLight(unittest.TestCase):
@ -54,7 +55,7 @@ class TestLight(unittest.TestCase):
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
light.turn_on( common.turn_on(
self.hass, self.hass,
entity_id='entity_id_val', entity_id='entity_id_val',
transition='transition_val', transition='transition_val',
@ -88,7 +89,7 @@ class TestLight(unittest.TestCase):
turn_off_calls = mock_service( turn_off_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_OFF) self.hass, light.DOMAIN, SERVICE_TURN_OFF)
light.turn_off( common.turn_off(
self.hass, entity_id='entity_id_val', transition='transition_val') self.hass, entity_id='entity_id_val', transition='transition_val')
self.hass.block_till_done() self.hass.block_till_done()
@ -105,7 +106,7 @@ class TestLight(unittest.TestCase):
toggle_calls = mock_service( toggle_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TOGGLE) self.hass, light.DOMAIN, SERVICE_TOGGLE)
light.toggle( common.toggle(
self.hass, entity_id='entity_id_val', transition='transition_val') self.hass, entity_id='entity_id_val', transition='transition_val')
self.hass.block_till_done() self.hass.block_till_done()
@ -135,8 +136,8 @@ class TestLight(unittest.TestCase):
self.assertFalse(light.is_on(self.hass, dev3.entity_id)) self.assertFalse(light.is_on(self.hass, dev3.entity_id))
# Test basic turn_on, turn_off, toggle services # Test basic turn_on, turn_off, toggle services
light.turn_off(self.hass, entity_id=dev1.entity_id) common.turn_off(self.hass, entity_id=dev1.entity_id)
light.turn_on(self.hass, entity_id=dev2.entity_id) common.turn_on(self.hass, entity_id=dev2.entity_id)
self.hass.block_till_done() self.hass.block_till_done()
@ -144,7 +145,7 @@ class TestLight(unittest.TestCase):
self.assertTrue(light.is_on(self.hass, dev2.entity_id)) self.assertTrue(light.is_on(self.hass, dev2.entity_id))
# turn on all lights # turn on all lights
light.turn_on(self.hass) common.turn_on(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
@ -153,7 +154,7 @@ class TestLight(unittest.TestCase):
self.assertTrue(light.is_on(self.hass, dev3.entity_id)) self.assertTrue(light.is_on(self.hass, dev3.entity_id))
# turn off all lights # turn off all lights
light.turn_off(self.hass) common.turn_off(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
@ -162,7 +163,7 @@ class TestLight(unittest.TestCase):
self.assertFalse(light.is_on(self.hass, dev3.entity_id)) self.assertFalse(light.is_on(self.hass, dev3.entity_id))
# toggle all lights # toggle all lights
light.toggle(self.hass) common.toggle(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
@ -171,7 +172,7 @@ class TestLight(unittest.TestCase):
self.assertTrue(light.is_on(self.hass, dev3.entity_id)) self.assertTrue(light.is_on(self.hass, dev3.entity_id))
# toggle all lights # toggle all lights
light.toggle(self.hass) common.toggle(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
@ -180,12 +181,12 @@ class TestLight(unittest.TestCase):
self.assertFalse(light.is_on(self.hass, dev3.entity_id)) self.assertFalse(light.is_on(self.hass, dev3.entity_id))
# Ensure all attributes process correctly # Ensure all attributes process correctly
light.turn_on(self.hass, dev1.entity_id, common.turn_on(self.hass, dev1.entity_id,
transition=10, brightness=20, color_name='blue') transition=10, brightness=20, color_name='blue')
light.turn_on( common.turn_on(
self.hass, dev2.entity_id, rgb_color=(255, 255, 255), self.hass, dev2.entity_id, rgb_color=(255, 255, 255),
white_value=255) white_value=255)
light.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6)) common.turn_on(self.hass, dev3.entity_id, xy_color=(.4, .6))
self.hass.block_till_done() self.hass.block_till_done()
@ -211,9 +212,9 @@ class TestLight(unittest.TestCase):
prof_name, prof_h, prof_s, prof_bri = 'relax', 35.932, 69.412, 144 prof_name, prof_h, prof_s, prof_bri = 'relax', 35.932, 69.412, 144
# Test light profiles # Test light profiles
light.turn_on(self.hass, dev1.entity_id, profile=prof_name) common.turn_on(self.hass, dev1.entity_id, profile=prof_name)
# Specify a profile and a brightness attribute to overwrite it # Specify a profile and a brightness attribute to overwrite it
light.turn_on( common.turn_on(
self.hass, dev2.entity_id, self.hass, dev2.entity_id,
profile=prof_name, brightness=100) profile=prof_name, brightness=100)
@ -232,10 +233,10 @@ class TestLight(unittest.TestCase):
}, data) }, data)
# Test bad data # Test bad data
light.turn_on(self.hass) common.turn_on(self.hass)
light.turn_on(self.hass, dev1.entity_id, profile="nonexisting") common.turn_on(self.hass, dev1.entity_id, profile="nonexisting")
light.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5]) common.turn_on(self.hass, dev2.entity_id, xy_color=["bla-di-bla", 5])
light.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2]) common.turn_on(self.hass, dev3.entity_id, rgb_color=[255, None, 2])
self.hass.block_till_done() self.hass.block_till_done()
@ -249,13 +250,13 @@ class TestLight(unittest.TestCase):
self.assertEqual({}, data) self.assertEqual({}, data)
# faulty attributes will not trigger a service call # faulty attributes will not trigger a service call
light.turn_on( common.turn_on(
self.hass, dev1.entity_id, self.hass, dev1.entity_id,
profile=prof_name, brightness='bright') profile=prof_name, brightness='bright')
light.turn_on( common.turn_on(
self.hass, dev1.entity_id, self.hass, dev1.entity_id,
rgb_color='yellowish') rgb_color='yellowish')
light.turn_on( common.turn_on(
self.hass, dev2.entity_id, self.hass, dev2.entity_id,
white_value='high') white_value='high')
@ -299,7 +300,7 @@ class TestLight(unittest.TestCase):
dev1, _, _ = platform.DEVICES dev1, _, _ = platform.DEVICES
light.turn_on(self.hass, dev1.entity_id, profile='test') common.turn_on(self.hass, dev1.entity_id, profile='test')
self.hass.block_till_done() self.hass.block_till_done()
@ -340,7 +341,7 @@ class TestLight(unittest.TestCase):
)) ))
dev, _, _ = platform.DEVICES dev, _, _ = platform.DEVICES
light.turn_on(self.hass, dev.entity_id) common.turn_on(self.hass, dev.entity_id)
self.hass.block_till_done() self.hass.block_till_done()
_, data = dev.last_call('turn_on') _, data = dev.last_call('turn_on')
self.assertEqual({ self.assertEqual({
@ -380,7 +381,7 @@ class TestLight(unittest.TestCase):
dev = next(filter(lambda x: x.entity_id == 'light.ceiling_2', dev = next(filter(lambda x: x.entity_id == 'light.ceiling_2',
platform.DEVICES)) platform.DEVICES))
light.turn_on(self.hass, dev.entity_id) common.turn_on(self.hass, dev.entity_id)
self.hass.block_till_done() self.hass.block_till_done()
_, data = dev.last_call('turn_on') _, data = dev.last_call('turn_on')
self.assertEqual({ self.assertEqual({

View file

@ -5,9 +5,11 @@ from unittest import mock
from homeassistant import setup from homeassistant import setup
from homeassistant.components import litejet from homeassistant.components import litejet
from tests.common import get_test_home_assistant
import homeassistant.components.light as light import homeassistant.components.light as light
from tests.common import get_test_home_assistant
from tests.components.light import common
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ENTITY_LIGHT = 'light.mock_load_1' ENTITY_LIGHT = 'light.mock_load_1'
@ -78,7 +80,7 @@ class TestLiteJetLight(unittest.TestCase):
assert not light.is_on(self.hass, ENTITY_LIGHT) assert not light.is_on(self.hass, ENTITY_LIGHT)
light.turn_on(self.hass, ENTITY_LIGHT, brightness=102) common.turn_on(self.hass, ENTITY_LIGHT, brightness=102)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_lj.activate_load_at.assert_called_with( self.mock_lj.activate_load_at.assert_called_with(
ENTITY_LIGHT_NUMBER, 39, 0) ENTITY_LIGHT_NUMBER, 39, 0)
@ -90,11 +92,11 @@ class TestLiteJetLight(unittest.TestCase):
assert not light.is_on(self.hass, ENTITY_LIGHT) assert not light.is_on(self.hass, ENTITY_LIGHT)
light.turn_on(self.hass, ENTITY_LIGHT) common.turn_on(self.hass, ENTITY_LIGHT)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER) self.mock_lj.activate_load.assert_called_with(ENTITY_LIGHT_NUMBER)
light.turn_off(self.hass, ENTITY_LIGHT) common.turn_off(self.hass, ENTITY_LIGHT)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER) self.mock_lj.deactivate_load.assert_called_with(ENTITY_LIGHT_NUMBER)

View file

@ -148,9 +148,11 @@ from homeassistant.const import (
import homeassistant.components.light as light import homeassistant.components.light as light
from homeassistant.components.mqtt.discovery import async_start from homeassistant.components.mqtt.discovery import async_start
import homeassistant.core as ha import homeassistant.core as ha
from tests.common import ( from tests.common import (
assert_setup_component, get_test_home_assistant, mock_mqtt_component, assert_setup_component, get_test_home_assistant, mock_mqtt_component,
async_fire_mqtt_message, fire_mqtt_message, mock_coro) async_fire_mqtt_message, fire_mqtt_message, mock_coro)
from tests.components.light import common
class TestLightMQTT(unittest.TestCase): class TestLightMQTT(unittest.TestCase):
@ -506,7 +508,7 @@ class TestLightMQTT(unittest.TestCase):
self.assertEqual(50, state.attributes.get('white_value')) self.assertEqual(50, state.attributes.get('white_value'))
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
light.turn_on(self.hass, 'light.test') common.turn_on(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -515,7 +517,7 @@ class TestLightMQTT(unittest.TestCase):
state = self.hass.states.get('light.test') state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
light.turn_off(self.hass, 'light.test') common.turn_off(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -525,9 +527,9 @@ class TestLightMQTT(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
self.mock_publish.reset_mock() self.mock_publish.reset_mock()
light.turn_on(self.hass, 'light.test', common.turn_on(self.hass, 'light.test',
brightness=50, xy_color=[0.123, 0.123]) brightness=50, xy_color=[0.123, 0.123])
light.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0], common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0],
white_value=80) white_value=80)
self.hass.block_till_done() self.hass.block_till_done()
@ -566,7 +568,7 @@ class TestLightMQTT(unittest.TestCase):
state = self.hass.states.get('light.test') state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
light.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 64]) common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 64])
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_has_calls([ self.mock_publish.async_publish.assert_has_calls([
@ -714,7 +716,7 @@ class TestLightMQTT(unittest.TestCase):
state = self.hass.states.get('light.test') state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
light.turn_on(self.hass, 'light.test', brightness=50) common.turn_on(self.hass, 'light.test', brightness=50)
self.hass.block_till_done() self.hass.block_till_done()
# Should get the following MQTT messages. # Should get the following MQTT messages.
@ -726,7 +728,7 @@ class TestLightMQTT(unittest.TestCase):
], any_order=True) ], any_order=True)
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
light.turn_off(self.hass, 'light.test') common.turn_off(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -747,7 +749,7 @@ class TestLightMQTT(unittest.TestCase):
state = self.hass.states.get('light.test') state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
light.turn_on(self.hass, 'light.test', brightness=50) common.turn_on(self.hass, 'light.test', brightness=50)
self.hass.block_till_done() self.hass.block_till_done()
# Should get the following MQTT messages. # Should get the following MQTT messages.
@ -759,7 +761,7 @@ class TestLightMQTT(unittest.TestCase):
], any_order=True) ], any_order=True)
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
light.turn_off(self.hass, 'light.test') common.turn_off(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -783,7 +785,7 @@ class TestLightMQTT(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
# Turn on w/ no brightness - should set to max # Turn on w/ no brightness - should set to max
light.turn_on(self.hass, 'light.test') common.turn_on(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
# Should get the following MQTT messages. # Should get the following MQTT messages.
@ -792,7 +794,7 @@ class TestLightMQTT(unittest.TestCase):
'test_light/bright', 255, 0, False) 'test_light/bright', 255, 0, False)
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
light.turn_off(self.hass, 'light.test') common.turn_off(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -800,19 +802,19 @@ class TestLightMQTT(unittest.TestCase):
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
# Turn on w/ brightness # Turn on w/ brightness
light.turn_on(self.hass, 'light.test', brightness=50) common.turn_on(self.hass, 'light.test', brightness=50)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
'test_light/bright', 50, 0, False) 'test_light/bright', 50, 0, False)
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
light.turn_off(self.hass, 'light.test') common.turn_off(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
# Turn on w/ just a color to insure brightness gets # Turn on w/ just a color to insure brightness gets
# added and sent. # added and sent.
light.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0]) common.turn_on(self.hass, 'light.test', rgb_color=[255, 128, 0])
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_has_calls([ self.mock_publish.async_publish.assert_has_calls([

View file

@ -98,9 +98,11 @@ from homeassistant.const import (
ATTR_SUPPORTED_FEATURES) ATTR_SUPPORTED_FEATURES)
import homeassistant.components.light as light import homeassistant.components.light as light
import homeassistant.core as ha import homeassistant.core as ha
from tests.common import ( from tests.common import (
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_mqtt_component, fire_mqtt_message,
assert_setup_component, mock_coro) assert_setup_component, mock_coro)
from tests.components.light import common
class TestLightMQTTJSON(unittest.TestCase): class TestLightMQTTJSON(unittest.TestCase):
@ -315,7 +317,7 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(191, state.attributes.get(ATTR_SUPPORTED_FEATURES)) self.assertEqual(191, state.attributes.get(ATTR_SUPPORTED_FEATURES))
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
light.turn_on(self.hass, 'light.test') common.turn_on(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -324,7 +326,7 @@ class TestLightMQTTJSON(unittest.TestCase):
state = self.hass.states.get('light.test') state = self.hass.states.get('light.test')
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
light.turn_off(self.hass, 'light.test') common.turn_off(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -333,7 +335,7 @@ class TestLightMQTTJSON(unittest.TestCase):
state = self.hass.states.get('light.test') state = self.hass.states.get('light.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
light.turn_on(self.hass, 'light.test', common.turn_on(self.hass, 'light.test',
brightness=50, color_temp=155, effect='colorloop', brightness=50, color_temp=155, effect='colorloop',
white_value=170) white_value=170)
self.hass.block_till_done() self.hass.block_till_done()
@ -361,7 +363,7 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(170, state.attributes['white_value']) self.assertEqual(170, state.attributes['white_value'])
# Test a color command # Test a color command
light.turn_on(self.hass, 'light.test', common.turn_on(self.hass, 'light.test',
brightness=50, hs_color=(125, 100)) brightness=50, hs_color=(125, 100))
self.hass.block_till_done() self.hass.block_till_done()
@ -398,7 +400,7 @@ class TestLightMQTTJSON(unittest.TestCase):
} }
}) })
light.turn_on(self.hass, 'light.test', hs_color=(180.0, 50.0)) common.turn_on(self.hass, 'light.test', hs_color=(180.0, 50.0))
self.hass.block_till_done() self.hass.block_till_done()
message_json = json.loads( message_json = json.loads(
@ -427,7 +429,7 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
self.assertEqual(40, state.attributes.get(ATTR_SUPPORTED_FEATURES)) self.assertEqual(40, state.attributes.get(ATTR_SUPPORTED_FEATURES))
light.turn_on(self.hass, 'light.test', flash="short") common.turn_on(self.hass, 'light.test', flash="short")
self.hass.block_till_done() self.hass.block_till_done()
self.assertEqual('test_light_rgb/set', self.assertEqual('test_light_rgb/set',
@ -443,7 +445,7 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual("ON", message_json["state"]) self.assertEqual("ON", message_json["state"])
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
light.turn_on(self.hass, 'light.test', flash="long") common.turn_on(self.hass, 'light.test', flash="long")
self.hass.block_till_done() self.hass.block_till_done()
self.assertEqual('test_light_rgb/set', self.assertEqual('test_light_rgb/set',
@ -474,7 +476,7 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
self.assertEqual(40, state.attributes.get(ATTR_SUPPORTED_FEATURES)) self.assertEqual(40, state.attributes.get(ATTR_SUPPORTED_FEATURES))
light.turn_on(self.hass, 'light.test', transition=10) common.turn_on(self.hass, 'light.test', transition=10)
self.hass.block_till_done() self.hass.block_till_done()
self.assertEqual('test_light_rgb/set', self.assertEqual('test_light_rgb/set',
@ -490,7 +492,7 @@ class TestLightMQTTJSON(unittest.TestCase):
self.assertEqual("ON", message_json["state"]) self.assertEqual("ON", message_json["state"])
# Transition back off # Transition back off
light.turn_off(self.hass, 'light.test', transition=10) common.turn_off(self.hass, 'light.test', transition=10)
self.hass.block_till_done() self.hass.block_till_done()
self.assertEqual('test_light_rgb/set', self.assertEqual('test_light_rgb/set',

View file

@ -34,9 +34,11 @@ from homeassistant.const import (
STATE_ON, STATE_OFF, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE) STATE_ON, STATE_OFF, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE)
import homeassistant.components.light as light import homeassistant.components.light as light
import homeassistant.core as ha import homeassistant.core as ha
from tests.common import ( from tests.common import (
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_mqtt_component, fire_mqtt_message,
assert_setup_component, mock_coro) assert_setup_component, mock_coro)
from tests.components.light import common
class TestLightMQTTTemplate(unittest.TestCase): class TestLightMQTTTemplate(unittest.TestCase):
@ -244,7 +246,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
# turn on the light # turn on the light
light.turn_on(self.hass, 'light.test') common.turn_on(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -254,7 +256,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
# turn the light off # turn the light off
light.turn_off(self.hass, 'light.test') common.turn_off(self.hass, 'light.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -264,7 +266,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
# turn on the light with brightness, color # turn on the light with brightness, color
light.turn_on(self.hass, 'light.test', brightness=50, common.turn_on(self.hass, 'light.test', brightness=50,
rgb_color=[75, 75, 75]) rgb_color=[75, 75, 75])
self.hass.block_till_done() self.hass.block_till_done()
@ -273,7 +275,8 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
# turn on the light with color temp and white val # turn on the light with color temp and white val
light.turn_on(self.hass, 'light.test', color_temp=200, white_value=139) common.turn_on(self.hass, 'light.test',
color_temp=200, white_value=139)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -305,7 +308,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
# short flash # short flash
light.turn_on(self.hass, 'light.test', flash='short') common.turn_on(self.hass, 'light.test', flash='short')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -313,7 +316,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
# long flash # long flash
light.turn_on(self.hass, 'light.test', flash='long') common.turn_on(self.hass, 'light.test', flash='long')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -336,7 +339,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
# transition on # transition on
light.turn_on(self.hass, 'light.test', transition=10) common.turn_on(self.hass, 'light.test', transition=10)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -344,7 +347,7 @@ class TestLightMQTTTemplate(unittest.TestCase):
self.mock_publish.async_publish.reset_mock() self.mock_publish.async_publish.reset_mock()
# transition off # transition off
light.turn_off(self.hass, 'light.test', transition=4) common.turn_off(self.hass, 'light.test', transition=4)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(

View file

@ -3,12 +3,13 @@ import logging
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant import setup from homeassistant import setup
import homeassistant.components as core
from homeassistant.components.light import ATTR_BRIGHTNESS from homeassistant.components.light import ATTR_BRIGHTNESS
from homeassistant.const import STATE_ON, STATE_OFF from homeassistant.const import STATE_ON, STATE_OFF
from tests.common import ( from tests.common import (
get_test_home_assistant, assert_setup_component) get_test_home_assistant, assert_setup_component)
from tests.components.light import common
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -378,7 +379,7 @@ class TestTemplateLight:
state = self.hass.states.get('light.test_template_light') state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF assert state.state == STATE_OFF
core.light.turn_on(self.hass, 'light.test_template_light') common.turn_on(self.hass, 'light.test_template_light')
self.hass.block_till_done() self.hass.block_till_done()
assert len(self.calls) == 1 assert len(self.calls) == 1
@ -418,7 +419,7 @@ class TestTemplateLight:
state = self.hass.states.get('light.test_template_light') state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF assert state.state == STATE_OFF
core.light.turn_on(self.hass, 'light.test_template_light') common.turn_on(self.hass, 'light.test_template_light')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('light.test_template_light') state = self.hass.states.get('light.test_template_light')
@ -461,7 +462,7 @@ class TestTemplateLight:
state = self.hass.states.get('light.test_template_light') state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_ON assert state.state == STATE_ON
core.light.turn_off(self.hass, 'light.test_template_light') common.turn_off(self.hass, 'light.test_template_light')
self.hass.block_till_done() self.hass.block_till_done()
assert len(self.calls) == 1 assert len(self.calls) == 1
@ -498,7 +499,7 @@ class TestTemplateLight:
state = self.hass.states.get('light.test_template_light') state = self.hass.states.get('light.test_template_light')
assert state.state == STATE_OFF assert state.state == STATE_OFF
core.light.turn_off(self.hass, 'light.test_template_light') common.turn_off(self.hass, 'light.test_template_light')
self.hass.block_till_done() self.hass.block_till_done()
assert len(self.calls) == 1 assert len(self.calls) == 1
@ -538,7 +539,7 @@ class TestTemplateLight:
state = self.hass.states.get('light.test_template_light') state = self.hass.states.get('light.test_template_light')
assert state.attributes.get('brightness') is None assert state.attributes.get('brightness') is None
core.light.turn_on( common.turn_on(
self.hass, 'light.test_template_light', **{ATTR_BRIGHTNESS: 124}) self.hass, 'light.test_template_light', **{ATTR_BRIGHTNESS: 124})
self.hass.block_till_done() self.hass.block_till_done()
assert len(self.calls) == 1 assert len(self.calls) == 1

View file

@ -8,6 +8,7 @@ from homeassistant.components import light, scene
from homeassistant.util import yaml from homeassistant.util import yaml
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant
from tests.components.light import common as common_light
from tests.components.scene import common from tests.components.scene import common
@ -26,7 +27,7 @@ class TestScene(unittest.TestCase):
self.light_1, self.light_2 = test_light.DEVICES[0:2] self.light_1, self.light_2 = test_light.DEVICES[0:2]
light.turn_off( common_light.turn_off(
self.hass, [self.light_1.entity_id, self.light_2.entity_id]) self.hass, [self.light_1.entity_id, self.light_2.entity_id])
self.hass.block_till_done() self.hass.block_till_done()

View file

@ -0,0 +1,39 @@
"""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.switch import DOMAIN
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_on(hass, entity_id=None):
"""Turn all or specified switch on."""
hass.add_job(async_turn_on, hass, entity_id)
@callback
@bind_hass
def async_turn_on(hass, entity_id=None):
"""Turn all or specified switch on."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
@bind_hass
def turn_off(hass, entity_id=None):
"""Turn all or specified switch off."""
hass.add_job(async_turn_off, hass, entity_id)
@callback
@bind_hass
def async_turn_off(hass, entity_id=None):
"""Turn all or specified switch off."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.async_add_job(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data))

View file

@ -10,6 +10,7 @@ import homeassistant.components.switch as switch
import homeassistant.components.switch.command_line as command_line import homeassistant.components.switch.command_line as command_line
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant
from tests.components.switch import common
# pylint: disable=invalid-name # pylint: disable=invalid-name
@ -44,13 +45,13 @@ class TestCommandSwitch(unittest.TestCase):
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.test') common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test') common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
@ -78,13 +79,13 @@ class TestCommandSwitch(unittest.TestCase):
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.test') common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test') common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
@ -114,13 +115,13 @@ class TestCommandSwitch(unittest.TestCase):
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.test') common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test') common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
@ -147,13 +148,13 @@ class TestCommandSwitch(unittest.TestCase):
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.test') common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test') common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')

View file

@ -11,6 +11,8 @@ import homeassistant.util.dt as dt_util
from tests.common import ( from tests.common import (
assert_setup_component, get_test_home_assistant, fire_time_changed, assert_setup_component, get_test_home_assistant, fire_time_changed,
mock_service) mock_service)
from tests.components.light import common as common_light
from tests.components.switch import common
class TestSwitchFlux(unittest.TestCase): class TestSwitchFlux(unittest.TestCase):
@ -147,7 +149,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -193,7 +195,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -240,7 +242,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -286,7 +288,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -334,7 +336,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -383,7 +385,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -434,7 +436,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -484,7 +486,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -534,7 +536,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -584,7 +586,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -633,7 +635,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -681,7 +683,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -698,9 +700,9 @@ class TestSwitchFlux(unittest.TestCase):
{light.DOMAIN: {CONF_PLATFORM: 'test'}})) {light.DOMAIN: {CONF_PLATFORM: 'test'}}))
dev1, dev2, dev3 = platform.DEVICES dev1, dev2, dev3 = platform.DEVICES
light.turn_on(self.hass, entity_id=dev2.entity_id) common_light.turn_on(self.hass, entity_id=dev2.entity_id)
self.hass.block_till_done() self.hass.block_till_done()
light.turn_on(self.hass, entity_id=dev3.entity_id) common_light.turn_on(self.hass, entity_id=dev3.entity_id)
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get(dev1.entity_id) state = self.hass.states.get(dev1.entity_id)
@ -743,7 +745,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -794,7 +796,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()
@ -838,7 +840,7 @@ class TestSwitchFlux(unittest.TestCase):
}) })
turn_on_calls = mock_service( turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON) self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux') common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done() self.hass.block_till_done()
fire_time_changed(self.hass, test_time) fire_time_changed(self.hass, test_time)
self.hass.block_till_done() self.hass.block_till_done()

View file

@ -8,6 +8,7 @@ from homeassistant.components import switch
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant
from tests.components.switch import common
class TestSwitch(unittest.TestCase): class TestSwitch(unittest.TestCase):
@ -41,8 +42,8 @@ class TestSwitch(unittest.TestCase):
self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))
switch.turn_off(self.hass, self.switch_1.entity_id) common.turn_off(self.hass, self.switch_1.entity_id)
switch.turn_on(self.hass, self.switch_2.entity_id) common.turn_on(self.hass, self.switch_2.entity_id)
self.hass.block_till_done() self.hass.block_till_done()
@ -51,7 +52,7 @@ class TestSwitch(unittest.TestCase):
self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id)) self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))
# Turn all off # Turn all off
switch.turn_off(self.hass) common.turn_off(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
@ -64,7 +65,7 @@ class TestSwitch(unittest.TestCase):
self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id)) self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))
# Turn all on # Turn all on
switch.turn_on(self.hass) common.turn_on(self.hass)
self.hass.block_till_done() self.hass.block_till_done()

View file

@ -5,9 +5,11 @@ from unittest import mock
from homeassistant import setup from homeassistant import setup
from homeassistant.components import litejet from homeassistant.components import litejet
from tests.common import get_test_home_assistant
import homeassistant.components.switch as switch import homeassistant.components.switch as switch
from tests.common import get_test_home_assistant
from tests.components.switch import common
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ENTITY_SWITCH = 'switch.mock_switch_1' ENTITY_SWITCH = 'switch.mock_switch_1'
@ -88,11 +90,11 @@ class TestLiteJetSwitch(unittest.TestCase):
assert not switch.is_on(self.hass, ENTITY_SWITCH) assert not switch.is_on(self.hass, ENTITY_SWITCH)
switch.turn_on(self.hass, ENTITY_SWITCH) common.turn_on(self.hass, ENTITY_SWITCH)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER) self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
switch.turn_off(self.hass, ENTITY_SWITCH) common.turn_off(self.hass, ENTITY_SWITCH)
self.hass.block_till_done() self.hass.block_till_done()
self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER) self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)

View file

@ -8,9 +8,11 @@ from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE,\
import homeassistant.core as ha import homeassistant.core as ha
import homeassistant.components.switch as switch import homeassistant.components.switch as switch
from homeassistant.components.mqtt.discovery import async_start from homeassistant.components.mqtt.discovery import async_start
from tests.common import ( from tests.common import (
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_coro, mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_coro,
async_mock_mqtt_component, async_fire_mqtt_message) async_mock_mqtt_component, async_fire_mqtt_message)
from tests.components.switch import common
class TestSwitchMQTT(unittest.TestCase): class TestSwitchMQTT(unittest.TestCase):
@ -75,7 +77,7 @@ class TestSwitchMQTT(unittest.TestCase):
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE)) self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
switch.turn_on(self.hass, 'switch.test') common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(
@ -84,7 +86,7 @@ class TestSwitchMQTT(unittest.TestCase):
state = self.hass.states.get('switch.test') state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test') common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done() self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with( self.mock_publish.async_publish.assert_called_once_with(

View file

@ -1,11 +1,11 @@
"""The tests for the Template switch platform.""" """The tests for the Template switch platform."""
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant import setup from homeassistant import setup
import homeassistant.components as core
from homeassistant.const import STATE_ON, STATE_OFF from homeassistant.const import STATE_ON, STATE_OFF
from tests.common import ( from tests.common import (
get_test_home_assistant, assert_setup_component) get_test_home_assistant, assert_setup_component)
from tests.components.switch import common
class TestTemplateSwitch: class TestTemplateSwitch:
@ -406,7 +406,7 @@ class TestTemplateSwitch:
state = self.hass.states.get('switch.test_template_switch') state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_OFF assert state.state == STATE_OFF
core.switch.turn_on(self.hass, 'switch.test_template_switch') common.turn_on(self.hass, 'switch.test_template_switch')
self.hass.block_till_done() self.hass.block_till_done()
assert len(self.calls) == 1 assert len(self.calls) == 1
@ -442,7 +442,7 @@ class TestTemplateSwitch:
state = self.hass.states.get('switch.test_template_switch') state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_ON assert state.state == STATE_ON
core.switch.turn_off(self.hass, 'switch.test_template_switch') common.turn_off(self.hass, 'switch.test_template_switch')
self.hass.block_till_done() self.hass.block_till_done()
assert len(self.calls) == 1 assert len(self.calls) == 1

View file

@ -7,6 +7,7 @@ from homeassistant.const import STATE_ON, STATE_OFF
import homeassistant.components.switch as switch import homeassistant.components.switch as switch
from tests.common import get_test_home_assistant, mock_service from tests.common import get_test_home_assistant, mock_service
from tests.components.switch import common
TEST_STATE = None TEST_STATE = None
@ -59,13 +60,13 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = True TEST_STATE = True
switch.turn_on(self.hass, 'switch.wake_on_lan') common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan') state = self.hass.states.get('switch.wake_on_lan')
self.assertEqual(STATE_ON, state.state) self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.wake_on_lan') common.turn_off(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan') state = self.hass.states.get('switch.wake_on_lan')
@ -91,7 +92,7 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = True TEST_STATE = True
switch.turn_on(self.hass, 'switch.wake_on_lan') common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan') state = self.hass.states.get('switch.wake_on_lan')
@ -123,7 +124,7 @@ class TestWOLSwitch(unittest.TestCase):
state = self.hass.states.get('switch.wake_on_lan') state = self.hass.states.get('switch.wake_on_lan')
self.assertEqual(STATE_OFF, state.state) self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.wake_on_lan') common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done() self.hass.block_till_done()
@patch('wakeonlan.send_magic_packet', new=send_magic_packet) @patch('wakeonlan.send_magic_packet', new=send_magic_packet)
@ -149,7 +150,7 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = True TEST_STATE = True
switch.turn_on(self.hass, 'switch.wake_on_lan') common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan') state = self.hass.states.get('switch.wake_on_lan')
@ -158,7 +159,7 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = False TEST_STATE = False
switch.turn_off(self.hass, 'switch.wake_on_lan') common.turn_off(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan') state = self.hass.states.get('switch.wake_on_lan')
@ -185,7 +186,7 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = True TEST_STATE = True
switch.turn_on(self.hass, 'switch.wake_on_lan') common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done() self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan') state = self.hass.states.get('switch.wake_on_lan')

View file

@ -12,6 +12,7 @@ from homeassistant.components import (
from homeassistant.util import dt as dt_util from homeassistant.util import dt as dt_util
from tests.common import get_test_home_assistant, fire_time_changed from tests.common import get_test_home_assistant, fire_time_changed
from tests.components.light import common as common_light
class TestDeviceSunLightTrigger(unittest.TestCase): class TestDeviceSunLightTrigger(unittest.TestCase):
@ -68,7 +69,7 @@ class TestDeviceSunLightTrigger(unittest.TestCase):
self.hass, device_sun_light_trigger.DOMAIN, { self.hass, device_sun_light_trigger.DOMAIN, {
device_sun_light_trigger.DOMAIN: {}})) device_sun_light_trigger.DOMAIN: {}}))
light.turn_off(self.hass) common_light.turn_off(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
@ -81,7 +82,7 @@ class TestDeviceSunLightTrigger(unittest.TestCase):
def test_lights_turn_off_when_everyone_leaves(self): def test_lights_turn_off_when_everyone_leaves(self):
"""Test lights turn off when everyone leaves the house.""" """Test lights turn off when everyone leaves the house."""
light.turn_on(self.hass) common_light.turn_on(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
@ -100,7 +101,7 @@ class TestDeviceSunLightTrigger(unittest.TestCase):
"""Test lights turn on when coming home after sun set.""" """Test lights turn on when coming home after sun set."""
test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC) test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
with patch('homeassistant.util.dt.utcnow', return_value=test_time): with patch('homeassistant.util.dt.utcnow', return_value=test_time):
light.turn_off(self.hass) common_light.turn_off(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
self.assertTrue(setup_component( self.assertTrue(setup_component(

View file

@ -3,9 +3,13 @@
import unittest import unittest
from unittest.mock import patch from unittest.mock import patch
from homeassistant.core import Context, callback
from homeassistant.setup import setup_component
from homeassistant.components import script from homeassistant.components import script
from homeassistant.components.script import DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_RELOAD, SERVICE_TOGGLE, SERVICE_TURN_OFF)
from homeassistant.core import Context, callback, split_entity_id
from homeassistant.loader import bind_hass
from homeassistant.setup import setup_component
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant
@ -13,6 +17,44 @@ from tests.common import get_test_home_assistant
ENTITY_ID = 'script.test' ENTITY_ID = 'script.test'
@bind_hass
def turn_on(hass, entity_id, variables=None, context=None):
"""Turn script on.
This is a legacy helper method. Do not use it for new tests.
"""
_, object_id = split_entity_id(entity_id)
hass.services.call(DOMAIN, object_id, variables, context=context)
@bind_hass
def turn_off(hass, entity_id):
"""Turn script on.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
@bind_hass
def toggle(hass, entity_id):
"""Toggle the script.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
@bind_hass
def reload(hass):
"""Reload script component.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_RELOAD)
class TestScriptComponent(unittest.TestCase): class TestScriptComponent(unittest.TestCase):
"""Test the Script component.""" """Test the Script component."""
@ -76,17 +118,17 @@ class TestScriptComponent(unittest.TestCase):
} }
}) })
script.turn_on(self.hass, ENTITY_ID) turn_on(self.hass, ENTITY_ID)
self.hass.block_till_done() self.hass.block_till_done()
self.assertTrue(script.is_on(self.hass, ENTITY_ID)) self.assertTrue(script.is_on(self.hass, ENTITY_ID))
self.assertEqual(0, len(events)) self.assertEqual(0, len(events))
# Calling turn_on a second time should not advance the script # Calling turn_on a second time should not advance the script
script.turn_on(self.hass, ENTITY_ID) turn_on(self.hass, ENTITY_ID)
self.hass.block_till_done() self.hass.block_till_done()
self.assertEqual(0, len(events)) self.assertEqual(0, len(events))
script.turn_off(self.hass, ENTITY_ID) turn_off(self.hass, ENTITY_ID)
self.hass.block_till_done() self.hass.block_till_done()
self.assertFalse(script.is_on(self.hass, ENTITY_ID)) self.assertFalse(script.is_on(self.hass, ENTITY_ID))
self.assertEqual(0, len(events)) self.assertEqual(0, len(events))
@ -121,12 +163,12 @@ class TestScriptComponent(unittest.TestCase):
} }
}) })
script.toggle(self.hass, ENTITY_ID) toggle(self.hass, ENTITY_ID)
self.hass.block_till_done() self.hass.block_till_done()
self.assertTrue(script.is_on(self.hass, ENTITY_ID)) self.assertTrue(script.is_on(self.hass, ENTITY_ID))
self.assertEqual(0, len(events)) self.assertEqual(0, len(events))
script.toggle(self.hass, ENTITY_ID) toggle(self.hass, ENTITY_ID)
self.hass.block_till_done() self.hass.block_till_done()
self.assertFalse(script.is_on(self.hass, ENTITY_ID)) self.assertFalse(script.is_on(self.hass, ENTITY_ID))
self.assertEqual(0, len(events)) self.assertEqual(0, len(events))
@ -156,7 +198,7 @@ class TestScriptComponent(unittest.TestCase):
}, },
}) })
script.turn_on(self.hass, ENTITY_ID, { turn_on(self.hass, ENTITY_ID, {
'greeting': 'world' 'greeting': 'world'
}, context=context) }, context=context)
@ -204,7 +246,7 @@ class TestScriptComponent(unittest.TestCase):
}}}): }}}):
with patch('homeassistant.config.find_config_file', with patch('homeassistant.config.find_config_file',
return_value=''): return_value=''):
script.reload(self.hass) reload(self.hass)
self.hass.block_till_done() self.hass.block_till_done()
assert self.hass.states.get(ENTITY_ID) is None assert self.hass.states.get(ENTITY_ID) is None

View file

@ -79,10 +79,10 @@ def test_component_loader_non_existing(hass):
@asyncio.coroutine @asyncio.coroutine
def test_component_wrapper(hass): def test_component_wrapper(hass):
"""Test component wrapper.""" """Test component wrapper."""
calls = async_mock_service(hass, 'light', 'turn_on') calls = async_mock_service(hass, 'persistent_notification', 'create')
components = loader.Components(hass) components = loader.Components(hass)
components.light.async_turn_on('light.test') components.persistent_notification.async_create('message')
yield from hass.async_block_till_done() yield from hass.async_block_till_done()
assert len(calls) == 1 assert len(calls) == 1