Remove service helper (8) (#17055)

* Updated keyboard

* Updated microsoft_face

* Updated ffmpeg

* Updated logger

* Updated components/__init__.py
This commit is contained in:
cdce8p 2018-10-02 10:23:37 +02:00 committed by Paulus Schoutsen
parent 13af61e103
commit 90f71261c5
9 changed files with 193 additions and 167 deletions

View file

@ -59,55 +59,6 @@ def is_on(hass, entity_id=None):
return False
def turn_on(hass, entity_id=None, **service_data):
"""Turn specified entity on if possible."""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TURN_ON, service_data)
def turn_off(hass, entity_id=None, **service_data):
"""Turn specified entity off."""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TURN_OFF, service_data)
def toggle(hass, entity_id=None, **service_data):
"""Toggle specified entity."""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TOGGLE, service_data)
def stop(hass):
"""Stop Home Assistant."""
hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP)
def restart(hass):
"""Stop Home Assistant."""
hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART)
def check_config(hass):
"""Check the config files."""
hass.services.call(ha.DOMAIN, SERVICE_CHECK_CONFIG)
def reload_core_config(hass):
"""Reload the core config."""
hass.services.call(ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG)
async def async_reload_core_config(hass):
"""Reload the core config."""
await hass.services.async_call(ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG)
async def async_setup(hass: ha.HomeAssistant, config: dict) -> Awaitable[bool]:
"""Set up general services related to Home Assistant."""
async def async_handle_turn_service(service):

View file

@ -1,8 +1,9 @@
"""Provide configuration end points for Customize."""
from homeassistant.components.config import EditKeyBasedConfigView
from homeassistant.components import async_reload_core_config
from homeassistant.components import SERVICE_RELOAD_CORE_CONFIG
from homeassistant.config import DATA_CUSTOMIZE
from homeassistant.core import DOMAIN
import homeassistant.helpers.config_validation as cv
@ -11,9 +12,13 @@ CONFIG_PATH = 'customize.yaml'
async def async_setup(hass):
"""Set up the Customize config API."""
async def hook(hass):
"""post_write_hook for Config View that reloads groups."""
await hass.services.async_call(DOMAIN, SERVICE_RELOAD_CORE_CONFIG)
hass.http.register_view(CustomizeConfigView(
'customize', 'config', CONFIG_PATH, cv.entity_id, dict,
post_write_hook=async_reload_core_config
post_write_hook=hook
))
return True

View file

