mirror of
https://github.com/home-assistant/core.git
synced 2025-08-12 23:10:05 +00:00
.github
docs
homeassistant
script
tests
auth
components
alarm_control_panel
alexa
auth
automation
binary_sensor
calendar
camera
cast
climate
cloud
config
counter
cover
deconz
device_tracker
dialogflow
emulated_hue
fan
frontend
geo_location
geofency
google_assistant
group
hangouts
hassio
homekit
homematicip_cloud
http
hue
ifttt
image_processing
ios
light
lock
lovelace
luftdaten
mailbox
mailgun
media_player
mqtt
nest
notify
onboarding
openuv
persistent_notification
point
rainmachine
recorder
remote
scene
sensor
simplisafe
smhi
sonos
switch
timer
tradfri
tts
twilio
unifi
upnp
vacuum
water_heater
weather
websocket_api
zone
zwave
__init__.py
conftest.py
test_alert.py
test_api.py
test_canary.py
test_configurator.py
test_conversation.py
test_datadog.py
test_demo.py
test_device_sun_light_trigger.py
test_discovery.py
test_duckdns.py
test_dyson.py
test_feedreader.py
test_ffmpeg.py
test_folder_watcher.py
test_freedns.py
test_google.py
test_google_domains.py
test_graphite.py
test_history.py
test_history_graph.py
test_huawei_lte.py
test_influxdb.py
test_init.py
test_input_boolean.py
test_input_datetime.py
test_input_number.py
test_input_select.py
test_input_text.py
test_intent_script.py
test_introduction.py
test_kira.py
test_litejet.py
test_logbook.py
test_logentries.py
test_logger.py
test_melissa.py
test_microsoft_face.py
test_mqtt_eventstream.py
test_mqtt_statestream.py
test_namecheapdns.py
test_no_ip.py
test_nuheat.py
test_panel_custom.py
test_panel_iframe.py
test_pilight.py
test_plant.py
test_prometheus.py
test_proximity.py
test_python_script.py
test_qwikswitch.py
test_remember_the_milk.py
test_rest_command.py
test_rflink.py
test_rfxtrx.py
test_ring.py
test_rss_feed_template.py
test_script.py
test_shell_command.py
test_shopping_list.py
test_sleepiq.py
test_snips.py
test_spaceapi.py
test_spc.py
test_splunk.py
test_statsd.py
test_sun.py
test_system_log.py
test_updater.py
test_vultr.py
test_wake_on_lan.py
test_webhook.py
test_weblink.py
fixtures
helpers
mock
resources
scripts
test_util
testing_config
util
__init__.py
common.py
conftest.py
test_bootstrap.py
test_config.py
test_config_entries.py
test_core.py
test_data_entry_flow.py
test_loader.py
test_main.py
test_requirements.py
test_setup.py
virtualization
.coveragerc
.dockerignore
.gitattributes
.gitignore
.hound.yml
.ignore
.isort.cfg
.readthedocs.yml
.travis.yml
CLA.md
CODEOWNERS
CODE_OF_CONDUCT.md
CONTRIBUTING.md
Dockerfile
LICENSE.md
MANIFEST.in
README.rst
mypy.ini
pylintrc
requirements_all.txt
requirements_docs.txt
requirements_test.txt
requirements_test_all.txt
setup.cfg
setup.py
tox.ini

