Remove service helper (5) (#16917)

* Update switch

* Update script

* Update light

* Fix tests

* Fix config/script hook

* Async_create_task

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

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
"""Collection of helper methods.
All containing methods are legacy helpers that should not be used by new
components. Instead call the service directly.
"""
from homeassistant.components.switch import DOMAIN
from homeassistant.const import (
ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON)
from homeassistant.core import callback
from homeassistant.loader import bind_hass
@bind_hass
def turn_on(hass, entity_id=None):
"""Turn all or specified switch on."""
hass.add_job(async_turn_on, hass, entity_id)
@callback
@bind_hass
def async_turn_on(hass, entity_id=None):
"""Turn all or specified switch on."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_TURN_ON, data))
@bind_hass
def turn_off(hass, entity_id=None):
"""Turn all or specified switch off."""
hass.add_job(async_turn_off, hass, entity_id)
@callback
@bind_hass
def async_turn_off(hass, entity_id=None):
"""Turn all or specified switch off."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.async_add_job(
hass.services.async_call(DOMAIN, SERVICE_TURN_OFF, data))
+9 -8
View File
@@ -10,6 +10,7 @@ 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
@@ -44,13 +45,13 @@ class TestCommandSwitch(unittest.TestCase):
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
@@ -78,13 +79,13 @@ class TestCommandSwitch(unittest.TestCase):
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
@@ -114,13 +115,13 @@ class TestCommandSwitch(unittest.TestCase):
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
@@ -147,13 +148,13 @@ class TestCommandSwitch(unittest.TestCase):
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
state = self.hass.states.get('switch.test')
+19 -17
View File
@@ -11,6 +11,8 @@ 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):
@@ -147,7 +149,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -193,7 +195,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -240,7 +242,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -286,7 +288,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -334,7 +336,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -383,7 +385,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -434,7 +436,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -484,7 +486,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -534,7 +536,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -584,7 +586,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -633,7 +635,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -681,7 +683,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -698,9 +700,9 @@ class TestSwitchFlux(unittest.TestCase):
{light.DOMAIN: {CONF_PLATFORM: 'test'}}))
dev1, dev2, dev3 = platform.DEVICES
light.turn_on(self.hass, entity_id=dev2.entity_id)
common_light.turn_on(self.hass, entity_id=dev2.entity_id)
self.hass.block_till_done()
light.turn_on(self.hass, entity_id=dev3.entity_id)
common_light.turn_on(self.hass, entity_id=dev3.entity_id)
self.hass.block_till_done()
state = self.hass.states.get(dev1.entity_id)
@@ -743,7 +745,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -794,7 +796,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
@@ -838,7 +840,7 @@ class TestSwitchFlux(unittest.TestCase):
})
turn_on_calls = mock_service(
self.hass, light.DOMAIN, SERVICE_TURN_ON)
switch.turn_on(self.hass, 'switch.flux')
common.turn_on(self.hass, 'switch.flux')
self.hass.block_till_done()
fire_time_changed(self.hass, test_time)
self.hass.block_till_done()
+5 -4
View File
@@ -8,6 +8,7 @@ from homeassistant.components import switch
from homeassistant.const import STATE_ON, STATE_OFF, CONF_PLATFORM
from tests.common import get_test_home_assistant
from tests.components.switch import common
class TestSwitch(unittest.TestCase):
@@ -41,8 +42,8 @@ class TestSwitch(unittest.TestCase):
self.assertFalse(switch.is_on(self.hass, self.switch_2.entity_id))
self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))
switch.turn_off(self.hass, self.switch_1.entity_id)
switch.turn_on(self.hass, self.switch_2.entity_id)
common.turn_off(self.hass, self.switch_1.entity_id)
common.turn_on(self.hass, self.switch_2.entity_id)
self.hass.block_till_done()
@@ -51,7 +52,7 @@ class TestSwitch(unittest.TestCase):
self.assertTrue(switch.is_on(self.hass, self.switch_2.entity_id))
# Turn all off
switch.turn_off(self.hass)
common.turn_off(self.hass)
self.hass.block_till_done()
@@ -64,7 +65,7 @@ class TestSwitch(unittest.TestCase):
self.assertFalse(switch.is_on(self.hass, self.switch_3.entity_id))
# Turn all on
switch.turn_on(self.hass)
common.turn_on(self.hass)
self.hass.block_till_done()
+5 -3
View File
@@ -5,9 +5,11 @@ from unittest import mock
from homeassistant import setup
from homeassistant.components import litejet
from tests.common import get_test_home_assistant
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'
@@ -88,11 +90,11 @@ class TestLiteJetSwitch(unittest.TestCase):
assert not switch.is_on(self.hass, ENTITY_SWITCH)
switch.turn_on(self.hass, ENTITY_SWITCH)
common.turn_on(self.hass, ENTITY_SWITCH)
self.hass.block_till_done()
self.mock_lj.press_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
switch.turn_off(self.hass, ENTITY_SWITCH)
common.turn_off(self.hass, ENTITY_SWITCH)
self.hass.block_till_done()
self.mock_lj.release_switch.assert_called_with(ENTITY_SWITCH_NUMBER)
+4 -2
View File
@@ -8,9 +8,11 @@ from homeassistant.const import STATE_ON, STATE_OFF, STATE_UNAVAILABLE,\
import homeassistant.core as ha
import homeassistant.components.switch as switch
from homeassistant.components.mqtt.discovery import async_start
from tests.common import (
mock_mqtt_component, fire_mqtt_message, get_test_home_assistant, mock_coro,
async_mock_mqtt_component, async_fire_mqtt_message)
from tests.components.switch import common
class TestSwitchMQTT(unittest.TestCase):
@@ -75,7 +77,7 @@ class TestSwitchMQTT(unittest.TestCase):
self.assertEqual(STATE_ON, state.state)
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
switch.turn_on(self.hass, 'switch.test')
common.turn_on(self.hass, 'switch.test')
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
@@ -84,7 +86,7 @@ class TestSwitchMQTT(unittest.TestCase):
state = self.hass.states.get('switch.test')
self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.test')
common.turn_off(self.hass, 'switch.test')
self.hass.block_till_done()
self.mock_publish.async_publish.assert_called_once_with(
+3 -3
View File
@@ -1,11 +1,11 @@
"""The tests for the Template switch platform."""
from homeassistant.core import callback
from homeassistant import setup
import homeassistant.components as core
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:
@@ -406,7 +406,7 @@ class TestTemplateSwitch:
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_OFF
core.switch.turn_on(self.hass, 'switch.test_template_switch')
common.turn_on(self.hass, 'switch.test_template_switch')
self.hass.block_till_done()
assert len(self.calls) == 1
@@ -442,7 +442,7 @@ class TestTemplateSwitch:
state = self.hass.states.get('switch.test_template_switch')
assert state.state == STATE_ON
core.switch.turn_off(self.hass, 'switch.test_template_switch')
common.turn_off(self.hass, 'switch.test_template_switch')
self.hass.block_till_done()
assert len(self.calls) == 1
+8 -7
View File
@@ -7,6 +7,7 @@ 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
@@ -59,13 +60,13 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = True
switch.turn_on(self.hass, 'switch.wake_on_lan')
common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan')
self.assertEqual(STATE_ON, state.state)
switch.turn_off(self.hass, 'switch.wake_on_lan')
common.turn_off(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan')
@@ -91,7 +92,7 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = True
switch.turn_on(self.hass, 'switch.wake_on_lan')
common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan')
@@ -123,7 +124,7 @@ class TestWOLSwitch(unittest.TestCase):
state = self.hass.states.get('switch.wake_on_lan')
self.assertEqual(STATE_OFF, state.state)
switch.turn_on(self.hass, 'switch.wake_on_lan')
common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done()
@patch('wakeonlan.send_magic_packet', new=send_magic_packet)
@@ -149,7 +150,7 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = True
switch.turn_on(self.hass, 'switch.wake_on_lan')
common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan')
@@ -158,7 +159,7 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = False
switch.turn_off(self.hass, 'switch.wake_on_lan')
common.turn_off(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan')
@@ -185,7 +186,7 @@ class TestWOLSwitch(unittest.TestCase):
TEST_STATE = True
switch.turn_on(self.hass, 'switch.wake_on_lan')
common.turn_on(self.hass, 'switch.wake_on_lan')
self.hass.block_till_done()
state = self.hass.states.get('switch.wake_on_lan')