Optimistic MQTT light (#14401)
* Restores light state, case the light is optimistic * lint * hound * hound * Added mqtt_json * hound * added mqtt_template * lint * cleanup * use ATTR
This commit is contained in:
1 parent
16bf10b1a2
commit
d47006c98f
6 files changed
+165
-73
No files matched your search
@@ -140,14 +140,16 @@ light:
|
||||
"""
|
||||
import unittest
|
||||
from unittest import mock
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE)
|
||||
import homeassistant.components.light as light
|
||||
import homeassistant.core as ha
|
||||
from tests.common import (
|
||||
assert_setup_component, get_test_home_assistant, mock_mqtt_component,
|
||||
fire_mqtt_message)
|
||||
fire_mqtt_message, mock_coro)
|
||||
|
||||
|
||||
class TestLightMQTT(unittest.TestCase):
|
||||
@@ -481,12 +483,23 @@ class TestLightMQTT(unittest.TestCase):
|
||||
'payload_on': 'on',
|
||||
'payload_off': 'off'
|
||||
}}
|
||||
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert setup_component(self.hass, light.DOMAIN, config)
|
||||
fake_state = ha.State('light.test', 'on', {'brightness': 95,
|
||||
'hs_color': [100, 100],
|
||||
'effect': 'random',
|
||||
'color_temp': 100,
|
||||
'white_value': 50})
|
||||
with patch('homeassistant.components.light.mqtt.async_get_last_state',
|
||||
return_value=mock_coro(fake_state)):
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert setup_component(self.hass, light.DOMAIN, config)
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
self.assertEqual(95, state.attributes.get('brightness'))
|
||||
self.assertEqual((100, 100), state.attributes.get('hs_color'))
|
||||
self.assertEqual('random', state.attributes.get('effect'))
|
||||
self.assertEqual(100, state.attributes.get('color_temp'))
|
||||
self.assertEqual(50, state.attributes.get('white_value'))
|
||||
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
|
||||
|
||||
light.turn_on(self.hass, 'light.test')
|
||||
|
||||
@@ -90,15 +90,17 @@ light:
|
||||
|
||||
import json
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE,
|
||||
ATTR_SUPPORTED_FEATURES)
|
||||
import homeassistant.components.light as light
|
||||
import homeassistant.core as ha
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message,
|
||||
assert_setup_component)
|
||||
assert_setup_component, mock_coro)
|
||||
|
||||
|
||||
class TestLightMQTTJSON(unittest.TestCase):
|
||||
@@ -284,22 +286,36 @@ class TestLightMQTTJSON(unittest.TestCase):
|
||||
def test_sending_mqtt_commands_and_optimistic(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test the sending of command in optimistic mode."""
|
||||
assert setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness': True,
|
||||
'color_temp': True,
|
||||
'effect': True,
|
||||
'rgb': True,
|
||||
'white_value': True,
|
||||
'qos': 2
|
||||
}
|
||||
})
|
||||
fake_state = ha.State('light.test', 'on', {'brightness': 95,
|
||||
'hs_color': [100, 100],
|
||||
'effect': 'random',
|
||||
'color_temp': 100,
|
||||
'white_value': 50})
|
||||
|
||||
with patch('homeassistant.components.light.mqtt_json'
|
||||
'.async_get_last_state',
|
||||
return_value=mock_coro(fake_state)):
|
||||
assert setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness': True,
|
||||
'color_temp': True,
|
||||
'effect': True,
|
||||
'rgb': True,
|
||||
'white_value': True,
|
||||
'qos': 2
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
self.assertEqual(95, state.attributes.get('brightness'))
|
||||
self.assertEqual((100, 100), state.attributes.get('hs_color'))
|
||||
self.assertEqual('random', state.attributes.get('effect'))
|
||||
self.assertEqual(100, state.attributes.get('color_temp'))
|
||||
self.assertEqual(50, state.attributes.get('white_value'))
|
||||
self.assertEqual(191, state.attributes.get(ATTR_SUPPORTED_FEATURES))
|
||||
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
|
||||
|
||||
|
||||
@@ -27,14 +27,16 @@ If your light doesn't support white value feature, omit `white_value_template`.
|
||||
If your light doesn't support RGB feature, omit `(red|green|blue)_template`.
|
||||
"""
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
from homeassistant.setup import setup_component
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE)
|
||||
import homeassistant.components.light as light
|
||||
import homeassistant.core as ha
|
||||
from tests.common import (
|
||||
get_test_home_assistant, mock_mqtt_component, fire_mqtt_message,
|
||||
assert_setup_component)
|
||||
assert_setup_component, mock_coro)
|
||||
|
||||
|
||||
class TestLightMQTTTemplate(unittest.TestCase):
|
||||
@@ -207,26 +209,40 @@ class TestLightMQTTTemplate(unittest.TestCase):
|
||||
def test_optimistic(self): \
|
||||
# pylint: disable=invalid-name
|
||||
"""Test optimistic mode."""
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'command_on_template': 'on,'
|
||||
'{{ brightness|d }},'
|
||||
'{{ color_temp|d }},'
|
||||
'{{ white_value|d }},'
|
||||
'{{ red|d }}-'
|
||||
'{{ green|d }}-'
|
||||
'{{ blue|d }}',
|
||||
'command_off_template': 'off',
|
||||
'qos': 2
|
||||
}
|
||||
})
|
||||
fake_state = ha.State('light.test', 'on', {'brightness': 95,
|
||||
'hs_color': [100, 100],
|
||||
'effect': 'random',
|
||||
'color_temp': 100,
|
||||
'white_value': 50})
|
||||
|
||||
with patch('homeassistant.components.light.mqtt_template'
|
||||
'.async_get_last_state',
|
||||
return_value=mock_coro(fake_state)):
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert setup_component(self.hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'command_on_template': 'on,'
|
||||
'{{ brightness|d }},'
|
||||
'{{ color_temp|d }},'
|
||||
'{{ white_value|d }},'
|
||||
'{{ red|d }}-'
|
||||
'{{ green|d }}-'
|
||||
'{{ blue|d }}',
|
||||
'command_off_template': 'off',
|
||||
'qos': 2
|
||||
}
|
||||
})
|
||||
|
||||
state = self.hass.states.get('light.test')
|
||||
self.assertEqual(STATE_OFF, state.state)
|
||||
self.assertEqual(STATE_ON, state.state)
|
||||
self.assertEqual(95, state.attributes.get('brightness'))
|
||||
self.assertEqual((100, 100), state.attributes.get('hs_color'))
|
||||
self.assertEqual('random', state.attributes.get('effect'))
|
||||
self.assertEqual(100, state.attributes.get('color_temp'))
|
||||
self.assertEqual(50, state.attributes.get('white_value'))
|
||||
self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))
|
||||
|
||||
# turn on the light
|
||||
|
||||
Reference in new issue
Block a user