* Convert core tests
* Convert component tests to use pytest assert
* Lint 🤷♂️
* Fix test
* Fix 3 typos in docs
257 lines
7.4 KiB
Python
257 lines
7.4 KiB
Python
"""The tests for the Script component."""
|
|
# pylint: disable=protected-access
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from homeassistant.components import script
|
|
from homeassistant.components.script import DOMAIN
|
|
from homeassistant.const import (
|
|
ATTR_ENTITY_ID, SERVICE_RELOAD, SERVICE_TOGGLE, SERVICE_TURN_OFF)
|
|
from homeassistant.core import Context, callback, split_entity_id
|
|
from homeassistant.loader import bind_hass
|
|
from homeassistant.setup import setup_component
|
|
|
|
from tests.common import get_test_home_assistant
|
|
|
|
|
|
ENTITY_ID = 'script.test'
|
|
|
|
|
|
@bind_hass
|
|
def turn_on(hass, entity_id, variables=None, context=None):
|
|
"""Turn script on.
|
|
|
|
This is a legacy helper method. Do not use it for new tests.
|
|
"""
|
|
_, object_id = split_entity_id(entity_id)
|
|
|
|
hass.services.call(DOMAIN, object_id, variables, context=context)
|
|
|
|
|
|
@bind_hass
|
|
def turn_off(hass, entity_id):
|
|
"""Turn script on.
|
|
|
|
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})
|
|
|
|
|
|
@bind_hass
|
|
def toggle(hass, entity_id):
|
|
"""Toggle the script.
|
|
|
|
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 reload(hass):
|
|
"""Reload script component.
|
|
|
|
This is a legacy helper method. Do not use it for new tests.
|
|
"""
|
|
hass.services.call(DOMAIN, SERVICE_RELOAD)
|
|
|
|
|
|
class TestScriptComponent(unittest.TestCase):
|
|
"""Test the Script component."""
|
|
|
|
# pylint: disable=invalid-name
|
|
def setUp(self):
|
|
"""Set up things to be run when tests are started."""
|
|
self.hass = get_test_home_assistant()
|
|
|
|
# pylint: disable=invalid-name
|
|
def tearDown(self):
|
|
"""Stop down everything that was started."""
|
|
self.hass.stop()
|
|
|
|
def test_setup_with_invalid_configs(self):
|
|
"""Test setup with invalid configs."""
|
|
for value in (
|
|
{'test': {}},
|
|
{
|
|
'test hello world': {
|
|
'sequence': [{'event': 'bla'}]
|
|
}
|
|
},
|
|
{
|
|
'test': {
|
|
'sequence': {
|
|
'event': 'test_event',
|
|
'service': 'homeassistant.turn_on',
|
|
}
|
|
}
|
|
},
|
|
):
|
|
assert not setup_component(self.hass, 'script', {
|
|
'script': value
|
|
}), 'Script loaded with wrong config {}'.format(value)
|
|
|
|
assert 0 == len(self.hass.states.entity_ids('script'))
|
|
|
|
def test_turn_on_service(self):
|
|
"""Verify that the turn_on service."""
|
|
event = 'test_event'
|
|
events = []
|
|
|
|
@callback
|
|
def record_event(event):
|
|
"""Add recorded event to set."""
|
|
events.append(event)
|
|
|
|
self.hass.bus.listen(event, record_event)
|
|
|
|
assert setup_component(self.hass, 'script', {
|
|
'script': {
|
|
'test': {
|
|
'sequence': [{
|
|
'delay': {
|
|
'seconds': 5
|
|
}
|
|
}, {
|
|
'event': event,
|
|
}]
|
|
}
|
|
}
|
|
})
|
|
|
|
turn_on(self.hass, ENTITY_ID)
|
|
self.hass.block_till_done()
|
|
assert script.is_on(self.hass, ENTITY_ID)
|
|
assert 0 == len(events)
|
|
|
|
# Calling turn_on a second time should not advance the script
|
|
turn_on(self.hass, ENTITY_ID)
|
|
self.hass.block_till_done()
|
|
assert 0 == len(events)
|
|
|
|
turn_off(self.hass, ENTITY_ID)
|
|
self.hass.block_till_done()
|
|
assert not script.is_on(self.hass, ENTITY_ID)
|
|
assert 0 == len(events)
|
|
|
|
state = self.hass.states.get('group.all_scripts')
|
|
assert state is not None
|
|
assert state.attributes.get('entity_id') == (ENTITY_ID,)
|
|
|
|
def test_toggle_service(self):
|
|
"""Test the toggling of a service."""
|
|
event = 'test_event'
|
|
events = []
|
|
|
|
@callback
|
|
def record_event(event):
|
|
"""Add recorded event to set."""
|
|
events.append(event)
|
|
|
|
self.hass.bus.listen(event, record_event)
|
|
|
|
assert setup_component(self.hass, 'script', {
|
|
'script': {
|
|
'test': {
|
|
'sequence': [{
|
|
'delay': {
|
|
'seconds': 5
|
|
}
|
|
}, {
|
|
'event': event,
|
|
}]
|
|
}
|
|
}
|
|
})
|
|
|
|
toggle(self.hass, ENTITY_ID)
|
|
self.hass.block_till_done()
|
|
assert script.is_on(self.hass, ENTITY_ID)
|
|
assert 0 == len(events)
|
|
|
|
toggle(self.hass, ENTITY_ID)
|
|
self.hass.block_till_done()
|
|
assert not script.is_on(self.hass, ENTITY_ID)
|
|
assert 0 == len(events)
|
|
|
|
def test_passing_variables(self):
|
|
"""Test different ways of passing in variables."""
|
|
calls = []
|
|
context = Context()
|
|
|
|
@callback
|
|
def record_call(service):
|
|
"""Add recorded event to set."""
|
|
calls.append(service)
|
|
|
|
self.hass.services.register('test', 'script', record_call)
|
|
|
|
assert setup_component(self.hass, 'script', {
|
|
'script': {
|
|
'test': {
|
|
'sequence': {
|
|
'service': 'test.script',
|
|
'data_template': {
|
|
'hello': '{{ greeting }}',
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
turn_on(self.hass, ENTITY_ID, {
|
|
'greeting': 'world'
|
|
}, context=context)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
assert len(calls) == 1
|
|
assert calls[0].context is context
|
|
assert calls[0].data['hello'] == 'world'
|
|
|
|
self.hass.services.call('script', 'test', {
|
|
'greeting': 'universe',
|
|
}, context=context)
|
|
|
|
self.hass.block_till_done()
|
|
|
|
assert len(calls) == 2
|
|
assert calls[1].context is context
|
|
assert calls[1].data['hello'] == 'universe'
|
|
|
|
def test_reload_service(self):
|
|
"""Verify that the turn_on service."""
|
|
assert setup_component(self.hass, 'script', {
|
|
'script': {
|
|
'test': {
|
|
'sequence': [{
|
|
'delay': {
|
|
'seconds': 5
|
|
}
|
|
}]
|
|
}
|
|
}
|
|
})
|
|
|
|
assert self.hass.states.get(ENTITY_ID) is not None
|
|
assert self.hass.services.has_service(script.DOMAIN, 'test')
|
|
|
|
with patch('homeassistant.config.load_yaml_config_file', return_value={
|
|
'script': {
|
|
'test2': {
|
|
'sequence': [{
|
|
'delay': {
|
|
'seconds': 5
|
|
}
|
|
}]
|
|
}}}):
|
|
with patch('homeassistant.config.find_config_file',
|
|
return_value=''):
|
|
reload(self.hass)
|
|
self.hass.block_till_done()
|
|
|
|
assert self.hass.states.get(ENTITY_ID) is None
|
|
assert not self.hass.services.has_service(script.DOMAIN, 'test')
|
|
|
|
assert self.hass.states.get("script.test2") is not None
|
|
assert self.hass.services.has_service(script.DOMAIN, 'test2')
|