Skip to content

Commit

Permalink
Add context to scripts run by template entities (home-assistant#17329)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Oct 15, 2018
1 parent 0bf10b0 commit ac79ff9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
22 changes: 13 additions & 9 deletions homeassistant/components/cover/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,55 +278,59 @@ def should_poll(self):
async def async_open_cover(self, **kwargs):
"""Move the cover up."""
if self._open_script:
await self._open_script.async_run()
await self._open_script.async_run(context=self._context)
elif self._position_script:
await self._position_script.async_run({"position": 100})
await self._position_script.async_run(
{"position": 100}, context=self._context)
if self._optimistic:
self._position = 100
self.async_schedule_update_ha_state()

async def async_close_cover(self, **kwargs):
"""Move the cover down."""
if self._close_script:
await self._close_script.async_run()
await self._close_script.async_run(context=self._context)
elif self._position_script:
await self._position_script.async_run({"position": 0})
await self._position_script.async_run(
{"position": 0}, context=self._context)
if self._optimistic:
self._position = 0
self.async_schedule_update_ha_state()

async def async_stop_cover(self, **kwargs):
"""Fire the stop action."""
if self._stop_script:
await self._stop_script.async_run()
await self._stop_script.async_run(context=self._context)

async def async_set_cover_position(self, **kwargs):
"""Set cover position."""
self._position = kwargs[ATTR_POSITION]
await self._position_script.async_run(
{"position": self._position})
{"position": self._position}, context=self._context)
if self._optimistic:
self.async_schedule_update_ha_state()

async def async_open_cover_tilt(self, **kwargs):
"""Tilt the cover open."""
self._tilt_value = 100
await self._tilt_script.async_run({"tilt": self._tilt_value})
await self._tilt_script.async_run(
{"tilt": self._tilt_value}, context=self._context)
if self._tilt_optimistic:
self.async_schedule_update_ha_state()

async def async_close_cover_tilt(self, **kwargs):
"""Tilt the cover closed."""
self._tilt_value = 0
await self._tilt_script.async_run(
{"tilt": self._tilt_value})
{"tilt": self._tilt_value}, context=self._context)
if self._tilt_optimistic:
self.async_schedule_update_ha_state()

async def async_set_cover_tilt_position(self, **kwargs):
"""Move the cover tilt to a specific position."""
self._tilt_value = kwargs[ATTR_TILT_POSITION]
await self._tilt_script.async_run({"tilt": self._tilt_value})
await self._tilt_script.async_run(
{"tilt": self._tilt_value}, context=self._context)
if self._tilt_optimistic:
self.async_schedule_update_ha_state()

Expand Down
11 changes: 6 additions & 5 deletions homeassistant/components/fan/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ def should_poll(self):
# pylint: disable=arguments-differ
async def async_turn_on(self, speed: str = None) -> None:
"""Turn on the fan."""
await self._on_script.async_run()
await self._on_script.async_run(context=self._context)
self._state = STATE_ON

if speed is not None:
Expand All @@ -233,7 +233,7 @@ async def async_turn_on(self, speed: str = None) -> None:
# pylint: disable=arguments-differ
async def async_turn_off(self) -> None:
"""Turn off the fan."""
await self._off_script.async_run()
await self._off_script.async_run(context=self._context)
self._state = STATE_OFF

async def async_set_speed(self, speed: str) -> None:
Expand All @@ -243,7 +243,8 @@ async def async_set_speed(self, speed: str) -> None:

if speed in self._speed_list:
self._speed = speed
await self._set_speed_script.async_run({ATTR_SPEED: speed})
await self._set_speed_script.async_run(
{ATTR_SPEED: speed}, context=self._context)
else:
_LOGGER.error(
'Received invalid speed: %s. Expected: %s.',
Expand All @@ -257,7 +258,7 @@ async def async_oscillate(self, oscillating: bool) -> None:
if oscillating in _VALID_OSC:
self._oscillating = oscillating
await self._set_oscillating_script.async_run(
{ATTR_OSCILLATING: oscillating})
{ATTR_OSCILLATING: oscillating}, context=self._context)
else:
_LOGGER.error(
'Received invalid oscillating value: %s. Expected: %s.',
Expand All @@ -271,7 +272,7 @@ async def async_set_direction(self, direction: str) -> None:
if direction in _VALID_DIRECTIONS:
self._direction = direction
await self._set_direction_script.async_run(
{ATTR_DIRECTION: direction})
{ATTR_DIRECTION: direction}, context=self._context)
else:
_LOGGER.error(
'Received invalid direction: %s. Expected: %s.',
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/light/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ async def async_turn_on(self, **kwargs):
optimistic_set = True

if ATTR_BRIGHTNESS in kwargs and self._level_script:
self.hass.async_create_task(self._level_script.async_run(
{"brightness": kwargs[ATTR_BRIGHTNESS]}))
await self._level_script.async_run(
{"brightness": kwargs[ATTR_BRIGHTNESS]}, context=self._context)
else:
await self._on_script.async_run()

Expand All @@ -225,7 +225,7 @@ async def async_turn_on(self, **kwargs):

async def async_turn_off(self, **kwargs):
"""Turn the light off."""
await self._off_script.async_run()
await self._off_script.async_run(context=self._context)
if self._template is None:
self._state = False
self.async_schedule_update_ha_state()
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/lock/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,11 @@ async def async_lock(self, **kwargs):
if self._optimistic:
self._state = True
self.async_schedule_update_ha_state()
await self._command_lock.async_run()
await self._command_lock.async_run(context=self._context)

async def async_unlock(self, **kwargs):
"""Unlock the device."""
if self._optimistic:
self._state = False
self.async_schedule_update_ha_state()
await self._command_unlock.async_run()
await self._command_unlock.async_run(context=self._context)
4 changes: 2 additions & 2 deletions homeassistant/components/switch/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ def entity_picture(self):

async def async_turn_on(self, **kwargs):
"""Fire the on action."""
await self._on_script.async_run()
await self._on_script.async_run(context=self._context)

async def async_turn_off(self, **kwargs):
"""Fire the off action."""
await self._off_script.async_run()
await self._off_script.async_run(context=self._context)

async def async_update(self):
"""Update the state from the template."""
Expand Down

0 comments on commit ac79ff9

Please sign in to comment.