diff --git a/homeassistant/components/command_line/__init__.py b/homeassistant/components/command_line/__init__.py index 27b69e59ca4..0f217eb0ee1 100644 --- a/homeassistant/components/command_line/__init__.py +++ b/homeassistant/components/command_line/__init__.py @@ -102,6 +102,7 @@ COVER_SCHEMA = vol.Schema( vol.Optional(CONF_COMMAND_STATE): cv.string, vol.Optional(CONF_COMMAND_STOP, default="true"): cv.string, vol.Required(CONF_NAME): cv.string, + vol.Optional(CONF_ICON): cv.template, vol.Optional(CONF_VALUE_TEMPLATE): cv.template, vol.Optional(CONF_COMMAND_TIMEOUT, default=DEFAULT_TIMEOUT): cv.positive_int, vol.Optional(CONF_UNIQUE_ID): cv.string, diff --git a/homeassistant/components/command_line/cover.py b/homeassistant/components/command_line/cover.py index c27cd97b39a..85c0ab605c7 100644 --- a/homeassistant/components/command_line/cover.py +++ b/homeassistant/components/command_line/cover.py @@ -12,6 +12,7 @@ from homeassistant.const import ( CONF_COMMAND_OPEN, CONF_COMMAND_STATE, CONF_COMMAND_STOP, + CONF_ICON, CONF_NAME, CONF_SCAN_INTERVAL, CONF_UNIQUE_ID, @@ -54,6 +55,7 @@ async def async_setup_platform( trigger_entity_config = { CONF_UNIQUE_ID: device_config.get(CONF_UNIQUE_ID), CONF_NAME: Template(device_config.get(CONF_NAME, device_name), hass), + CONF_ICON: device_config.get(CONF_ICON), CONF_AVAILABILITY: device_config.get(CONF_AVAILABILITY), } diff --git a/tests/components/command_line/test_cover.py b/tests/components/command_line/test_cover.py index 8b98d8d1623..7ed48909d79 100644 --- a/tests/components/command_line/test_cover.py +++ b/tests/components/command_line/test_cover.py @@ -383,3 +383,48 @@ async def test_availability( entity_state = hass.states.get("cover.test") assert entity_state assert entity_state.state == STATE_UNAVAILABLE + + +async def test_icon_template(hass: HomeAssistant) -> None: + """Test with state value.""" + with tempfile.TemporaryDirectory() as tempdirname: + path = os.path.join(tempdirname, "cover_status_icon") + await setup.async_setup_component( + hass, + DOMAIN, + { + "command_line": [ + { + "cover": { + "command_state": f"cat {path}", + "command_open": f"echo 100 > {path}", + "command_close": f"echo 0 > {path}", + "command_stop": f"echo 0 > {path}", + "name": "Test", + "icon": "{% if this.state=='open' %} mdi:open {% else %} mdi:closed {% endif %}", + } + } + ] + }, + ) + await hass.async_block_till_done() + + await hass.services.async_call( + COVER_DOMAIN, + SERVICE_CLOSE_COVER, + {ATTR_ENTITY_ID: "cover.test"}, + blocking=True, + ) + entity_state = hass.states.get("cover.test") + assert entity_state + assert entity_state.attributes.get("icon") == "mdi:closed" + + await hass.services.async_call( + COVER_DOMAIN, + SERVICE_OPEN_COVER, + {ATTR_ENTITY_ID: "cover.test"}, + blocking=True, + ) + entity_state = hass.states.get("cover.test") + assert entity_state + assert entity_state.attributes.get("icon") == "mdi:open"