mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 08:47:10 +00:00
More service helper tests
This commit is contained in:
parent
12b5caed70
commit
73cdf00512
@ -13,11 +13,16 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
def call_from_config(hass, config, blocking=False):
|
def call_from_config(hass, config, blocking=False):
|
||||||
"""Call a service based on a config hash."""
|
"""Call a service based on a config hash."""
|
||||||
if CONF_SERVICE not in config:
|
if not isinstance(config, dict) or CONF_SERVICE not in config:
|
||||||
_LOGGER.error('Missing key %s: %s', CONF_SERVICE, config)
|
_LOGGER.error('Missing key %s: %s', CONF_SERVICE, config)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
domain, service = split_entity_id(config[CONF_SERVICE])
|
domain, service = split_entity_id(config[CONF_SERVICE])
|
||||||
|
except ValueError:
|
||||||
|
_LOGGER.error('Invalid service specified: %s', config[CONF_SERVICE])
|
||||||
|
return
|
||||||
|
|
||||||
service_data = config.get(CONF_SERVICE_DATA)
|
service_data = config.get(CONF_SERVICE_DATA)
|
||||||
|
|
||||||
if service_data is None:
|
if service_data is None:
|
||||||
|
@ -4,11 +4,9 @@ tests.helpers.test_service
|
|||||||
|
|
||||||
Test service helpers.
|
Test service helpers.
|
||||||
"""
|
"""
|
||||||
from datetime import timedelta
|
|
||||||
import unittest
|
import unittest
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
import homeassistant.core as ha
|
|
||||||
from homeassistant.const import SERVICE_TURN_ON
|
from homeassistant.const import SERVICE_TURN_ON
|
||||||
from homeassistant.helpers import service
|
from homeassistant.helpers import service
|
||||||
|
|
||||||
@ -37,3 +35,34 @@ class TestServiceHelpers(unittest.TestCase):
|
|||||||
self.hass.pool.block_till_done()
|
self.hass.pool.block_till_done()
|
||||||
self.assertEqual(['hello.world', 'sensor.beer'],
|
self.assertEqual(['hello.world', 'sensor.beer'],
|
||||||
self.calls[-1].data.get('entity_id'))
|
self.calls[-1].data.get('entity_id'))
|
||||||
|
|
||||||
|
def test_not_mutate_input(self):
|
||||||
|
orig = {
|
||||||
|
'service': 'test_domain.test_service',
|
||||||
|
'entity_id': 'hello.world, sensor.beer',
|
||||||
|
'data': {
|
||||||
|
'hello': 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
service.call_from_config(self.hass, orig)
|
||||||
|
self.hass.pool.block_till_done()
|
||||||
|
self.assertEqual({
|
||||||
|
'service': 'test_domain.test_service',
|
||||||
|
'entity_id': 'hello.world, sensor.beer',
|
||||||
|
'data': {
|
||||||
|
'hello': 1,
|
||||||
|
},
|
||||||
|
}, orig)
|
||||||
|
|
||||||
|
@patch('homeassistant.helpers.service._LOGGER.error')
|
||||||
|
def test_fail_silently_if_no_service(self, mock_log):
|
||||||
|
service.call_from_config(self.hass, None)
|
||||||
|
self.assertEqual(1, mock_log.call_count)
|
||||||
|
|
||||||
|
service.call_from_config(self.hass, {})
|
||||||
|
self.assertEqual(2, mock_log.call_count)
|
||||||
|
|
||||||
|
service.call_from_config(self.hass, {
|
||||||
|
'service': 'invalid'
|
||||||
|
})
|
||||||
|
self.assertEqual(3, mock_log.call_count)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user