Skip to content

Commit 453d6b7

Browse files
authored
Add template condition (#61)
1 parent 93f76dd commit 453d6b7

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ media_lights_sync:
7979

8080
<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.
8181

82+
### Using a more complex `condition`
83+
84+
You can use a template condition to match more complex states:
85+
86+
```yaml
87+
condition:
88+
value_template: "{{ state_attr('media_player.tv', 'media_content_type') == 'movie' and is_state('sun.sun', 'below_horizon') }}"
89+
```
90+
91+
The app will run if the condition returns `True`.
92+
8293
## Selecting a `quantization_method`
8394

8495
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:

apps/media_lights_sync/media_lights_sync.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ def change_lights_color(self, entity, attribute, old_url, new_url, kwargs):
7979

8080
def can_change_colors(self):
8181
"""Validate that light should be sync if a condition is set."""
82-
return self.condition is None or self.get_state(self.condition["entity"]) == self.condition["state"]
82+
if self.condition is None:
83+
return True
84+
elif "value_template" in self.condition:
85+
return self.render_template(self.condition["value_template"]) == True
86+
else:
87+
return self.get_state(self.condition["entity"]) == self.condition["state"]
8388

8489
def store_initial_lights_states(self):
8590
"""Save the initial state of all lights if not already done."""

tests/media_lights_sync_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,28 @@ def test_wont_change_if_condition_is_false(self, given_that, conditional_media_l
307307
assert conditional_media_lights_sync.can_change_colors() == False
308308

309309

310+
class TestTemplateConditions:
311+
template = "{{ 'test' != 123 and is_state('sun.sun', 'below_horizon') }}"
312+
313+
@pytest.fixture
314+
def conditional_template_media_lights_sync(self, media_lights_sync, given_that, update_passed_args):
315+
given_that.state_of('sun.sun').is_set_to('below_horizon')
316+
with update_passed_args():
317+
given_that.passed_arg('condition').is_set_to({"value_template": self.template})
318+
319+
# hass.render_template is not available in appdaemontestframework, so we mock it here.
320+
media_lights_sync.render_template = lambda _template: 'test' != 123 and media_lights_sync.get_state('sun.sun') == 'below_horizon'
321+
return media_lights_sync
322+
323+
def test_can_change_if_condition_is_met(self, given_that, conditional_template_media_lights_sync):
324+
given_that.state_of('sun.sun').is_set_to('below_horizon')
325+
assert conditional_template_media_lights_sync.can_change_colors() == True
326+
327+
def test_wont_change_if_condition_is_false(self, given_that, conditional_template_media_lights_sync):
328+
given_that.state_of('sun.sun').is_set_to('above_horizon')
329+
assert conditional_template_media_lights_sync.can_change_colors() == False
330+
331+
310332
class TestFormatUrl:
311333
relative_url = "/api/media_player_proxy/media_player.tv_test"
312334

0 commit comments

Comments
 (0)