Skip to content

Commit

Permalink
Add template condition (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmatte authored Jan 5, 2023
1 parent 93f76dd commit 453d6b7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ media_lights_sync:

<b id="ha-url-note">[1](#ha-url)</b>: See `/developer-tools/state` in your Home Assistant instance. This app will listen to changes on `entity_picture_local` and/or `entity_picture` attributes of your `media_player` entities.

### Using a more complex `condition`

You can use a template condition to match more complex states:

```yaml
condition:
value_template: "{{ state_attr('media_player.tv', 'media_content_type') == 'movie' and is_state('sun.sun', 'below_horizon') }}"
```

The app will run if the condition returns `True`.

## Selecting a `quantization_method`

There is four [quantization method](https://pillow.readthedocs.io/en/stable/reference/Image.html?highlight=getpalette#quantization-methods) available, which change the way the colors palette is extracted:
Expand Down
7 changes: 6 additions & 1 deletion apps/media_lights_sync/media_lights_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,12 @@ def change_lights_color(self, entity, attribute, old_url, new_url, kwargs):

def can_change_colors(self):
"""Validate that light should be sync if a condition is set."""
return self.condition is None or self.get_state(self.condition["entity"]) == self.condition["state"]
if self.condition is None:
return True
elif "value_template" in self.condition:
return self.render_template(self.condition["value_template"]) == True
else:
return self.get_state(self.condition["entity"]) == self.condition["state"]

def store_initial_lights_states(self):
"""Save the initial state of all lights if not already done."""
Expand Down
22 changes: 22 additions & 0 deletions tests/media_lights_sync_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,28 @@ def test_wont_change_if_condition_is_false(self, given_that, conditional_media_l
assert conditional_media_lights_sync.can_change_colors() == False


class TestTemplateConditions:
template = "{{ 'test' != 123 and is_state('sun.sun', 'below_horizon') }}"

@pytest.fixture
def conditional_template_media_lights_sync(self, media_lights_sync, given_that, update_passed_args):
given_that.state_of('sun.sun').is_set_to('below_horizon')
with update_passed_args():
given_that.passed_arg('condition').is_set_to({"value_template": self.template})

# hass.render_template is not available in appdaemontestframework, so we mock it here.
media_lights_sync.render_template = lambda _template: 'test' != 123 and media_lights_sync.get_state('sun.sun') == 'below_horizon'
return media_lights_sync

def test_can_change_if_condition_is_met(self, given_that, conditional_template_media_lights_sync):
given_that.state_of('sun.sun').is_set_to('below_horizon')
assert conditional_template_media_lights_sync.can_change_colors() == True

def test_wont_change_if_condition_is_false(self, given_that, conditional_template_media_lights_sync):
given_that.state_of('sun.sun').is_set_to('above_horizon')
assert conditional_template_media_lights_sync.can_change_colors() == False


class TestFormatUrl:
relative_url = "/api/media_player_proxy/media_player.tv_test"

Expand Down

0 comments on commit 453d6b7

Please sign in to comment.