Remove service helper (3) (#16879)

* Update duckdns

* Update google_assistant

* Update group

* Update homematic

* Update image_processing

* Update input_boolean

* Update input_number

* Update input_select

* Update input_text
This commit is contained in:
cdce8p authored and Paulus Schoutsen committed 2018-09-26 08:50:05 +02:00
1 parent 672fc61bb2
commit e2a56721d3
22 files changed
+217 -231

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
"""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.group import ATTR_VISIBLE, DOMAIN, \
SERVICE_SET_VISIBILITY
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.loader import bind_hass
@bind_hass
def set_visibility(hass, entity_id=None, visible=True):
"""Hide or shows a group."""
data = {ATTR_ENTITY_ID: entity_id, ATTR_VISIBLE: visible}
hass.services.call(DOMAIN, SERVICE_SET_VISIBILITY, data)
+3 -2
View File
@@ -12,6 +12,7 @@ from homeassistant.const import (
import homeassistant.components.group as group
from tests.common import get_test_home_assistant, assert_setup_component
from tests.components.group import common
class TestComponentsGroup(unittest.TestCase):
@@ -385,13 +386,13 @@ class TestComponentsGroup(unittest.TestCase):
group_entity_id = group.ENTITY_ID_FORMAT.format('test_group')
# Hide the group
group.set_visibility(self.hass, group_entity_id, False)
common.set_visibility(self.hass, group_entity_id, False)
self.hass.block_till_done()
group_state = self.hass.states.get(group_entity_id)
self.assertTrue(group_state.attributes.get(ATTR_HIDDEN))
# Show it again
group.set_visibility(self.hass, group_entity_id, True)
common.set_visibility(self.hass, group_entity_id, True)
self.hass.block_till_done()
group_state = self.hass.states.get(group_entity_id)
self.assertIsNone(group_state.attributes.get(ATTR_HIDDEN))
@@ -0,0 +1,23 @@
"""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.image_processing import DOMAIN, SERVICE_SCAN
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import callback
from homeassistant.loader import bind_hass
@bind_hass
def scan(hass, entity_id=None):
"""Force process of all cameras or given entity."""
hass.add_job(async_scan, hass, entity_id)
@callback
@bind_hass
def async_scan(hass, entity_id=None):
"""Force process of all cameras or given entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_SCAN, data))
@@ -10,6 +10,7 @@ import homeassistant.components.image_processing as ip
from tests.common import (
get_test_home_assistant, get_test_instance_port, assert_setup_component)
from tests.components.image_processing import common
class TestSetupImageProcessing:
@@ -85,7 +86,7 @@ class TestImageProcessing:
"""Grab an image from camera entity."""
self.hass.start()
ip.scan(self.hass, entity_id='image_processing.test')
common.scan(self.hass, entity_id='image_processing.test')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.test')
@@ -100,7 +101,7 @@ class TestImageProcessing:
"""Try to get image without exists camera."""
self.hass.states.remove('camera.demo_camera')
ip.scan(self.hass, entity_id='image_processing.test')
common.scan(self.hass, entity_id='image_processing.test')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.test')
@@ -152,7 +153,7 @@ class TestImageProcessingAlpr:
"""Set up and scan a picture and test plates from event."""
aioclient_mock.get(self.url, content=b'image')
ip.scan(self.hass, entity_id='image_processing.demo_alpr')
common.scan(self.hass, entity_id='image_processing.demo_alpr')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.demo_alpr')
@@ -171,8 +172,8 @@ class TestImageProcessingAlpr:
"""Set up and scan a picture and test plates from event."""
aioclient_mock.get(self.url, content=b'image')
ip.scan(self.hass, entity_id='image_processing.demo_alpr')
ip.scan(self.hass, entity_id='image_processing.demo_alpr')
common.scan(self.hass, entity_id='image_processing.demo_alpr')
common.scan(self.hass, entity_id='image_processing.demo_alpr')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.demo_alpr')
@@ -195,7 +196,7 @@ class TestImageProcessingAlpr:
"""Set up and scan a picture and test plates from event."""
aioclient_mock.get(self.url, content=b'image')
ip.scan(self.hass, entity_id='image_processing.demo_alpr')
common.scan(self.hass, entity_id='image_processing.demo_alpr')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.demo_alpr')
@@ -254,7 +255,7 @@ class TestImageProcessingFace:
"""Set up and scan a picture and test faces from event."""
aioclient_mock.get(self.url, content=b'image')
ip.scan(self.hass, entity_id='image_processing.demo_face')
common.scan(self.hass, entity_id='image_processing.demo_face')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.demo_face')
@@ -279,7 +280,7 @@ class TestImageProcessingFace:
"""Set up and scan a picture and test faces from event."""
aioclient_mock.get(self.url, content=b'image')
ip.scan(self.hass, entity_id='image_processing.demo_face')
common.scan(self.hass, entity_id='image_processing.demo_face')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.demo_face')
@@ -9,6 +9,7 @@ import homeassistant.components.microsoft_face as mf
from tests.common import (
get_test_home_assistant, assert_setup_component, load_fixture, mock_coro)
from tests.components.image_processing import common
class TestMicrosoftFaceDetectSetup:
@@ -146,7 +147,7 @@ class TestMicrosoftFaceDetect:
params={'returnFaceAttributes': "age,gender"}
)
ip.scan(self.hass, entity_id='image_processing.test_local')
common.scan(self.hass, entity_id='image_processing.test_local')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.test_local')
@@ -9,6 +9,7 @@ import homeassistant.components.microsoft_face as mf
from tests.common import (
get_test_home_assistant, assert_setup_component, load_fixture, mock_coro)
from tests.components.image_processing import common
class TestMicrosoftFaceIdentifySetup:
@@ -150,7 +151,7 @@ class TestMicrosoftFaceIdentify:
text=load_fixture('microsoft_face_identify.json')
)
ip.scan(self.hass, entity_id='image_processing.test_local')
common.scan(self.hass, entity_id='image_processing.test_local')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.test_local')
@@ -10,6 +10,7 @@ from homeassistant.components.image_processing.openalpr_cloud import (
from tests.common import (
get_test_home_assistant, assert_setup_component, load_fixture, mock_coro)
from tests.components.image_processing import common
class TestOpenAlprCloudSetup:
@@ -160,7 +161,7 @@ class TestOpenAlprCloud:
with patch('homeassistant.components.camera.async_get_image',
return_value=mock_coro(
camera.Image('image/jpeg', b'image'))):
ip.scan(self.hass, entity_id='image_processing.test_local')
common.scan(self.hass, entity_id='image_processing.test_local')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.test_local')
@@ -188,7 +189,7 @@ class TestOpenAlprCloud:
with patch('homeassistant.components.camera.async_get_image',
return_value=mock_coro(
camera.Image('image/jpeg', b'image'))):
ip.scan(self.hass, entity_id='image_processing.test_local')
common.scan(self.hass, entity_id='image_processing.test_local')
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
@@ -204,7 +205,7 @@ class TestOpenAlprCloud:
with patch('homeassistant.components.camera.async_get_image',
return_value=mock_coro(
camera.Image('image/jpeg', b'image'))):
ip.scan(self.hass, entity_id='image_processing.test_local')
common.scan(self.hass, entity_id='image_processing.test_local')
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
@@ -9,6 +9,7 @@ import homeassistant.components.image_processing as ip
from tests.common import (
get_test_home_assistant, assert_setup_component, load_fixture)
from tests.components.image_processing import common
@asyncio.coroutine
@@ -146,7 +147,7 @@ class TestOpenAlprLocal:
"""Set up and scan a picture and test plates from event."""
aioclient_mock.get(self.url, content=b'image')
ip.scan(self.hass, entity_id='image_processing.test_local')
common.scan(self.hass, entity_id='image_processing.test_local')
self.hass.block_till_done()
state = self.hass.states.get('image_processing.test_local')
+16 -2
View File
@@ -4,6 +4,7 @@ from datetime import timedelta
import pytest
from homeassistant.loader import bind_hass
from homeassistant.setup import async_setup_component
from homeassistant.components import duckdns
from homeassistant.util.dt import utcnow
@@ -14,6 +15,19 @@ DOMAIN = 'bla'
TOKEN = 'abcdefgh'
@bind_hass
@asyncio.coroutine
def async_set_txt(hass, txt):
"""Set the txt record. Pass in None to remove it.
This is a legacy helper method. Do not use it for new tests.
"""
yield from hass.services.async_call(
duckdns.DOMAIN, duckdns.SERVICE_SET_TXT, {
duckdns.ATTR_TXT: txt
}, blocking=True)
@pytest.fixture
def setup_duckdns(hass, aioclient_mock):
"""Fixture that sets up DuckDNS."""
@@ -84,7 +98,7 @@ def test_service_set_txt(hass, aioclient_mock, setup_duckdns):
}, text='OK')
assert aioclient_mock.call_count == 0
yield from hass.components.duckdns.async_set_txt('some-txt')
yield from async_set_txt(hass, 'some-txt')
assert aioclient_mock.call_count == 1
@@ -102,5 +116,5 @@ def test_service_clear_txt(hass, aioclient_mock, setup_duckdns):
}, text='OK')
assert aioclient_mock.call_count == 0
yield from hass.components.duckdns.async_set_txt(None)
yield from async_set_txt(hass, None)
assert aioclient_mock.call_count == 1
+31 -2
View File
@@ -7,9 +7,11 @@ import logging
from homeassistant.core import CoreState, State, Context
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.components.input_boolean import (
DOMAIN, is_on, toggle, turn_off, turn_on, CONF_INITIAL)
is_on, CONF_INITIAL, DOMAIN)
from homeassistant.const import (
STATE_ON, STATE_OFF, ATTR_ICON, ATTR_FRIENDLY_NAME)
STATE_ON, STATE_OFF, ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, ATTR_ICON,
SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON)
from homeassistant.loader import bind_hass
from tests.common import (
get_test_home_assistant, mock_component, mock_restore_cache)
@@ -17,6 +19,33 @@ from tests.common import (
_LOGGER = logging.getLogger(__name__)
@bind_hass
def toggle(hass, entity_id):
"""Set input_boolean to False.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_TOGGLE, {ATTR_ENTITY_ID: entity_id})
@bind_hass
def turn_on(hass, entity_id):
"""Set input_boolean to True.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id})
@bind_hass
def turn_off(hass, entity_id):
"""Set input_boolean to False.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id})
class TestInputBoolean(unittest.TestCase):
"""Test the input boolean module."""
+39 -2
View File
@@ -4,13 +4,50 @@ import asyncio
import unittest
from homeassistant.core import CoreState, State, Context
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.components.input_number import (
DOMAIN, set_value, increment, decrement)
ATTR_VALUE, DOMAIN, SERVICE_DECREMENT, SERVICE_INCREMENT,
SERVICE_SET_VALUE)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.loader import bind_hass
from homeassistant.setup import setup_component, async_setup_component
from tests.common import get_test_home_assistant, mock_restore_cache
@bind_hass
def set_value(hass, entity_id, value):
"""Set input_number to value.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_SET_VALUE, {
ATTR_ENTITY_ID: entity_id,
ATTR_VALUE: value,
})
@bind_hass
def increment(hass, entity_id):
"""Increment value of entity.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_INCREMENT, {
ATTR_ENTITY_ID: entity_id
})
@bind_hass
def decrement(hass, entity_id):
"""Decrement value of entity.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_DECREMENT, {
ATTR_ENTITY_ID: entity_id
})
class TestInputNumber(unittest.TestCase):
"""Test the input number component."""
+54 -7
View File
@@ -3,15 +3,62 @@
import asyncio
import unittest
from tests.common import get_test_home_assistant, mock_restore_cache
from homeassistant.loader import bind_hass
from homeassistant.components.input_select import (
ATTR_OPTION, ATTR_OPTIONS, DOMAIN, SERVICE_SET_OPTIONS,
SERVICE_SELECT_NEXT, SERVICE_SELECT_OPTION, SERVICE_SELECT_PREVIOUS)
from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, ATTR_ICON)
from homeassistant.core import State, Context
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.components.input_select import (
ATTR_OPTIONS, DOMAIN, SERVICE_SET_OPTIONS,
select_option, select_next, select_previous)
from homeassistant.const import (
ATTR_ICON, ATTR_FRIENDLY_NAME)
from tests.common import get_test_home_assistant, mock_restore_cache
@bind_hass
def select_option(hass, entity_id, option):
"""Set value of input_select.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_SELECT_OPTION, {
ATTR_ENTITY_ID: entity_id,
ATTR_OPTION: option,
})
@bind_hass
def select_next(hass, entity_id):
"""Set next value of input_select.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_SELECT_NEXT, {
ATTR_ENTITY_ID: entity_id,
})
@bind_hass
def select_previous(hass, entity_id):
"""Set previous value of input_select.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_SELECT_PREVIOUS, {
ATTR_ENTITY_ID: entity_id,
})
@bind_hass
def set_options(hass, entity_id, options):
"""Set options of input_select.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_SET_OPTIONS, {
ATTR_ENTITY_ID: entity_id,
ATTR_OPTIONS: options,
})
class TestInputSelect(unittest.TestCase):
+16 -1
View File
@@ -3,13 +3,28 @@
import asyncio
import unittest
from homeassistant.components.input_text import (
ATTR_VALUE, DOMAIN, SERVICE_SET_VALUE)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import CoreState, State, Context
from homeassistant.loader import bind_hass
from homeassistant.setup import setup_component, async_setup_component
from homeassistant.components.input_text import (DOMAIN, set_value)
from tests.common import get_test_home_assistant, mock_restore_cache
@bind_hass
def set_value(hass, entity_id, value):
"""Set input_text to value.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(DOMAIN, SERVICE_SET_VALUE, {
ATTR_ENTITY_ID: entity_id,
ATTR_VALUE: value,
})
class TestInputText(unittest.TestCase):
"""Test the input slider component."""