@ -54,27 +54,6 @@ SERVICE_FFMPEG_SCHEMA = vol.Schema({
})
@callback
def async_start(hass, entity_id=None):
"""Start a FFmpeg process on entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_START, data))
@callback
def async_stop(hass, entity_id=None):
"""Stop a FFmpeg process on entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_STOP, data))
@callback
def async_restart(hass, entity_id=None):
"""Restart a FFmpeg process on entity."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_RESTART, data))
async def async_setup(hass, config):
"""Set up the FFmpeg component."""
conf = config.get(DOMAIN, {})

View file

@ -18,36 +18,6 @@ DOMAIN = 'keyboard'
TAP_KEY_SCHEMA = vol.Schema({})
def volume_up(hass):
"""Press the keyboard button for volume up."""
hass.services.call(DOMAIN, SERVICE_VOLUME_UP)
def volume_down(hass):
"""Press the keyboard button for volume down."""
hass.services.call(DOMAIN, SERVICE_VOLUME_DOWN)
def volume_mute(hass):
"""Press the keyboard button for muting volume."""
hass.services.call(DOMAIN, SERVICE_VOLUME_MUTE)
def media_play_pause(hass):
"""Press the keyboard button for play/pause."""
hass.services.call(DOMAIN, SERVICE_MEDIA_PLAY_PAUSE)
def media_next_track(hass):
"""Press the keyboard button for next track."""
hass.services.call(DOMAIN, SERVICE_MEDIA_NEXT_TRACK)
def media_prev_track(hass):
"""Press the keyboard button for prev track."""
hass.services.call(DOMAIN, SERVICE_MEDIA_PREVIOUS_TRACK)
def setup(hass, config):
"""Listen for keyboard events."""
# pylint: disable=import-error

View file

@ -47,11 +47,6 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA)
def set_level(hass, logs):
"""Set log level for components."""
hass.services.call(DOMAIN, SERVICE_SET_LEVEL, logs)
class HomeAssistantLogFilter(logging.Filter):
"""A log filter."""

View file

@ -69,43 +69,6 @@ SCHEMA_TRAIN_SERVICE = vol.Schema({
})
def create_group(hass, name):
"""Create a new person group."""
data = {ATTR_NAME: name}
hass.services.call(DOMAIN, SERVICE_CREATE_GROUP, data)
def delete_group(hass, name):
"""Delete a person group."""
data = {ATTR_NAME: name}
hass.services.call(DOMAIN, SERVICE_DELETE_GROUP, data)
def train_group(hass, group):
"""Train a person group."""
data = {ATTR_GROUP: group}
hass.services.call(DOMAIN, SERVICE_TRAIN_GROUP, data)
def create_person(hass, group, name):
"""Create a person in a group."""
data = {ATTR_GROUP: group, ATTR_NAME: name}
hass.services.call(DOMAIN, SERVICE_CREATE_PERSON, data)
def delete_person(hass, group, name):
"""Delete a person in a group."""
data = {ATTR_GROUP: group, ATTR_NAME: name}
hass.services.call(DOMAIN, SERVICE_DELETE_PERSON, data)
def face_person(hass, group, person, camera_entity):
"""Add a new face picture to a person."""
data = {ATTR_GROUP: group, ATTR_PERSON: person,
ATTR_CAMERA_ENTITY: camera_entity}
hass.services.call(DOMAIN, SERVICE_FACE_PERSON, data)
async def async_setup(hass, config):
"""Set up Microsoft Face."""
entities = {}

View file

@ -3,12 +3,46 @@ import asyncio
from unittest.mock import patch, MagicMock
import homeassistant.components.ffmpeg as ffmpeg
from homeassistant.components.ffmpeg import (
DOMAIN, SERVICE_RESTART, SERVICE_START, SERVICE_STOP)
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import callback
from homeassistant.setup import setup_component, async_setup_component
from tests.common import (
get_test_home_assistant, assert_setup_component, mock_coro)
@callback
def async_start(hass, entity_id=None):
"""Start a FFmpeg process on entity.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_START, data))
@callback
def async_stop(hass, entity_id=None):
"""Stop a FFmpeg process on entity.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_STOP, data))
@callback
def async_restart(hass, entity_id=None):
"""Restart a FFmpeg process on entity.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else {}
hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_RESTART, data))
class MockFFmpegDev(ffmpeg.FFmpegBase):
"""FFmpeg device mock."""
@ -106,7 +140,7 @@ def test_setup_component_test_service_start(hass):
ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass()
ffmpeg.async_start(hass)
async_start(hass)
yield from hass.async_block_till_done()
assert ffmpeg_dev.called_start
@ -122,7 +156,7 @@ def test_setup_component_test_service_stop(hass):
ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass()
ffmpeg.async_stop(hass)
async_stop(hass)
yield from hass.async_block_till_done()
assert ffmpeg_dev.called_stop
@ -138,7 +172,7 @@ def test_setup_component_test_service_restart(hass):
ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass()
ffmpeg.async_restart(hass)
async_restart(hass)
yield from hass.async_block_till_done()
assert ffmpeg_dev.called_stop
@ -155,7 +189,7 @@ def test_setup_component_test_service_start_with_entity(hass):
ffmpeg_dev = MockFFmpegDev(hass, False)
yield from ffmpeg_dev.async_added_to_hass()
ffmpeg.async_start(hass, 'test.ffmpeg_device')
async_start(hass, 'test.ffmpeg_device')
yield from hass.async_block_till_done()
assert ffmpeg_dev.called_start

View file

