Skip to content

Commit

Permalink
Merge pull request #38 from pschmitt/optional-on-off
Browse files Browse the repository at this point in the history
Make turn on and off actions optional
  • Loading branch information
Sennevds authored Sep 19, 2022
2 parents 51b5c1a + ab64e93 commit 7b8c3ad
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions custom_components/media_player_template/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@
vol.Optional(CONF_ENTITY_PICTURE_TEMPLATE): cv.template,
vol.Optional(CONF_AVAILABILITY_TEMPLATE): cv.template,
vol.Optional(CURRENT_SOURCE_TEMPLATE): cv.template,
vol.Required(ON_ACTION): cv.SCRIPT_SCHEMA,
vol.Required(OFF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(ON_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(OFF_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(PLAY_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(STOP_ACTION): cv.SCRIPT_SCHEMA,
vol.Optional(PAUSE_ACTION): cv.SCRIPT_SCHEMA,
Expand Down Expand Up @@ -143,7 +143,6 @@
vol.Optional(CURRENT_SOUND_MODE_TEMPLATE): cv.template,
}
)
SUPPORT_TEMPLATE = SUPPORT_TURN_OFF | SUPPORT_TURN_ON

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Required(CONF_MEDIAPLAYER): cv.schema_with_slug_keys(MEDIA_PLAYER_SCHEMA)}
Expand All @@ -170,8 +169,8 @@ async def _async_create_entities(hass, config):
entity_picture_template = device_config.get(CONF_ENTITY_PICTURE_TEMPLATE)
availability_template = device_config.get(CONF_AVAILABILITY_TEMPLATE)
current_source_template = device_config.get(CURRENT_SOURCE_TEMPLATE)
on_action = device_config[ON_ACTION]
off_action = device_config[OFF_ACTION]
on_action = device_config.get(ON_ACTION)
off_action = device_config.get(OFF_ACTION)
play_action = device_config.get(PLAY_ACTION)
stop_action = device_config.get(STOP_ACTION)
pause_action = device_config.get(PAUSE_ACTION)
Expand Down Expand Up @@ -309,8 +308,15 @@ def __init__(
self._device_class = device_class
self._template = state_template
self._domain = __name__.split(".")[-2]
self._on_script = Script(hass, on_action, friendly_name, self._domain)
self._off_script = Script(hass, off_action, friendly_name, self._domain)

self._on_script = None
if on_action is not None:
self._on_script = Script(hass, on_action, friendly_name, self._domain)

self._off_script = None
if off_action is not None:
self._off_script = Script(hass, off_action, friendly_name, self._domain)

self._play_script = None
if play_action is not None:
self._play_script = Script(hass, play_action, friendly_name, self._domain)
Expand Down Expand Up @@ -513,7 +519,11 @@ def entity_picture(self):
def supported_features(self):
"""Flag media player features that are supported."""

support = SUPPORT_TEMPLATE
support = 0
if self._on_script is not None:
support |= SUPPORT_TURN_ON
if self._off_script is not None:
support |= SUPPORT_TURN_OFF
if self._play_script is not None:
support |= SUPPORT_PLAY
if self._stop_script is not None:
Expand Down

0 comments on commit 7b8c3ad

Please sign in to comment.