Consolidate all platforms that have tests (#22109)
* Moved climate components with tests into platform dirs. * Updated tests from climate component. * Moved binary_sensor components with tests into platform dirs. * Updated tests from binary_sensor component. * Moved calendar components with tests into platform dirs. * Updated tests from calendar component. * Moved camera components with tests into platform dirs. * Updated tests from camera component. * Moved cover components with tests into platform dirs. * Updated tests from cover component. * Moved device_tracker components with tests into platform dirs. * Updated tests from device_tracker component. * Moved fan components with tests into platform dirs. * Updated tests from fan component. * Moved geo_location components with tests into platform dirs. * Updated tests from geo_location component. * Moved image_processing components with tests into platform dirs. * Updated tests from image_processing component. * Moved light components with tests into platform dirs. * Updated tests from light component. * Moved lock components with tests into platform dirs. * Moved media_player components with tests into platform dirs. * Updated tests from media_player component. * Moved scene components with tests into platform dirs. * Moved sensor components with tests into platform dirs. * Updated tests from sensor component. * Moved switch components with tests into platform dirs. * Updated tests from sensor component. * Moved vacuum components with tests into platform dirs. * Updated tests from vacuum component. * Moved weather components with tests into platform dirs. * Fixed __init__.py files * Fixes for stuff moved as part of this branch. * Fix stuff needed to merge with balloob's branch. * Formatting issues. * Missing __init__.py files. * Fix-ups * Fixup * Regenerated requirements. * Linting errors fixed. * Fixed more broken tests. * Missing init files. * Fix broken tests. * More broken tests * There seems to be a thread race condition. I suspect the logger stuff is running in another thread, which means waiting until the aio loop is done is missing the log messages. Used sleep instead because that allows the logger thread to run. I think the api_streams sensor might not be thread safe. * Disabled tests, will remove sensor in #22147 * Updated coverage and codeowners.
This commit is contained in:
1 parent
46ece3603f
commit
f195ecca4b
486 files changed
+662
-455
No files matched your search
@@ -1,200 +0,0 @@
|
||||
"""The tests for the Command line switch platform."""
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.const import STATE_ON, STATE_OFF
|
||||
import homeassistant.components.switch as switch
|
||||
import homeassistant.components.switch.command_line as command_line
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.components.switch import common
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
class TestCommandSwitch(unittest.TestCase):
|
||||
"""Test the command switch."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_state_none(self):
|
||||
"""Test with none state."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'switch_status')
|
||||
test_switch = {
|
||||
'command_on': 'echo 1 > {}'.format(path),
|
||||
'command_off': 'echo 0 > {}'.format(path),
|
||||
}
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'command_line',
|
||||
'switches': {
|
||||
'test': test_switch
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
def test_state_value(self):
|
||||
"""Test with state value."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'switch_status')
|
||||
test_switch = {
|
||||
'command_state': 'cat {}'.format(path),
|
||||
'command_on': 'echo 1 > {}'.format(path),
|
||||
'command_off': 'echo 0 > {}'.format(path),
|
||||
'value_template': '{{ value=="1" }}'
|
||||
}
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'command_line',
|
||||
'switches': {
|
||||
'test': test_switch
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
def test_state_json_value(self):
|
||||
"""Test with state JSON value."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'switch_status')
|
||||
oncmd = json.dumps({'status': 'ok'})
|
||||
offcmd = json.dumps({'status': 'nope'})
|
||||
test_switch = {
|
||||
'command_state': 'cat {}'.format(path),
|
||||
'command_on': 'echo \'{}\' > {}'.format(oncmd, path),
|
||||
'command_off': 'echo \'{}\' > {}'.format(offcmd, path),
|
||||
'value_template': '{{ value_json.status=="ok" }}'
|
||||
}
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'command_line',
|
||||
'switches': {
|
||||
'test': test_switch
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
def test_state_code(self):
|
||||
"""Test with state code."""
|
||||
with tempfile.TemporaryDirectory() as tempdirname:
|
||||
path = os.path.join(tempdirname, 'switch_status')
|
||||
test_switch = {
|
||||
'command_state': 'cat {}'.format(path),
|
||||
'command_on': 'echo 1 > {}'.format(path),
|
||||
'command_off': 'echo 0 > {}'.format(path),
|
||||
}
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'command_line',
|
||||
'switches': {
|
||||
'test': test_switch
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.test')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
def test_assumed_state_should_be_true_if_command_state_is_none(self):
|
||||
"""Test with state value."""
|
||||
# args: hass, device_name, friendly_name, command_on, command_off,
|
||||
# command_state, value_template
|
||||
init_args = [
|
||||
self.hass,
|
||||
"test_device_name",
|
||||
"Test friendly name!",
|
||||
"echo 'on command'",
|
||||
"echo 'off command'",
|
||||
None,
|
||||
None,
|
||||
]
|
||||
|
||||
no_state_device = command_line.CommandSwitch(*init_args)
|
||||
assert no_state_device.assumed_state
|
||||
|
||||
# Set state command
|
||||
init_args[-2] = 'cat {}'
|
||||
|
||||
state_device = command_line.CommandSwitch(*init_args)
|
||||
assert not state_device.assumed_state
|
||||
|
||||
def test_entity_id_set_correctly(self):
|
||||
"""Test that entity_id is set correctly from object_id."""
|
||||
init_args = [
|
||||
self.hass,
|
||||
"test_device_name",
|
||||
"Test friendly name!",
|
||||
"echo 'on command'",
|
||||
"echo 'off command'",
|
||||
False,
|
||||
None,
|
||||
]
|
||||
|
||||
test_switch = command_line.CommandSwitch(*init_args)
|
||||
assert test_switch.entity_id == 'switch.test_device_name'
|
||||
assert test_switch.name == 'Test friendly name!'
|
||||
@@ -1,847 +0,0 @@
|
||||
"""The tests for the Flux switch platform."""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.components import switch, light
|
||||
from homeassistant.const import (
|
||||
CONF_PLATFORM, STATE_ON, SERVICE_TURN_ON, SUN_EVENT_SUNRISE)
|
||||
import homeassistant.loader as loader
|
||||
import homeassistant.util.dt as dt_util
|
||||
|
||||
from tests.common import (
|
||||
assert_setup_component, get_test_home_assistant, fire_time_changed,
|
||||
mock_service)
|
||||
from tests.components.light import common as common_light
|
||||
from tests.components.switch import common
|
||||
|
||||
|
||||
class TestSwitchFlux(unittest.TestCase):
|
||||
"""Test the Flux switch platform."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_valid_config(self):
|
||||
"""Test configuration."""
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': ['light.desk', 'light.lamp'],
|
||||
}
|
||||
})
|
||||
|
||||
def test_valid_config_with_info(self):
|
||||
"""Test configuration."""
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': ['light.desk', 'light.lamp'],
|
||||
'stop_time': '22:59',
|
||||
'start_time': '7:22',
|
||||
'start_colortemp': '1000',
|
||||
'sunset_colortemp': '2000',
|
||||
'stop_colortemp': '4000'
|
||||
}
|
||||
})
|
||||
|
||||
def test_valid_config_no_name(self):
|
||||
"""Test configuration."""
|
||||
with assert_setup_component(1, 'switch'):
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'flux',
|
||||
'lights': ['light.desk', 'light.lamp']
|
||||
}
|
||||
})
|
||||
|
||||
def test_invalid_config_no_lights(self):
|
||||
"""Test configuration."""
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'flux',
|
||||
'name': 'flux'
|
||||
}
|
||||
})
|
||||
|
||||
def test_flux_when_switch_is_off(self):
|
||||
"""Test the flux switch when it is off."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=10, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=test_time):
|
||||
with patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id]
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
assert 0 == len(turn_on_calls)
|
||||
|
||||
def test_flux_before_sunrise(self):
|
||||
"""Test the flux switch before sunrise."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=2, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=test_time):
|
||||
with patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id]
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 112
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.606, 0.379]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_after_sunrise_before_sunset(self):
|
||||
"""Test the flux switch after sunrise and before sunset."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=8, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id]
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 173
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.439, 0.37]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_after_sunset_before_stop(self):
|
||||
"""Test the flux switch after sunset and before stop."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=17, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'stop_time': '22:00'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 146
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.506, 0.385]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_after_stop_before_sunrise(self):
|
||||
"""Test the flux switch after stop and before sunrise."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=23, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=test_time):
|
||||
with patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id]
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 112
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.606, 0.379]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_with_custom_start_stop_times(self):
|
||||
"""Test the flux with custom start and stop times."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=17, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'start_time': '6:00',
|
||||
'stop_time': '23:30'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 147
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.504, 0.385]
|
||||
|
||||
def test_flux_before_sunrise_stop_next_day(self):
|
||||
"""Test the flux switch before sunrise.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=2, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'stop_time': '01:00'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 112
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.606, 0.379]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_after_sunrise_before_sunset_stop_next_day(self):
|
||||
"""
|
||||
Test the flux switch after sunrise and before sunset.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=8, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'stop_time': '01:00'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 173
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.439, 0.37]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_after_sunset_before_midnight_stop_next_day(self):
|
||||
"""Test the flux switch after sunset and before stop.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=23, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.util.dt.utcnow', return_value=test_time):
|
||||
with patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'stop_time': '01:00'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 119
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.588, 0.386]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_after_sunset_after_midnight_stop_next_day(self):
|
||||
"""Test the flux switch after sunset and before stop.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=00, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'stop_time': '01:00'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 114
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.601, 0.382]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_after_stop_before_sunrise_stop_next_day(self):
|
||||
"""Test the flux switch after stop and before sunrise.
|
||||
|
||||
This test has the stop_time on the next day (after midnight).
|
||||
"""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=2, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'stop_time': '01:00'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 112
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.606, 0.379]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_with_custom_colortemps(self):
|
||||
"""Test the flux with custom start and stop colortemps."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=17, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'start_colortemp': '1000',
|
||||
'stop_colortemp': '6000',
|
||||
'stop_time': '22:00'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 159
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.469, 0.378]
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def test_flux_with_custom_brightness(self):
|
||||
"""Test the flux with custom start and stop colortemps."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=17, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'brightness': 255,
|
||||
'stop_time': '22:00'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 255
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.506, 0.385]
|
||||
|
||||
def test_flux_with_multiple_lights(self):
|
||||
"""Test the flux switch with multiple light entities."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1, dev2, dev3 = platform.DEVICES
|
||||
common_light.turn_on(self.hass, entity_id=dev2.entity_id)
|
||||
self.hass.block_till_done()
|
||||
common_light.turn_on(self.hass, entity_id=dev3.entity_id)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
state = self.hass.states.get(dev2.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
state = self.hass.states.get(dev3.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('xy_color') is None
|
||||
assert state.attributes.get('brightness') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=12, minute=0, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
print('sunrise {}'.format(sunrise_time))
|
||||
return sunrise_time
|
||||
print('sunset {}'.format(sunset_time))
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id,
|
||||
dev2.entity_id,
|
||||
dev3.entity_id]
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 163
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.46, 0.376]
|
||||
call = turn_on_calls[-2]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 163
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.46, 0.376]
|
||||
call = turn_on_calls[-3]
|
||||
assert call.data[light.ATTR_BRIGHTNESS] == 163
|
||||
assert call.data[light.ATTR_XY_COLOR] == [0.46, 0.376]
|
||||
|
||||
def test_flux_with_mired(self):
|
||||
"""Test the flux switch´s mode mired."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('color_temp') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=8, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'mode': 'mired'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
assert call.data[light.ATTR_COLOR_TEMP] == 269
|
||||
|
||||
def test_flux_with_rgb(self):
|
||||
"""Test the flux switch´s mode rgb."""
|
||||
platform = loader.get_component(self.hass, 'light.test')
|
||||
platform.init()
|
||||
assert setup_component(self.hass, light.DOMAIN,
|
||||
{light.DOMAIN: {CONF_PLATFORM: 'test'}})
|
||||
|
||||
dev1 = platform.DEVICES[0]
|
||||
|
||||
# Verify initial state of light
|
||||
state = self.hass.states.get(dev1.entity_id)
|
||||
assert STATE_ON == state.state
|
||||
assert state.attributes.get('color_temp') is None
|
||||
|
||||
test_time = dt_util.utcnow().replace(hour=8, minute=30, second=0)
|
||||
sunset_time = test_time.replace(hour=17, minute=0, second=0)
|
||||
sunrise_time = test_time.replace(hour=5, minute=0, second=0)
|
||||
|
||||
def event_date(hass, event, now=None):
|
||||
if event == SUN_EVENT_SUNRISE:
|
||||
return sunrise_time
|
||||
return sunset_time
|
||||
|
||||
with patch('homeassistant.components.switch.flux.dt_utcnow',
|
||||
return_value=test_time), \
|
||||
patch('homeassistant.helpers.sun.get_astral_event_date',
|
||||
side_effect=event_date):
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
switch.DOMAIN: {
|
||||
'platform': 'flux',
|
||||
'name': 'flux',
|
||||
'lights': [dev1.entity_id],
|
||||
'mode': 'rgb'
|
||||
}
|
||||
})
|
||||
turn_on_calls = mock_service(
|
||||
self.hass, light.DOMAIN, SERVICE_TURN_ON)
|
||||
common.turn_on(self.hass, 'switch.flux')
|
||||
self.hass.block_till_done()
|
||||
fire_time_changed(self.hass, test_time)
|
||||
self.hass.block_till_done()
|
||||
call = turn_on_calls[-1]
|
||||
rgb = (255, 198, 152)
|
||||
rounded_call = tuple(map(round, call.data[light.ATTR_RGB_COLOR]))
|
||||
assert rounded_call == rgb
|
||||
@@ -1,138 +0,0 @@
|
||||
"""The tests for the litejet component."""
|
||||
import logging
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from homeassistant import setup
|
||||
from homeassistant.components import litejet
|
||||
import homeassistant.components.switch as switch
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
from tests.components.switch import common
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ENTITY_SWITCH = 'switch.mock_switch_1'
|
||||
ENTITY_SWITCH_NUMBER = 1
|
||||
ENTITY_OTHER_SWITCH = 'switch.mock_switch_2'
|
||||
ENTITY_OTHER_SWITCH_NUMBER = 2
|
||||
|
||||
|
||||
class TestLiteJetSwitch(unittest.TestCase):
|
||||
"""Test the litejet component."""
|
||||
|
||||
@mock.patch('pylitejet.LiteJet')
|
||||
def setup_method(self, method, mock_pylitejet):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.hass.start()
|
||||
|
||||
self.switch_pressed_callbacks = {}
|
||||
self.switch_released_callbacks = {}
|
||||
|
||||
def get_switch_name(number):
|
||||
return "Mock Switch #"+str(number)
|
||||
|
||||
def on_switch_pressed(number, callback):
|
||||
self.switch_pressed_callbacks[number] = callback
|
||||
|
||||
def on_switch_released(number, callback):
|
||||
self.switch_released_callbacks[number] = callback
|
||||
|
||||
self.mock_lj = mock_pylitejet.return_value
|
||||
self.mock_lj.loads.return_value = range(0)
|
||||
self.mock_lj.button_switches.return_value = range(1, 3)
|
||||
self.mock_lj.all_switches.return_value = range(1, 6)
|
||||
self.mock_lj.scenes.return_value = range(0)
|
||||
self.mock_lj.get_switch_name.side_effect = get_switch_name
|
||||
self.mock_lj.on_switch_pressed.side_effect = on_switch_pressed
|
||||
self.mock_lj.on_switch_released.side_effect = on_switch_released
|
||||
|
||||
config = {
|
||||
'litejet': {
|
||||
'port': '/tmp/this_will_be_mocked',
|
||||
}
|
||||
}
|
||||
if method == self.__class__.test_include_switches_False:
|
||||
config['litejet']['include_switches'] = False
|
||||
elif method != self.__class__.test_include_switches_unspecified:
|
||||
config['litejet']['include_switches'] = True
|
||||
|
||||
assert setup.setup_component(self.hass, litejet.DOMAIN, config)
|
||||
self.hass.block_till_done()
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def switch(self):
|
||||
"""Return the switch state."""
|
||||
return self.hass.states.get(ENTITY_SWITCH)
|
||||
|
||||
def other_switch(self):
|
||||
"""Return the other switch state."""
|
||||
return self.hass.states.get(ENTITY_OTHER_SWITCH)
|
||||
|
||||
def test_include_switches_unspecified(self):
|
||||
"""Test that switches are ignored by default."""
|
||||
self.mock_lj.button_switches.assert_not_called()
|
||||
self.mock_lj.all_switches.assert_not_called()
|
||||
|
||||
def test_include_switches_False(self):
|
||||
"""Test that switches can be explicitly ignored."""
|
||||
self.mock_lj.button_switches.assert_not_called()
|
||||
self.mock_lj.all_switches.assert_not_called()
|
||||
|
||||
def test_on_off(self):
|
||||
"""Test turning the switch on and off."""
|
||||
assert self.switch().state == 'off'
|
||||
assert self.other_switch().state == 'off'
|
||||
|
||||
assert not switch.is_on(self.hass, ENTITY_SWITCH)
|
||||
|
||||
common.turn_on(self.hass, ENTITY_SWITCH)
|
||||
self.hass.block_till_done()
|
||||
self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
|
||||
|
||||
common.turn_off(self.hass, ENTITY_SWITCH)
|
||||
self.hass.block_till_done()
|
||||
self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
|
||||
|
||||
def test_pressed_event(self):
|
||||
"""Test handling an event from LiteJet."""
|
||||
# Switch 1
|
||||
_LOGGER.info(self.switch_pressed_callbacks[ENTITY_SWITCH_NUMBER])
|
||||
self.switch_pressed_callbacks[ENTITY_SWITCH_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert switch.is_on(self.hass, ENTITY_SWITCH)
|
||||
assert not switch.is_on(self.hass, ENTITY_OTHER_SWITCH)
|
||||
assert self.switch().state == 'on'
|
||||
assert self.other_switch().state == 'off'
|
||||
|
||||
# Switch 2
|
||||
self.switch_pressed_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert switch.is_on(self.hass, ENTITY_OTHER_SWITCH)
|
||||
assert switch.is_on(self.hass, ENTITY_SWITCH)
|
||||
assert self.other_switch().state == 'on'
|
||||
assert self.switch().state == 'on'
|
||||
|
||||
def test_released_event(self):
|
||||
"""Test handling an event from LiteJet."""
|
||||
# Initial state is on.
|
||||
self.switch_pressed_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert switch.is_on(self.hass, ENTITY_OTHER_SWITCH)
|
||||
|
||||
# Event indicates it is off now.
|
||||
|
||||
self.switch_released_callbacks[ENTITY_OTHER_SWITCH_NUMBER]()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert not switch.is_on(self.hass, ENTITY_OTHER_SWITCH)
|
||||
assert not switch.is_on(self.hass, ENTITY_SWITCH)
|
||||
assert self.other_switch().state == 'off'
|
||||
assert self.switch().state == 'off'
|
||||
@@ -1,112 +0,0 @@
|
||||
"""The tests for the mFi switch platform."""
|
||||
import unittest
|
||||
import unittest.mock as mock
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
import homeassistant.components.switch as switch
|
||||
import homeassistant.components.switch.mfi as mfi
|
||||
from tests.components.sensor import test_mfi as test_mfi_sensor
|
||||
|
||||
from tests.common import get_test_home_assistant
|
||||
|
||||
|
||||
class TestMfiSwitchSetup(test_mfi_sensor.TestMfiSensorSetup):
|
||||
"""Test the mFi switch."""
|
||||
|
||||
PLATFORM = mfi
|
||||
COMPONENT = switch
|
||||
THING = 'switch'
|
||||
GOOD_CONFIG = {
|
||||
'switch': {
|
||||
'platform': 'mfi',
|
||||
'host': 'foo',
|
||||
'port': 6123,
|
||||
'username': 'user',
|
||||
'password': 'pass',
|
||||
'ssl': True,
|
||||
'verify_ssl': True,
|
||||
}
|
||||
}
|
||||
|
||||
@mock.patch('mficlient.client.MFiClient')
|
||||
@mock.patch('homeassistant.components.switch.mfi.MfiSwitch')
|
||||
def test_setup_adds_proper_devices(self, mock_switch, mock_client):
|
||||
"""Test if setup adds devices."""
|
||||
ports = {i: mock.MagicMock(model=model)
|
||||
for i, model in enumerate(mfi.SWITCH_MODELS)}
|
||||
ports['bad'] = mock.MagicMock(model='notaswitch')
|
||||
print(ports['bad'].model)
|
||||
mock_client.return_value.get_devices.return_value = \
|
||||
[mock.MagicMock(ports=ports)]
|
||||
assert setup_component(self.hass, switch.DOMAIN, self.GOOD_CONFIG)
|
||||
for ident, port in ports.items():
|
||||
if ident != 'bad':
|
||||
mock_switch.assert_any_call(port)
|
||||
assert mock.call(ports['bad'], self.hass) not in mock_switch.mock_calls
|
||||
|
||||
|
||||
class TestMfiSwitch(unittest.TestCase):
|
||||
"""Test for mFi switch platform."""
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.port = mock.MagicMock()
|
||||
self.switch = mfi.MfiSwitch(self.port)
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_name(self):
|
||||
"""Test the name."""
|
||||
assert self.port.label == self.switch.name
|
||||
|
||||
def test_update(self):
|
||||
"""Test update."""
|
||||
self.switch.update()
|
||||
assert self.port.refresh.call_count == 1
|
||||
assert self.port.refresh.call_args == mock.call()
|
||||
|
||||
def test_update_with_target_state(self):
|
||||
"""Test update with target state."""
|
||||
self.switch._target_state = True
|
||||
self.port.data = {}
|
||||
self.port.data['output'] = 'stale'
|
||||
self.switch.update()
|
||||
assert 1.0 == self.port.data['output']
|
||||
assert self.switch._target_state is None
|
||||
self.port.data['output'] = 'untouched'
|
||||
self.switch.update()
|
||||
assert 'untouched' == self.port.data['output']
|
||||
|
||||
def test_turn_on(self):
|
||||
"""Test turn_on."""
|
||||
self.switch.turn_on()
|
||||
assert self.port.control.call_count == 1
|
||||
assert self.port.control.call_args == mock.call(True)
|
||||
assert self.switch._target_state
|
||||
|
||||
def test_turn_off(self):
|
||||
"""Test turn_off."""
|
||||
self.switch.turn_off()
|
||||
assert self.port.control.call_count == 1
|
||||
assert self.port.control.call_args == mock.call(False)
|
||||
assert not self.switch._target_state
|
||||
|
||||
def test_current_power_w(self):
|
||||
"""Test current power."""
|
||||
self.port.data = {'active_pwr': 10}
|
||||
assert 10 == self.switch.current_power_w
|
||||
|
||||
def test_current_power_w_no_data(self):
|
||||
"""Test current power if there is no data."""
|
||||
self.port.data = {'notpower': 123}
|
||||
assert 0 == self.switch.current_power_w
|
||||
|
||||
def test_device_state_attributes(self):
|
||||
"""Test the state attributes."""
|
||||
self.port.data = {'v_rms': 1.25,
|
||||
'i_rms': 2.75}
|
||||
assert {'volts': 1.2, 'amps': 2.8} == \
|
||||
self.switch.device_state_attributes
|
||||
@@ -1,210 +0,0 @@
|
||||
"""The tests for the REST switch platform."""
|
||||
import asyncio
|
||||
|
||||
import aiohttp
|
||||
|
||||
import homeassistant.components.switch.rest as rest
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.util.async_ import run_coroutine_threadsafe
|
||||
from homeassistant.helpers.template import Template
|
||||
from tests.common import get_test_home_assistant, assert_setup_component
|
||||
|
||||
|
||||
class TestRestSwitchSetup:
|
||||
"""Tests for setting up the REST switch platform."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_setup_missing_config(self):
|
||||
"""Test setup with configuration missing required entries."""
|
||||
assert not run_coroutine_threadsafe(
|
||||
rest.async_setup_platform(self.hass, {
|
||||
'platform': 'rest'
|
||||
}, None),
|
||||
self.hass.loop
|
||||
).result()
|
||||
|
||||
def test_setup_missing_schema(self):
|
||||
"""Test setup with resource missing schema."""
|
||||
assert not run_coroutine_threadsafe(
|
||||
rest.async_setup_platform(self.hass, {
|
||||
'platform': 'rest',
|
||||
'resource': 'localhost'
|
||||
}, None),
|
||||
self.hass.loop
|
||||
).result()
|
||||
|
||||
def test_setup_failed_connect(self, aioclient_mock):
|
||||
"""Test setup when connection error occurs."""
|
||||
aioclient_mock.get('http://localhost', exc=aiohttp.ClientError)
|
||||
assert not run_coroutine_threadsafe(
|
||||
rest.async_setup_platform(self.hass, {
|
||||
'platform': 'rest',
|
||||
'resource': 'http://localhost',
|
||||
}, None),
|
||||
self.hass.loop
|
||||
).result()
|
||||
|
||||
def test_setup_timeout(self, aioclient_mock):
|
||||
"""Test setup when connection timeout occurs."""
|
||||
aioclient_mock.get('http://localhost', exc=asyncio.TimeoutError())
|
||||
assert not run_coroutine_threadsafe(
|
||||
rest.async_setup_platform(self.hass, {
|
||||
'platform': 'rest',
|
||||
'resource': 'http://localhost',
|
||||
}, None),
|
||||
self.hass.loop
|
||||
).result()
|
||||
|
||||
def test_setup_minimum(self, aioclient_mock):
|
||||
"""Test setup with minimum configuration."""
|
||||
aioclient_mock.get('http://localhost', status=200)
|
||||
with assert_setup_component(1, 'switch'):
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'rest',
|
||||
'resource': 'http://localhost'
|
||||
}
|
||||
})
|
||||
assert aioclient_mock.call_count == 1
|
||||
|
||||
def test_setup(self, aioclient_mock):
|
||||
"""Test setup with valid configuration."""
|
||||
aioclient_mock.get('http://localhost', status=200)
|
||||
assert setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'rest',
|
||||
'name': 'foo',
|
||||
'resource': 'http://localhost',
|
||||
'headers': {'Content-type': 'application/json'},
|
||||
'body_on': 'custom on text',
|
||||
'body_off': 'custom off text',
|
||||
}
|
||||
})
|
||||
assert aioclient_mock.call_count == 1
|
||||
assert_setup_component(1, 'switch')
|
||||
|
||||
|
||||
class TestRestSwitch:
|
||||
"""Tests for REST switch platform."""
|
||||
|
||||
def setup_method(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.name = 'foo'
|
||||
self.method = 'post'
|
||||
self.resource = 'http://localhost/'
|
||||
self.headers = {'Content-type': 'application/json'}
|
||||
self.auth = None
|
||||
self.body_on = Template('on', self.hass)
|
||||
self.body_off = Template('off', self.hass)
|
||||
self.switch = rest.RestSwitch(
|
||||
self.name, self.resource, self.method, self.headers, self.auth,
|
||||
self.body_on, self.body_off, None, 10, True)
|
||||
self.switch.hass = self.hass
|
||||
|
||||
def teardown_method(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_name(self):
|
||||
"""Test the name."""
|
||||
assert self.name == self.switch.name
|
||||
|
||||
def test_is_on_before_update(self):
|
||||
"""Test is_on in initial state."""
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_turn_on_success(self, aioclient_mock):
|
||||
"""Test turn_on."""
|
||||
aioclient_mock.post(self.resource, status=200)
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_turn_on(), self.hass.loop).result()
|
||||
|
||||
assert self.body_on.template == \
|
||||
aioclient_mock.mock_calls[-1][2].decode()
|
||||
assert self.switch.is_on
|
||||
|
||||
def test_turn_on_status_not_ok(self, aioclient_mock):
|
||||
"""Test turn_on when error status returned."""
|
||||
aioclient_mock.post(self.resource, status=500)
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_turn_on(), self.hass.loop).result()
|
||||
|
||||
assert self.body_on.template == \
|
||||
aioclient_mock.mock_calls[-1][2].decode()
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_turn_on_timeout(self, aioclient_mock):
|
||||
"""Test turn_on when timeout occurs."""
|
||||
aioclient_mock.post(self.resource, status=500)
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_turn_on(), self.hass.loop).result()
|
||||
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_turn_off_success(self, aioclient_mock):
|
||||
"""Test turn_off."""
|
||||
aioclient_mock.post(self.resource, status=200)
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_turn_off(), self.hass.loop).result()
|
||||
|
||||
assert self.body_off.template == \
|
||||
aioclient_mock.mock_calls[-1][2].decode()
|
||||
assert not self.switch.is_on
|
||||
|
||||
def test_turn_off_status_not_ok(self, aioclient_mock):
|
||||
"""Test turn_off when error status returned."""
|
||||
aioclient_mock.post(self.resource, status=500)
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_turn_off(), self.hass.loop).result()
|
||||
|
||||
assert self.body_off.template == \
|
||||
aioclient_mock.mock_calls[-1][2].decode()
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_turn_off_timeout(self, aioclient_mock):
|
||||
"""Test turn_off when timeout occurs."""
|
||||
aioclient_mock.post(self.resource, exc=asyncio.TimeoutError())
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_turn_on(), self.hass.loop).result()
|
||||
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_update_when_on(self, aioclient_mock):
|
||||
"""Test update when switch is on."""
|
||||
aioclient_mock.get(self.resource, text=self.body_on.template)
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_update(), self.hass.loop).result()
|
||||
|
||||
assert self.switch.is_on
|
||||
|
||||
def test_update_when_off(self, aioclient_mock):
|
||||
"""Test update when switch is off."""
|
||||
aioclient_mock.get(self.resource, text=self.body_off.template)
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_update(), self.hass.loop).result()
|
||||
|
||||
assert not self.switch.is_on
|
||||
|
||||
def test_update_when_unknown(self, aioclient_mock):
|
||||
"""Test update when unknown status returned."""
|
||||
aioclient_mock.get(self.resource, text='unknown status')
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_update(), self.hass.loop).result()
|
||||
|
||||
assert self.switch.is_on is None
|
||||
|
||||
def test_update_timeout(self, aioclient_mock):
|
||||
"""Test update when timeout occurs."""
|
||||
aioclient_mock.get(self.resource, exc=asyncio.TimeoutError())
|
||||
run_coroutine_threadsafe(
|
||||
self.switch.async_update(), self.hass.loop).result()
|
||||
|
||||
assert self.switch.is_on is None
|
||||
@@ -1,353 +0,0 @@
|
||||
"""Test for RFlink switch components.
|
||||
|
||||
Test setup of rflink switch component/platform. State tracking and
|
||||
control of Rflink switch devices.
|
||||
|
||||
"""
|
||||
|
||||
from homeassistant.components.rflink import EVENT_BUTTON_PRESSED
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON, STATE_OFF)
|
||||
from homeassistant.core import callback, State, CoreState
|
||||
|
||||
from tests.common import mock_restore_cache
|
||||
from tests.components.rflink.test_init import mock_rflink
|
||||
|
||||
DOMAIN = 'switch'
|
||||
|
||||
CONFIG = {
|
||||
'rflink': {
|
||||
'port': '/dev/ttyABC0',
|
||||
'ignore_devices': ['ignore_wildcard_*', 'ignore_sensor'],
|
||||
},
|
||||
DOMAIN: {
|
||||
'platform': 'rflink',
|
||||
'devices': {
|
||||
'protocol_0_0': {
|
||||
'name': 'test',
|
||||
'aliases': ['test_alias_0_0'],
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
async def test_default_setup(hass, monkeypatch):
|
||||
"""Test all basic functionality of the rflink switch component."""
|
||||
# setup mocking rflink module
|
||||
event_callback, create, protocol, _ = await mock_rflink(
|
||||
hass, CONFIG, DOMAIN, monkeypatch)
|
||||
|
||||
# make sure arguments are passed
|
||||
assert create.call_args_list[0][1]['ignore']
|
||||
|
||||
# test default state of switch loaded from config
|
||||
switch_initial = hass.states.get('switch.test')
|
||||
assert switch_initial.state == 'off'
|
||||
assert switch_initial.attributes['assumed_state']
|
||||
|
||||
# switch should follow state of the hardware device by interpreting
|
||||
# incoming events for its name and aliases
|
||||
|
||||
# mock incoming command event for this device
|
||||
event_callback({
|
||||
'id': 'protocol_0_0',
|
||||
'command': 'on',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
switch_after_first_command = hass.states.get('switch.test')
|
||||
assert switch_after_first_command.state == 'on'
|
||||
# also after receiving first command state not longer has to be assumed
|
||||
assert not switch_after_first_command.attributes.get('assumed_state')
|
||||
|
||||
# mock incoming command event for this device
|
||||
event_callback({
|
||||
'id': 'protocol_0_0',
|
||||
'command': 'off',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('switch.test').state == 'off'
|
||||
|
||||
# test following aliases
|
||||
# mock incoming command event for this device alias
|
||||
event_callback({
|
||||
'id': 'test_alias_0_0',
|
||||
'command': 'on',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get('switch.test').state == 'on'
|
||||
|
||||
# The switch component does not support adding new devices for incoming
|
||||
# events because every new unknown device is added as a light by default.
|
||||
|
||||
# test changing state from HA propagates to Rflink
|
||||
hass.async_create_task(
|
||||
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF,
|
||||
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
||||
assert protocol.send_command_ack.call_args_list[0][0][0] == 'protocol_0_0'
|
||||
assert protocol.send_command_ack.call_args_list[0][0][1] == 'off'
|
||||
|
||||
hass.async_create_task(
|
||||
hass.services.async_call(DOMAIN, SERVICE_TURN_ON,
|
||||
{ATTR_ENTITY_ID: DOMAIN + '.test'}))
|
||||
await hass.async_block_till_done()
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
||||
assert protocol.send_command_ack.call_args_list[1][0][1] == 'on'
|
||||
|
||||
|
||||
async def test_group_alias(hass, monkeypatch):
|
||||
"""Group aliases should only respond to group commands (allon/alloff)."""
|
||||
config = {
|
||||
'rflink': {
|
||||
'port': '/dev/ttyABC0',
|
||||
},
|
||||
DOMAIN: {
|
||||
'platform': 'rflink',
|
||||
'devices': {
|
||||
'protocol_0_0': {
|
||||
'name': 'test',
|
||||
'group_aliases': ['test_group_0_0'],
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# setup mocking rflink module
|
||||
event_callback, _, _, _ = await mock_rflink(
|
||||
hass, config, DOMAIN, monkeypatch)
|
||||
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
||||
|
||||
# test sending group command to group alias
|
||||
event_callback({
|
||||
'id': 'test_group_0_0',
|
||||
'command': 'allon',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
||||
|
||||
# test sending group command to group alias
|
||||
event_callback({
|
||||
'id': 'test_group_0_0',
|
||||
'command': 'off',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
||||
|
||||
|
||||
async def test_nogroup_alias(hass, monkeypatch):
|
||||
"""Non group aliases should not respond to group commands."""
|
||||
config = {
|
||||
'rflink': {
|
||||
'port': '/dev/ttyABC0',
|
||||
},
|
||||
DOMAIN: {
|
||||
'platform': 'rflink',
|
||||
'devices': {
|
||||
'protocol_0_0': {
|
||||
'name': 'test',
|
||||
'nogroup_aliases': ['test_nogroup_0_0'],
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# setup mocking rflink module
|
||||
event_callback, _, _, _ = await mock_rflink(
|
||||
hass, config, DOMAIN, monkeypatch)
|
||||
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
||||
|
||||
# test sending group command to nogroup alias
|
||||
event_callback({
|
||||
'id': 'test_nogroup_0_0',
|
||||
'command': 'allon',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
# should not affect state
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
||||
|
||||
# test sending group command to nogroup alias
|
||||
event_callback({
|
||||
'id': 'test_nogroup_0_0',
|
||||
'command': 'on',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
# should affect state
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
||||
|
||||
|
||||
async def test_nogroup_device_id(hass, monkeypatch):
|
||||
"""Device id that do not respond to group commands (allon/alloff)."""
|
||||
config = {
|
||||
'rflink': {
|
||||
'port': '/dev/ttyABC0',
|
||||
},
|
||||
DOMAIN: {
|
||||
'platform': 'rflink',
|
||||
'devices': {
|
||||
'test_nogroup_0_0': {
|
||||
'name': 'test',
|
||||
'group': False,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# setup mocking rflink module
|
||||
event_callback, _, _, _ = await mock_rflink(
|
||||
hass, config, DOMAIN, monkeypatch)
|
||||
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
||||
|
||||
# test sending group command to nogroup
|
||||
event_callback({
|
||||
'id': 'test_nogroup_0_0',
|
||||
'command': 'allon',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
# should not affect state
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'off'
|
||||
|
||||
# test sending group command to nogroup
|
||||
event_callback({
|
||||
'id': 'test_nogroup_0_0',
|
||||
'command': 'on',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
# should affect state
|
||||
assert hass.states.get(DOMAIN + '.test').state == 'on'
|
||||
|
||||
|
||||
async def test_device_defaults(hass, monkeypatch):
|
||||
"""Event should fire if device_defaults config says so."""
|
||||
config = {
|
||||
'rflink': {
|
||||
'port': '/dev/ttyABC0',
|
||||
},
|
||||
DOMAIN: {
|
||||
'platform': 'rflink',
|
||||
'device_defaults': {
|
||||
'fire_event': True,
|
||||
},
|
||||
'devices': {
|
||||
'protocol_0_0': {
|
||||
'name': 'test',
|
||||
'aliases': ['test_alias_0_0'],
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# setup mocking rflink module
|
||||
event_callback, _, _, _ = await mock_rflink(
|
||||
hass, config, DOMAIN, monkeypatch)
|
||||
|
||||
calls = []
|
||||
|
||||
@callback
|
||||
def listener(event):
|
||||
calls.append(event)
|
||||
hass.bus.async_listen_once(EVENT_BUTTON_PRESSED, listener)
|
||||
|
||||
# test event for new unconfigured sensor
|
||||
event_callback({
|
||||
'id': 'protocol_0_0',
|
||||
'command': 'off',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert calls[0].data == {'state': 'off', 'entity_id': DOMAIN + '.test'}
|
||||
|
||||
|
||||
async def test_not_firing_default(hass, monkeypatch):
|
||||
"""By default no bus events should be fired."""
|
||||
config = {
|
||||
'rflink': {
|
||||
'port': '/dev/ttyABC0',
|
||||
},
|
||||
DOMAIN: {
|
||||
'platform': 'rflink',
|
||||
'devices': {
|
||||
'protocol_0_0': {
|
||||
'name': 'test',
|
||||
'aliases': ['test_alias_0_0'],
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
# setup mocking rflink module
|
||||
event_callback, _, _, _ = await mock_rflink(
|
||||
hass, config, DOMAIN, monkeypatch)
|
||||
|
||||
calls = []
|
||||
|
||||
@callback
|
||||
def listener(event):
|
||||
calls.append(event)
|
||||
hass.bus.async_listen_once(EVENT_BUTTON_PRESSED, listener)
|
||||
|
||||
# test event for new unconfigured sensor
|
||||
event_callback({
|
||||
'id': 'protocol_0_0',
|
||||
'command': 'off',
|
||||
})
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert not calls, 'an event has been fired'
|
||||
|
||||
|
||||
async def test_restore_state(hass, monkeypatch):
|
||||
"""Ensure states are restored on startup."""
|
||||
config = {
|
||||
'rflink': {
|
||||
'port': '/dev/ttyABC0',
|
||||
},
|
||||
DOMAIN: {
|
||||
'platform': 'rflink',
|
||||
'devices': {
|
||||
'test': {
|
||||
'name': 's1',
|
||||
'aliases': ['test_alias_0_0'],
|
||||
},
|
||||
'switch_test': {
|
||||
'name': 's2',
|
||||
},
|
||||
'switch_s3': {
|
||||
'name': 's3',
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mock_restore_cache(hass, (
|
||||
State(DOMAIN + '.s1', STATE_ON, ),
|
||||
State(DOMAIN + '.s2', STATE_OFF, ),
|
||||
))
|
||||
|
||||
hass.state = CoreState.starting
|
||||
|
||||
# setup mocking rflink module
|
||||
_, _, _, _ = await mock_rflink(hass, config, DOMAIN, monkeypatch)
|
||||
|
||||
state = hass.states.get(DOMAIN + '.s1')
|
||||
assert state
|
||||
assert state.state == STATE_ON
|
||||
|
||||
state = hass.states.get(DOMAIN + '.s2')
|
||||
assert state
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
# not cached switch must default values
|
||||
state = hass.states.get(DOMAIN + '.s3')
|
||||
assert state
|
||||
assert state.state == STATE_OFF
|
||||
assert state.attributes['assumed_state']
|
||||
@@ -1,448 +0,0 @@
|
||||
"""The tests for the Template switch platform."""
|
||||
from homeassistant.core import callback
|
||||
from homeassistant import setup
|
||||
from homeassistant.const import STATE_ON, STATE_OFF
|
||||
|
||||
from tests.common import (
|
||||
get_test_home_assistant, assert_setup_component)
|
||||
from tests.components.switch import common
|
||||
|
||||
|
||||
class TestTemplateSwitch:
|
||||
"""Test the Template switch."""
|
||||
|
||||
hass = None
|
||||
calls = None
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
def setup_method(self, method):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.calls = []
|
||||
|
||||
@callback
|
||||
def record_call(service):
|
||||
"""Track function calls.."""
|
||||
self.calls.append(service)
|
||||
|
||||
self.hass.services.register('test', 'automation', record_call)
|
||||
|
||||
def teardown_method(self, method):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
def test_template_state_text(self):
|
||||
"""Test the state text of a template."""
|
||||
with assert_setup_component(1, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.set('switch.test_state', STATE_ON)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.state == STATE_ON
|
||||
|
||||
state = self.hass.states.set('switch.test_state', STATE_OFF)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
def test_template_state_boolean_on(self):
|
||||
"""Test the setting of the state with boolean on."""
|
||||
with assert_setup_component(1, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ 1 == 1 }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.state == STATE_ON
|
||||
|
||||
def test_template_state_boolean_off(self):
|
||||
"""Test the setting of the state with off."""
|
||||
with assert_setup_component(1, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ 1 == 2 }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
def test_icon_template(self):
|
||||
"""Test icon template."""
|
||||
with assert_setup_component(1, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'icon_template':
|
||||
"{% if states.switch.test_state.state %}"
|
||||
"mdi:check"
|
||||
"{% endif %}"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.attributes.get('icon') == ''
|
||||
|
||||
state = self.hass.states.set('switch.test_state', STATE_ON)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.attributes['icon'] == 'mdi:check'
|
||||
|
||||
def test_entity_picture_template(self):
|
||||
"""Test entity_picture template."""
|
||||
with assert_setup_component(1, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'entity_picture_template':
|
||||
"{% if states.switch.test_state.state %}"
|
||||
"/local/switch.png"
|
||||
"{% endif %}"
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.attributes.get('entity_picture') == ''
|
||||
|
||||
state = self.hass.states.set('switch.test_state', STATE_ON)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.attributes['entity_picture'] == '/local/switch.png'
|
||||
|
||||
def test_template_syntax_error(self):
|
||||
"""Test templating syntax error."""
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{% if rubbish %}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_invalid_name_does_not_create(self):
|
||||
"""Test invalid name."""
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test INVALID switch': {
|
||||
'value_template':
|
||||
"{{ rubbish }",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_invalid_switch_does_not_create(self):
|
||||
"""Test invalid switch."""
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': 'Invalid'
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_no_switches_does_not_create(self):
|
||||
"""Test if there are no switches no creation."""
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template'
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_missing_template_does_not_create(self):
|
||||
"""Test missing template."""
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'not_value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_missing_on_does_not_create(self):
|
||||
"""Test missing on."""
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'not_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_missing_off_does_not_create(self):
|
||||
"""Test missing off."""
|
||||
with assert_setup_component(0, 'switch'):
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
'not_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert self.hass.states.all() == []
|
||||
|
||||
def test_on_action(self):
|
||||
"""Test on action."""
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'test.automation'
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'switch.turn_off',
|
||||
'entity_id': 'switch.test_state'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.hass.states.set('switch.test_state', STATE_OFF)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.state == STATE_OFF
|
||||
|
||||
common.turn_on(self.hass, 'switch.test_template_switch')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(self.calls) == 1
|
||||
|
||||
def test_off_action(self):
|
||||
"""Test off action."""
|
||||
assert setup.setup_component(self.hass, 'switch', {
|
||||
'switch': {
|
||||
'platform': 'template',
|
||||
'switches': {
|
||||
'test_template_switch': {
|
||||
'value_template':
|
||||
"{{ states.switch.test_state.state }}",
|
||||
'turn_on': {
|
||||
'service': 'switch.turn_on',
|
||||
'entity_id': 'switch.test_state'
|
||||
|
||||
},
|
||||
'turn_off': {
|
||||
'service': 'test.automation'
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
self.hass.start()
|
||||
self.hass.block_till_done()
|
||||
|
||||
self.hass.states.set('switch.test_state', STATE_ON)
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.test_template_switch')
|
||||
assert state.state == STATE_ON
|
||||
|
||||
common.turn_off(self.hass, 'switch.test_template_switch')
|
||||
self.hass.block_till_done()
|
||||
|
||||
assert len(self.calls) == 1
|
||||
@@ -1,199 +0,0 @@
|
||||
"""Test the Vultr switch platform."""
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
import requests_mock
|
||||
import pytest
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import vultr
|
||||
from homeassistant.components import vultr as base_vultr
|
||||
from homeassistant.components.vultr import (
|
||||
ATTR_ALLOWED_BANDWIDTH, ATTR_AUTO_BACKUPS, ATTR_IPV4_ADDRESS,
|
||||
ATTR_COST_PER_MONTH, ATTR_CREATED_AT, ATTR_SUBSCRIPTION_ID,
|
||||
CONF_SUBSCRIPTION)
|
||||
from homeassistant.const import (
|
||||
CONF_PLATFORM, CONF_NAME)
|
||||
|
||||
from tests.components.vultr.test_init import VALID_CONFIG
|
||||
from tests.common import (
|
||||
get_test_home_assistant, load_fixture)
|
||||
|
||||
|
||||
class TestVultrSwitchSetup(unittest.TestCase):
|
||||
"""Test the Vultr switch platform."""
|
||||
|
||||
DEVICES = []
|
||||
|
||||
def add_entities(self, devices, action):
|
||||
"""Mock add devices."""
|
||||
for device in devices:
|
||||
self.DEVICES.append(device)
|
||||
|
||||
def setUp(self):
|
||||
"""Init values for this testcase class."""
|
||||
self.hass = get_test_home_assistant()
|
||||
self.configs = [
|
||||
{
|
||||
CONF_SUBSCRIPTION: '576965',
|
||||
CONF_NAME: "A Server"
|
||||
},
|
||||
{
|
||||
CONF_SUBSCRIPTION: '123456',
|
||||
CONF_NAME: "Failed Server"
|
||||
},
|
||||
{
|
||||
CONF_SUBSCRIPTION: '555555',
|
||||
CONF_NAME: vultr.DEFAULT_NAME
|
||||
}
|
||||
]
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop our started services."""
|
||||
self.hass.stop()
|
||||
|
||||
@requests_mock.Mocker()
|
||||
def test_switch(self, mock):
|
||||
"""Test successful instance."""
|
||||
mock.get(
|
||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
||||
text=load_fixture('vultr_account_info.json'))
|
||||
|
||||
with patch(
|
||||
'vultr.Vultr.server_list',
|
||||
return_value=json.loads(
|
||||
load_fixture('vultr_server_list.json'))):
|
||||
# Setup hub
|
||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
||||
|
||||
# Setup each of our test configs
|
||||
for config in self.configs:
|
||||
vultr.setup_platform(self.hass,
|
||||
config,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
assert len(self.DEVICES) == 3
|
||||
|
||||
tested = 0
|
||||
|
||||
for device in self.DEVICES:
|
||||
if device.subscription == '555555':
|
||||
assert 'Vultr {}' == device.name
|
||||
tested += 1
|
||||
|
||||
device.update()
|
||||
device_attrs = device.device_state_attributes
|
||||
|
||||
if device.subscription == '555555':
|
||||
assert 'Vultr Another Server' == device.name
|
||||
tested += 1
|
||||
|
||||
if device.name == 'A Server':
|
||||
assert device.is_on is True
|
||||
assert 'on' == device.state
|
||||
assert 'mdi:server' == device.icon
|
||||
assert '1000' == \
|
||||
device_attrs[ATTR_ALLOWED_BANDWIDTH]
|
||||
assert 'yes' == \
|
||||
device_attrs[ATTR_AUTO_BACKUPS]
|
||||
assert '123.123.123.123' == \
|
||||
device_attrs[ATTR_IPV4_ADDRESS]
|
||||
assert '10.05' == \
|
||||
device_attrs[ATTR_COST_PER_MONTH]
|
||||
assert '2013-12-19 14:45:41' == \
|
||||
device_attrs[ATTR_CREATED_AT]
|
||||
assert '576965' == \
|
||||
device_attrs[ATTR_SUBSCRIPTION_ID]
|
||||
tested += 1
|
||||
|
||||
elif device.name == 'Failed Server':
|
||||
assert device.is_on is False
|
||||
assert 'off' == device.state
|
||||
assert 'mdi:server-off' == device.icon
|
||||
assert '1000' == \
|
||||
device_attrs[ATTR_ALLOWED_BANDWIDTH]
|
||||
assert 'no' == \
|
||||
device_attrs[ATTR_AUTO_BACKUPS]
|
||||
assert '192.168.100.50' == \
|
||||
device_attrs[ATTR_IPV4_ADDRESS]
|
||||
assert '73.25' == \
|
||||
device_attrs[ATTR_COST_PER_MONTH]
|
||||
assert '2014-10-13 14:45:41' == \
|
||||
device_attrs[ATTR_CREATED_AT]
|
||||
assert '123456' == \
|
||||
device_attrs[ATTR_SUBSCRIPTION_ID]
|
||||
tested += 1
|
||||
|
||||
assert 4 == tested
|
||||
|
||||
@requests_mock.Mocker()
|
||||
def test_turn_on(self, mock):
|
||||
"""Test turning a subscription on."""
|
||||
with patch(
|
||||
'vultr.Vultr.server_list',
|
||||
return_value=json.loads(load_fixture('vultr_server_list.json'))), \
|
||||
patch('vultr.Vultr.server_start') as mock_start:
|
||||
for device in self.DEVICES:
|
||||
if device.name == 'Failed Server':
|
||||
device.turn_on()
|
||||
|
||||
# Turn on
|
||||
assert 1 == mock_start.call_count
|
||||
|
||||
@requests_mock.Mocker()
|
||||
def test_turn_off(self, mock):
|
||||
"""Test turning a subscription off."""
|
||||
with patch(
|
||||
'vultr.Vultr.server_list',
|
||||
return_value=json.loads(load_fixture('vultr_server_list.json'))), \
|
||||
patch('vultr.Vultr.server_halt') as mock_halt:
|
||||
for device in self.DEVICES:
|
||||
if device.name == 'A Server':
|
||||
device.turn_off()
|
||||
|
||||
# Turn off
|
||||
assert 1 == mock_halt.call_count
|
||||
|
||||
def test_invalid_switch_config(self):
|
||||
"""Test config type failures."""
|
||||
with pytest.raises(vol.Invalid): # No subscription
|
||||
vultr.PLATFORM_SCHEMA({
|
||||
CONF_PLATFORM: base_vultr.DOMAIN,
|
||||
})
|
||||
|
||||
@requests_mock.Mocker()
|
||||
def test_invalid_switches(self, mock):
|
||||
"""Test the VultrSwitch fails."""
|
||||
mock.get(
|
||||
'https://api.vultr.com/v1/account/info?api_key=ABCDEFG1234567',
|
||||
text=load_fixture('vultr_account_info.json'))
|
||||
|
||||
with patch(
|
||||
'vultr.Vultr.server_list',
|
||||
return_value=json.loads(
|
||||
load_fixture('vultr_server_list.json'))):
|
||||
# Setup hub
|
||||
base_vultr.setup(self.hass, VALID_CONFIG)
|
||||
|
||||
bad_conf = {} # No subscription
|
||||
|
||||
no_subs_setup = vultr.setup_platform(self.hass,
|
||||
bad_conf,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
assert no_subs_setup is not None
|
||||
|
||||
bad_conf = {
|
||||
CONF_NAME: "Missing Server",
|
||||
CONF_SUBSCRIPTION: '665544'
|
||||
} # Sub not associated with API key (not in server_list)
|
||||
|
||||
wrong_subs_setup = vultr.setup_platform(self.hass,
|
||||
bad_conf,
|
||||
self.add_entities,
|
||||
None)
|
||||
|
||||
assert wrong_subs_setup is not None
|
||||
@@ -1,193 +0,0 @@
|
||||
"""The tests for the wake on lan switch platform."""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.const import STATE_ON, STATE_OFF
|
||||
import homeassistant.components.switch as switch
|
||||
|
||||
from tests.common import get_test_home_assistant, mock_service
|
||||
from tests.components.switch import common
|
||||
|
||||
|
||||
TEST_STATE = None
|
||||
|
||||
|
||||
def send_magic_packet(*macs, **kwargs):
|
||||
"""Fake call for sending magic packets."""
|
||||
return
|
||||
|
||||
|
||||
def call(cmd, stdout, stderr):
|
||||
"""Return fake subprocess return codes."""
|
||||
if cmd[5] == 'validhostname' and TEST_STATE:
|
||||
return 0
|
||||
return 2
|
||||
|
||||
|
||||
def system():
|
||||
"""Fake system call to test the windows platform."""
|
||||
return 'Windows'
|
||||
|
||||
|
||||
class TestWOLSwitch(unittest.TestCase):
|
||||
"""Test the wol switch."""
|
||||
|
||||
def setUp(self):
|
||||
"""Set up things to be run when tests are started."""
|
||||
self.hass = get_test_home_assistant()
|
||||
|
||||
def tearDown(self):
|
||||
"""Stop everything that was started."""
|
||||
self.hass.stop()
|
||||
|
||||
@patch('wakeonlan.send_magic_packet', new=send_magic_packet)
|
||||
@patch('subprocess.call', new=call)
|
||||
def test_valid_hostname(self):
|
||||
"""Test with valid hostname."""
|
||||
global TEST_STATE
|
||||
TEST_STATE = False
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'wake_on_lan',
|
||||
'mac_address': '00-01-02-03-04-05',
|
||||
'host': 'validhostname',
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
TEST_STATE = True
|
||||
|
||||
common.turn_on(self.hass, 'switch.wake_on_lan')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
common.turn_off(self.hass, 'switch.wake_on_lan')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
@patch('wakeonlan.send_magic_packet', new=send_magic_packet)
|
||||
@patch('subprocess.call', new=call)
|
||||
@patch('platform.system', new=system)
|
||||
def test_valid_hostname_windows(self):
|
||||
"""Test with valid hostname on windows."""
|
||||
global TEST_STATE
|
||||
TEST_STATE = False
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'wake_on_lan',
|
||||
'mac_address': '00-01-02-03-04-05',
|
||||
'host': 'validhostname',
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
TEST_STATE = True
|
||||
|
||||
common.turn_on(self.hass, 'switch.wake_on_lan')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_ON == state.state
|
||||
|
||||
@patch('wakeonlan.send_magic_packet', new=send_magic_packet)
|
||||
@patch('subprocess.call', new=call)
|
||||
def test_minimal_config(self):
|
||||
"""Test with minimal config."""
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'wake_on_lan',
|
||||
'mac_address': '00-01-02-03-04-05',
|
||||
}
|
||||
})
|
||||
|
||||
@patch('wakeonlan.send_magic_packet', new=send_magic_packet)
|
||||
@patch('subprocess.call', new=call)
|
||||
def test_broadcast_config(self):
|
||||
"""Test with broadcast address config."""
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'wake_on_lan',
|
||||
'mac_address': '00-01-02-03-04-05',
|
||||
'broadcast_address': '255.255.255.255',
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
common.turn_on(self.hass, 'switch.wake_on_lan')
|
||||
self.hass.block_till_done()
|
||||
|
||||
@patch('wakeonlan.send_magic_packet', new=send_magic_packet)
|
||||
@patch('subprocess.call', new=call)
|
||||
def test_off_script(self):
|
||||
"""Test with turn off script."""
|
||||
global TEST_STATE
|
||||
TEST_STATE = False
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'wake_on_lan',
|
||||
'mac_address': '00-01-02-03-04-05',
|
||||
'host': 'validhostname',
|
||||
'turn_off': {
|
||||
'service': 'shell_command.turn_off_target',
|
||||
},
|
||||
}
|
||||
})
|
||||
calls = mock_service(self.hass, 'shell_command', 'turn_off_target')
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
TEST_STATE = True
|
||||
|
||||
common.turn_on(self.hass, 'switch.wake_on_lan')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_ON == state.state
|
||||
assert len(calls) == 0
|
||||
|
||||
TEST_STATE = False
|
||||
|
||||
common.turn_off(self.hass, 'switch.wake_on_lan')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_OFF == state.state
|
||||
assert len(calls) == 1
|
||||
|
||||
@patch('wakeonlan.send_magic_packet', new=send_magic_packet)
|
||||
@patch('subprocess.call', new=call)
|
||||
@patch('platform.system', new=system)
|
||||
def test_invalid_hostname_windows(self):
|
||||
"""Test with invalid hostname on windows."""
|
||||
global TEST_STATE
|
||||
TEST_STATE = False
|
||||
assert setup_component(self.hass, switch.DOMAIN, {
|
||||
'switch': {
|
||||
'platform': 'wake_on_lan',
|
||||
'mac_address': '00-01-02-03-04-05',
|
||||
'host': 'invalidhostname',
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_OFF == state.state
|
||||
|
||||
TEST_STATE = True
|
||||
|
||||
common.turn_on(self.hass, 'switch.wake_on_lan')
|
||||
self.hass.block_till_done()
|
||||
|
||||
state = self.hass.states.get('switch.wake_on_lan')
|
||||
assert STATE_OFF == state.state
|
||||
Reference in new issue
Block a user