@ -8,8 +8,12 @@ import yaml
import homeassistant.core as ha
from homeassistant import config
from homeassistant.const import (
STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE)
ATTR_ENTITY_ID, STATE_ON, STATE_OFF, SERVICE_HOMEASSISTANT_RESTART,
SERVICE_HOMEASSISTANT_STOP, SERVICE_TURN_ON, SERVICE_TURN_OFF,
SERVICE_TOGGLE)
import homeassistant.components as comps
from homeassistant.components import (
SERVICE_CHECK_CONFIG, SERVICE_RELOAD_CORE_CONFIG)
import homeassistant.helpers.intent as intent
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity
@ -20,6 +24,71 @@ from tests.common import (
async_mock_service)
def turn_on(hass, entity_id=None, **service_data):
"""Turn specified entity on if possible.
This is a legacy helper method. Do not use it for new tests.
"""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TURN_ON, service_data)
def turn_off(hass, entity_id=None, **service_data):
"""Turn specified entity off.
This is a legacy helper method. Do not use it for new tests.
"""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TURN_OFF, service_data)
def toggle(hass, entity_id=None, **service_data):
"""Toggle specified entity.
This is a legacy helper method. Do not use it for new tests.
"""
if entity_id is not None:
service_data[ATTR_ENTITY_ID] = entity_id
hass.services.call(ha.DOMAIN, SERVICE_TOGGLE, service_data)
def stop(hass):
"""Stop Home Assistant.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_STOP)
def restart(hass):
"""Stop Home Assistant.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(ha.DOMAIN, SERVICE_HOMEASSISTANT_RESTART)
def check_config(hass):
"""Check the config files.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(ha.DOMAIN, SERVICE_CHECK_CONFIG)
def reload_core_config(hass):
"""Reload the core config.
This is a legacy helper method. Do not use it for new tests.
"""
hass.services.call(ha.DOMAIN, SERVICE_RELOAD_CORE_CONFIG)
class TestComponentsCore(unittest.TestCase):
"""Test homeassistant.components module."""
@ -49,28 +118,28 @@ class TestComponentsCore(unittest.TestCase):
def test_turn_on_without_entities(self):
"""Test turn_on method without entities."""
calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)
comps.turn_on(self.hass)
turn_on(self.hass)
self.hass.block_till_done()
self.assertEqual(0, len(calls))
def test_turn_on(self):
"""Test turn_on method."""
calls = mock_service(self.hass, 'light', SERVICE_TURN_ON)
comps.turn_on(self.hass, 'light.Ceiling')
turn_on(self.hass, 'light.Ceiling')
self.hass.block_till_done()
self.assertEqual(1, len(calls))
def test_turn_off(self):
"""Test turn_off method."""
calls = mock_service(self.hass, 'light', SERVICE_TURN_OFF)
comps.turn_off(self.hass, 'light.Bowl')
turn_off(self.hass, 'light.Bowl')
self.hass.block_till_done()
self.assertEqual(1, len(calls))
def test_toggle(self):
"""Test toggle method."""
calls = mock_service(self.hass, 'light', SERVICE_TOGGLE)
comps.toggle(self.hass, 'light.Bowl')
toggle(self.hass, 'light.Bowl')
self.hass.block_till_done()
self.assertEqual(1, len(calls))
@ -102,7 +171,7 @@ class TestComponentsCore(unittest.TestCase):
})
}
with patch_yaml_files(files, True):
comps.reload_core_config(self.hass)
reload_core_config(self.hass)
self.hass.block_till_done()
assert self.hass.config.latitude == 10
@ -125,7 +194,7 @@ class TestComponentsCore(unittest.TestCase):
config.YAML_CONFIG_FILE: yaml.dump(['invalid', 'config'])
}
with patch_yaml_files(files, True):
comps.reload_core_config(self.hass)
reload_core_config(self.hass)
self.hass.block_till_done()
assert mock_error.called
@ -135,7 +204,7 @@ class TestComponentsCore(unittest.TestCase):
return_value=mock_coro())
def test_stop_homeassistant(self, mock_stop):
"""Test stop service."""
comps.stop(self.hass)
stop(self.hass)
self.hass.block_till_done()
assert mock_stop.called
@ -145,7 +214,7 @@ class TestComponentsCore(unittest.TestCase):
return_value=mock_coro())
def test_restart_homeassistant(self, mock_check, mock_restart):
"""Test stop service."""
comps.restart(self.hass)
restart(self.hass)
self.hass.block_till_done()
assert mock_restart.called
assert mock_check.called
@ -156,7 +225,7 @@ class TestComponentsCore(unittest.TestCase):
side_effect=HomeAssistantError("Test error"))
def test_restart_homeassistant_wrong_conf(self, mock_check, mock_restart):
"""Test stop service."""
comps.restart(self.hass)
restart(self.hass)
self.hass.block_till_done()
assert mock_check.called
assert not mock_restart.called
@ -167,7 +236,7 @@ class TestComponentsCore(unittest.TestCase):
return_value=mock_coro())
def test_check_config(self, mock_check, mock_stop):
"""Test stop service."""
comps.check_config(self.hass)
check_config(self.hass)
self.hass.block_till_done()
assert mock_check.called
assert not mock_stop.called

View file

@ -3,12 +3,72 @@ import asyncio
from unittest.mock import patch
from homeassistant.components import camera, microsoft_face as mf
from homeassistant.components.microsoft_face import (
ATTR_CAMERA_ENTITY, ATTR_GROUP, ATTR_PERSON, DOMAIN, SERVICE_CREATE_GROUP,
SERVICE_CREATE_PERSON, SERVICE_DELETE_GROUP, SERVICE_DELETE_PERSON,
SERVICE_FACE_PERSON, SERVICE_TRAIN_GROUP)
from homeassistant.const import ATTR_NAME
from homeassistant.setup import setup_component
from tests.common import (
get_test_home_assistant, assert_setup_component, mock_coro, load_fixture)
def create_group(hass, name):
"""Create a new person group.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_NAME: name}
hass.services.call(DOMAIN, SERVICE_CREATE_GROUP, data)
def delete_group(hass, name):
"""Delete a person group.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_NAME: name}
hass.services.call(DOMAIN, SERVICE_DELETE_GROUP, data)
def train_group(hass, group):
"""Train a person group.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_GROUP: group}
hass.services.call(DOMAIN, SERVICE_TRAIN_GROUP, data)
def create_person(hass, group, name):
"""Create a person in a group.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_GROUP: group, ATTR_NAME: name}
hass.services.call(DOMAIN, SERVICE_CREATE_PERSON, data)
def delete_person(hass, group, name):
"""Delete a person in a group.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_GROUP: group, ATTR_NAME: name}
hass.services.call(DOMAIN, SERVICE_DELETE_PERSON, data)
def face_person(hass, group, person, camera_entity):
"""Add a new face picture to a person.
This is a legacy helper method. Do not use it for new tests.
"""
data = {ATTR_GROUP: group, ATTR_PERSON: person,
ATTR_CAMERA_ENTITY: camera_entity}
hass.services.call(DOMAIN, SERVICE_FACE_PERSON, data)
class TestMicrosoftFaceSetup:
"""Test the microsoft face component."""
@ -108,14 +168,14 @@ class TestMicrosoftFaceSetup:
with assert_setup_component(3, mf.DOMAIN):
setup_component(self.hass, mf.DOMAIN, self.config)
mf.create_group(self.hass, 'Service Group')
create_group(self.hass, 'Service Group')
self.hass.block_till_done()
entity = self.hass.states.get('microsoft_face.service_group')
assert entity is not None
assert len(aioclient_mock.mock_calls) == 1
mf.delete_group(self.hass, 'Service Group')
delete_group(self.hass, 'Service Group')
self.hass.block_till_done()
entity = self.hass.states.get('microsoft_face.service_group')
@ -153,7 +213,7 @@ class TestMicrosoftFaceSetup:
status=200, text="{}"
)
mf.create_person(self.hass, 'test group1', 'Hans')
create_person(self.hass, 'test group1', 'Hans')
self.hass.block_till_done()
entity_group1 = self.hass.states.get('microsoft_face.test_group1')
@ -163,7 +223,7 @@ class TestMicrosoftFaceSetup:
assert entity_group1.attributes['Hans'] == \
'25985303-c537-4467-b41d-bdb45cd95ca1'
mf.delete_person(self.hass, 'test group1', 'Hans')
delete_person(self.hass, 'test group1', 'Hans')
self.hass.block_till_done()
entity_group1 = self.hass.states.get('microsoft_face.test_group1')
@ -184,7 +244,7 @@ class TestMicrosoftFaceSetup:
status=200, text="{}"
)
mf.train_group(self.hass, 'Service Group')
train_group(self.hass, 'Service Group')
self.hass.block_till_done()
assert len(aioclient_mock.mock_calls) == 1
@ -219,7 +279,7 @@ class TestMicrosoftFaceSetup:
status=200, text="{}"
)
mf.face_person(
face_person(
self.hass, 'test_group2', 'David', 'camera.demo_camera')
self.hass.block_till_done()
@ -238,7 +298,7 @@ class TestMicrosoftFaceSetup:
with assert_setup_component(3, mf.DOMAIN):
setup_component(self.hass, mf.DOMAIN, self.config)
mf.create_group(self.hass, 'Service Group')
create_group(self.hass, 'Service Group')
self.hass.block_till_done()
entity = self.hass.states.get('microsoft_face.service_group')
@ -257,7 +317,7 @@ class TestMicrosoftFaceSetup:
with assert_setup_component(3, mf.DOMAIN):
setup_component(self.hass, mf.DOMAIN, self.config)
mf.create_group(self.hass, 'Service Group')
create_group(self.hass, 'Service Group')
self.hass.block_till_done()
entity = self.hass.states.get('microsoft_face.service_group')