From 1674c8309a6c9463015ca942476892e0933d6c00 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Thu, 24 Sep 2015 00:06:05 -0400 Subject: [PATCH 1/6] Support playing, pausing states for media players when reproducing state This allows the state helper to call the correct service call for media_players when attempting to resolve state. --- homeassistant/helpers/state.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index d4a18806a17..b1ea902ae61 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -9,7 +9,8 @@ import logging from homeassistant.core import State import homeassistant.util.dt as dt_util from homeassistant.const import ( - STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID) + STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, + STATE_PLAYING, STATE_PAUSED, ATTR_ENTITY_ID) _LOGGER = logging.getLogger(__name__) @@ -55,14 +56,19 @@ def reproduce_state(hass, states, blocking=False): state.entity_id) continue - if state.state == STATE_ON: - service = SERVICE_TURN_ON - elif state.state == STATE_OFF: - service = SERVICE_TURN_OFF - else: - _LOGGER.warning("reproduce_state: Unable to reproduce state %s", - state) - continue + if state.domain == 'media_player' and state == STATE_PAUSED: + service = 'media_pause' + elif state.domain == 'media_player' and state == STATE_PLAYING: + service = 'media_play' + elif + if state.state == STATE_ON: + service = SERVICE_TURN_ON + elif state.state == STATE_OFF: + service = SERVICE_TURN_OFF + else: + _LOGGER.warning("reproduce_state: Unable to reproduce state %s", + state) + continue service_data = dict(state.attributes) service_data[ATTR_ENTITY_ID] = state.entity_id From 4a8bbc52e096dd7cd720f9eaeb3211efe68a2986 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Thu, 24 Sep 2015 00:15:36 -0400 Subject: [PATCH 2/6] derp --- homeassistant/helpers/state.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index b1ea902ae61..d46f0be6d34 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -60,7 +60,7 @@ def reproduce_state(hass, states, blocking=False): service = 'media_pause' elif state.domain == 'media_player' and state == STATE_PLAYING: service = 'media_play' - elif + else if state.state == STATE_ON: service = SERVICE_TURN_ON elif state.state == STATE_OFF: From 082920abe07670e3ead3b3c4be237f759f29b021 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Thu, 24 Sep 2015 00:20:05 -0400 Subject: [PATCH 3/6] moar constants --- homeassistant/helpers/state.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index d46f0be6d34..c62e57bee36 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -10,6 +10,7 @@ from homeassistant.core import State import homeassistant.util.dt as dt_util from homeassistant.const import ( STATE_ON, STATE_OFF, SERVICE_TURN_ON, SERVICE_TURN_OFF, + SERVICE_MEDIA_PLAY, SERVICE_MEDIA_PAUSE, STATE_PLAYING, STATE_PAUSED, ATTR_ENTITY_ID) _LOGGER = logging.getLogger(__name__) @@ -57,9 +58,9 @@ def reproduce_state(hass, states, blocking=False): continue if state.domain == 'media_player' and state == STATE_PAUSED: - service = 'media_pause' + service = SERVICE_MEDIA_PAUSE elif state.domain == 'media_player' and state == STATE_PLAYING: - service = 'media_play' + service = SERVICE_MEDIA_PLAY else if state.state == STATE_ON: service = SERVICE_TURN_ON From 90f35b35cd4220bbcfe614a22f2502b29f15a99d Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Thu, 24 Sep 2015 00:20:17 -0400 Subject: [PATCH 4/6] moar derp --- homeassistant/helpers/state.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index c62e57bee36..1de6e7d367b 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -61,7 +61,7 @@ def reproduce_state(hass, states, blocking=False): service = SERVICE_MEDIA_PAUSE elif state.domain == 'media_player' and state == STATE_PLAYING: service = SERVICE_MEDIA_PLAY - else + else: if state.state == STATE_ON: service = SERVICE_TURN_ON elif state.state == STATE_OFF: From 8d42e422303e1bcde8f4bb382480b45619f8dac8 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Thu, 24 Sep 2015 00:38:18 -0400 Subject: [PATCH 5/6] style --- homeassistant/helpers/state.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index 1de6e7d367b..eff70384a3a 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -67,8 +67,8 @@ def reproduce_state(hass, states, blocking=False): elif state.state == STATE_OFF: service = SERVICE_TURN_OFF else: - _LOGGER.warning("reproduce_state: Unable to reproduce state %s", - state) + _LOGGER.warning("reproduce_state: Unable to reproduce \ + state %s", state) continue service_data = dict(state.attributes) From efdd0c9e8a54298b66bb73edee1e0ff73d86bf52 Mon Sep 17 00:00:00 2001 From: Jon Maddox Date: Thu, 24 Sep 2015 01:35:08 -0400 Subject: [PATCH 6/6] don't break the chain --- homeassistant/helpers/state.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index eff70384a3a..6d4cb36dd4b 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -61,15 +61,14 @@ def reproduce_state(hass, states, blocking=False): service = SERVICE_MEDIA_PAUSE elif state.domain == 'media_player' and state == STATE_PLAYING: service = SERVICE_MEDIA_PLAY + elif state.state == STATE_ON: + service = SERVICE_TURN_ON + elif state.state == STATE_OFF: + service = SERVICE_TURN_OFF else: - if state.state == STATE_ON: - service = SERVICE_TURN_ON - elif state.state == STATE_OFF: - service = SERVICE_TURN_OFF - else: - _LOGGER.warning("reproduce_state: Unable to reproduce \ - state %s", state) - continue + _LOGGER.warning("reproduce_state: Unable to reproduce state %s", + state) + continue service_data = dict(state.attributes) service_data[ATTR_ENTITY_ID] = state.entity_id