diff --git a/homeassistant/components/command_line/binary_sensor.py b/homeassistant/components/command_line/binary_sensor.py index 86916e86a26..cb78b7b9144 100644 --- a/homeassistant/components/command_line/binary_sensor.py +++ b/homeassistant/components/command_line/binary_sensor.py @@ -18,8 +18,9 @@ from homeassistant.const import ( CONF_VALUE_TEMPLATE, ) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.reload import setup_reload_service -from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT +from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, PLATFORMS from .sensor import CommandSensorData _LOGGER = logging.getLogger(__name__) @@ -46,6 +47,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Command line Binary Sensor.""" + + setup_reload_service(hass, DOMAIN, PLATFORMS) + name = config.get(CONF_NAME) command = config.get(CONF_COMMAND) payload_off = config.get(CONF_PAYLOAD_OFF) diff --git a/homeassistant/components/command_line/const.py b/homeassistant/components/command_line/const.py index 8c5bc0b2967..2ac6aab29a5 100644 --- a/homeassistant/components/command_line/const.py +++ b/homeassistant/components/command_line/const.py @@ -2,3 +2,5 @@ CONF_COMMAND_TIMEOUT = "command_timeout" DEFAULT_TIMEOUT = 15 +DOMAIN = "command_line" +PLATFORMS = ["binary_sensor", "cover", "sensor", "switch"] diff --git a/homeassistant/components/command_line/cover.py b/homeassistant/components/command_line/cover.py index 1fdcdf3b3e7..05d2b9634f2 100644 --- a/homeassistant/components/command_line/cover.py +++ b/homeassistant/components/command_line/cover.py @@ -14,9 +14,10 @@ from homeassistant.const import ( CONF_VALUE_TEMPLATE, ) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.reload import setup_reload_service from . import call_shell_with_timeout, check_output_or_log -from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT +from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) @@ -39,6 +40,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( def setup_platform(hass, config, add_entities, discovery_info=None): """Set up cover controlled by shell commands.""" + + setup_reload_service(hass, DOMAIN, PLATFORMS) + devices = config.get(CONF_COVERS, {}) covers = [] diff --git a/homeassistant/components/command_line/sensor.py b/homeassistant/components/command_line/sensor.py index 778806099aa..35f7c5a4811 100644 --- a/homeassistant/components/command_line/sensor.py +++ b/homeassistant/components/command_line/sensor.py @@ -18,9 +18,10 @@ from homeassistant.exceptions import TemplateError from homeassistant.helpers import template import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity import Entity +from homeassistant.helpers.reload import setup_reload_service from . import check_output_or_log -from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT +from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) @@ -44,6 +45,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Command Sensor.""" + + setup_reload_service(hass, DOMAIN, PLATFORMS) + name = config.get(CONF_NAME) command = config.get(CONF_COMMAND) unit = config.get(CONF_UNIT_OF_MEASUREMENT) diff --git a/homeassistant/components/command_line/services.yaml b/homeassistant/components/command_line/services.yaml new file mode 100644 index 00000000000..8876e8dc925 --- /dev/null +++ b/homeassistant/components/command_line/services.yaml @@ -0,0 +1,2 @@ +reload: + description: Reload all command_line entities. diff --git a/homeassistant/components/command_line/switch.py b/homeassistant/components/command_line/switch.py index 804e3c6a4d5..ce46cd4f2cd 100644 --- a/homeassistant/components/command_line/switch.py +++ b/homeassistant/components/command_line/switch.py @@ -17,9 +17,10 @@ from homeassistant.const import ( CONF_VALUE_TEMPLATE, ) import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.reload import setup_reload_service from . import call_shell_with_timeout, check_output_or_log -from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT +from .const import CONF_COMMAND_TIMEOUT, DEFAULT_TIMEOUT, DOMAIN, PLATFORMS _LOGGER = logging.getLogger(__name__) @@ -41,6 +42,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( def setup_platform(hass, config, add_entities, discovery_info=None): """Find and return switches controlled by shell commands.""" + + setup_reload_service(hass, DOMAIN, PLATFORMS) + devices = config.get(CONF_SWITCHES, {}) switches = [] diff --git a/tests/components/command_line/test_cover.py b/tests/components/command_line/test_cover.py index cc91e521d68..4707296a426 100644 --- a/tests/components/command_line/test_cover.py +++ b/tests/components/command_line/test_cover.py @@ -1,16 +1,20 @@ """The tests the cover command line platform.""" import os +from os import path import tempfile from unittest import mock +from asynctest.mock import patch import pytest +from homeassistant import config as hass_config import homeassistant.components.command_line.cover as cmd_rs from homeassistant.components.cover import DOMAIN from homeassistant.const import ( ATTR_ENTITY_ID, SERVICE_CLOSE_COVER, SERVICE_OPEN_COVER, + SERVICE_RELOAD, SERVICE_STOP_COVER, ) from homeassistant.setup import async_setup_component @@ -87,3 +91,39 @@ async def test_state_value(hass): DOMAIN, SERVICE_STOP_COVER, {ATTR_ENTITY_ID: "cover.test"}, blocking=True ) assert "closed" == hass.states.get("cover.test").state + + +async def test_reload(hass): + """Verify we can reload command_line covers.""" + + test_cover = { + "command_state": "echo open", + "value_template": "{{ value }}", + } + await async_setup_component( + hass, + DOMAIN, + {"cover": {"platform": "command_line", "covers": {"test": test_cover}}}, + ) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 1 + assert hass.states.get("cover.test").state + + yaml_path = path.join( + _get_fixtures_base_path(), "fixtures", "command_line/configuration.yaml", + ) + with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path): + await hass.services.async_call( + "command_line", SERVICE_RELOAD, {}, blocking=True, + ) + await hass.async_block_till_done() + + assert len(hass.states.async_all()) == 1 + + assert hass.states.get("cover.test") is None + assert hass.states.get("cover.from_yaml") + + +def _get_fixtures_base_path(): + return path.dirname(path.dirname(path.dirname(__file__))) diff --git a/tests/fixtures/command_line/configuration.yaml b/tests/fixtures/command_line/configuration.yaml new file mode 100644 index 00000000000..f210b640338 --- /dev/null +++ b/tests/fixtures/command_line/configuration.yaml @@ -0,0 +1,6 @@ +cover: + - platform: command_line + covers: + from_yaml: + command_state: "echo closed" + value_template: "{{ value }}"