Support this variable in template vacuum actions (#71800)

This commit is contained in:
Erik Montnemery 2022-05-13 18:32:19 +02:00 committed by GitHub
parent 9eca91afc9
commit 6f7a465347
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 141 additions and 43 deletions

View File

@ -204,42 +204,42 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
async def async_start(self):
"""Start or resume the cleaning task."""
await self._start_script.async_run(context=self._context)
await self.async_run_script(self._start_script, context=self._context)
async def async_pause(self):
"""Pause the cleaning task."""
if self._pause_script is None:
return
await self._pause_script.async_run(context=self._context)
await self.async_run_script(self._pause_script, context=self._context)
async def async_stop(self, **kwargs):
"""Stop the cleaning task."""
if self._stop_script is None:
return
await self._stop_script.async_run(context=self._context)
await self.async_run_script(self._stop_script, context=self._context)
async def async_return_to_base(self, **kwargs):
"""Set the vacuum cleaner to return to the dock."""
if self._return_to_base_script is None:
return
await self._return_to_base_script.async_run(context=self._context)
await self.async_run_script(self._return_to_base_script, context=self._context)
async def async_clean_spot(self, **kwargs):
"""Perform a spot clean-up."""
if self._clean_spot_script is None:
return
await self._clean_spot_script.async_run(context=self._context)
await self.async_run_script(self._clean_spot_script, context=self._context)
async def async_locate(self, **kwargs):
"""Locate the vacuum cleaner."""
if self._locate_script is None:
return
await self._locate_script.async_run(context=self._context)
await self.async_run_script(self._locate_script, context=self._context)
async def async_set_fan_speed(self, fan_speed, **kwargs):
"""Set fan speed."""
@ -248,8 +248,10 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
if fan_speed in self._attr_fan_speed_list:
self._attr_fan_speed = fan_speed
await self._set_fan_speed_script.async_run(
{ATTR_FAN_SPEED: fan_speed}, context=self._context
await self.async_run_script(
self._set_fan_speed_script,
run_variables={ATTR_FAN_SPEED: fan_speed},
context=self._context,
)
else:
_LOGGER.error(

View File

@ -342,7 +342,7 @@ async def test_unused_services(hass):
_verify(hass, STATE_UNKNOWN, None)
async def test_state_services(hass):
async def test_state_services(hass, calls):
"""Test state services."""
await _register_components(hass)
@ -353,6 +353,9 @@ async def test_state_services(hass):
# verify
assert hass.states.get(_STATE_INPUT_SELECT).state == STATE_CLEANING
_verify(hass, STATE_CLEANING, None)
assert len(calls) == 1
assert calls[-1].data["action"] == "start"
assert calls[-1].data["caller"] == _TEST_VACUUM
# Pause vacuum
await common.async_pause(hass, _TEST_VACUUM)
@ -361,6 +364,9 @@ async def test_state_services(hass):
# verify
assert hass.states.get(_STATE_INPUT_SELECT).state == STATE_PAUSED
_verify(hass, STATE_PAUSED, None)
assert len(calls) == 2
assert calls[-1].data["action"] == "pause"
assert calls[-1].data["caller"] == _TEST_VACUUM
# Stop vacuum
await common.async_stop(hass, _TEST_VACUUM)
@ -369,6 +375,9 @@ async def test_state_services(hass):
# verify
assert hass.states.get(_STATE_INPUT_SELECT).state == STATE_IDLE
_verify(hass, STATE_IDLE, None)
assert len(calls) == 3
assert calls[-1].data["action"] == "stop"
assert calls[-1].data["caller"] == _TEST_VACUUM
# Return vacuum to base
await common.async_return_to_base(hass, _TEST_VACUUM)
@ -377,9 +386,12 @@ async def test_state_services(hass):
# verify
assert hass.states.get(_STATE_INPUT_SELECT).state == STATE_RETURNING
_verify(hass, STATE_RETURNING, None)
assert len(calls) == 4
assert calls[-1].data["action"] == "return_to_base"
assert calls[-1].data["caller"] == _TEST_VACUUM
async def test_clean_spot_service(hass):
async def test_clean_spot_service(hass, calls):
"""Test clean spot service."""
await _register_components(hass)
@ -389,9 +401,12 @@ async def test_clean_spot_service(hass):
# verify
assert hass.states.get(_SPOT_CLEANING_INPUT_BOOLEAN).state == STATE_ON
assert len(calls) == 1
assert calls[-1].data["action"] == "clean_spot"
assert calls[-1].data["caller"] == _TEST_VACUUM
async def test_locate_service(hass):
async def test_locate_service(hass, calls):
"""Test locate service."""
await _register_components(hass)
@ -401,9 +416,12 @@ async def test_locate_service(hass):
# verify
assert hass.states.get(_LOCATING_INPUT_BOOLEAN).state == STATE_ON
assert len(calls) == 1
assert calls[-1].data["action"] == "locate"
assert calls[-1].data["caller"] == _TEST_VACUUM
async def test_set_fan_speed(hass):
async def test_set_fan_speed(hass, calls):
"""Test set valid fan speed."""
await _register_components(hass)
@ -413,6 +431,10 @@ async def test_set_fan_speed(hass):
# verify
assert hass.states.get(_FAN_SPEED_INPUT_SELECT).state == "high"
assert len(calls) == 1
assert calls[-1].data["action"] == "set_fan_speed"
assert calls[-1].data["caller"] == _TEST_VACUUM
assert calls[-1].data["option"] == "high"
# Set fan's speed to medium
await common.async_set_fan_speed(hass, "medium", _TEST_VACUUM)
@ -420,9 +442,13 @@ async def test_set_fan_speed(hass):
# verify
assert hass.states.get(_FAN_SPEED_INPUT_SELECT).state == "medium"
assert len(calls) == 2
assert calls[-1].data["action"] == "set_fan_speed"
assert calls[-1].data["caller"] == _TEST_VACUUM
assert calls[-1].data["option"] == "medium"
async def test_set_invalid_fan_speed(hass):
async def test_set_invalid_fan_speed(hass, calls):
"""Test set invalid fan speed when fan has valid speed."""
await _register_components(hass)
@ -522,37 +548,107 @@ async def _register_components(hass):
test_vacuum_config = {
"value_template": "{{ states('input_select.state') }}",
"fan_speed_template": "{{ states('input_select.fan_speed') }}",
"start": {
"start": [
{
"service": "input_select.select_option",
"data": {"entity_id": _STATE_INPUT_SELECT, "option": STATE_CLEANING},
"data": {
"entity_id": _STATE_INPUT_SELECT,
"option": STATE_CLEANING,
},
"pause": {
},
{
"service": "test.automation",
"data_template": {
"action": "start",
"caller": "{{ this.entity_id }}",
},
},
],
"pause": [
{
"service": "input_select.select_option",
"data": {"entity_id": _STATE_INPUT_SELECT, "option": STATE_PAUSED},
},
"stop": {
{
"service": "test.automation",
"data_template": {
"action": "pause",
"caller": "{{ this.entity_id }}",
},
},
],
"stop": [
{
"service": "input_select.select_option",
"data": {"entity_id": _STATE_INPUT_SELECT, "option": STATE_IDLE},
},
"return_to_base": {
"service": "input_select.select_option",
"data": {"entity_id": _STATE_INPUT_SELECT, "option": STATE_RETURNING},
{
"service": "test.automation",
"data_template": {
"action": "stop",
"caller": "{{ this.entity_id }}",
},
"clean_spot": {
},
],
"return_to_base": [
{
"service": "input_select.select_option",
"data": {
"entity_id": _STATE_INPUT_SELECT,
"option": STATE_RETURNING,
},
},
{
"service": "test.automation",
"data_template": {
"action": "return_to_base",
"caller": "{{ this.entity_id }}",
},
},
],
"clean_spot": [
{
"service": "input_boolean.turn_on",
"entity_id": _SPOT_CLEANING_INPUT_BOOLEAN,
},
"locate": {
{
"service": "test.automation",
"data_template": {
"action": "clean_spot",
"caller": "{{ this.entity_id }}",
},
},
],
"locate": [
{
"service": "input_boolean.turn_on",
"entity_id": _LOCATING_INPUT_BOOLEAN,
},
"set_fan_speed": {
{
"service": "test.automation",
"data_template": {
"action": "locate",
"caller": "{{ this.entity_id }}",
},
},
],
"set_fan_speed": [
{
"service": "input_select.select_option",
"data_template": {
"entity_id": _FAN_SPEED_INPUT_SELECT,
"option": "{{ fan_speed }}",
},
},
{
"service": "test.automation",
"data_template": {
"action": "set_fan_speed",
"caller": "{{ this.entity_id }}",
"option": "{{ fan_speed }}",
},
},
],
"fan_speeds": ["low", "medium", "high"],
"attribute_templates": {
"test_attribute": "It {{ states.sensor.test_state.state }}."