mirror of
https://github.com/home-assistant/core.git
synced 2026-09-24 07:25:52 -05:00
Mqtt light refactor (#18227)
* Rename mqtt light files * Refactor mqtt light * Remove outdated testcase * Add backwards compatibility for MQTT discovered MQTT lights. Refactor according to review comments.
This commit is contained in:
committed by
Paulus Schoutsen
parent
c1ed2f17ac
commit
16e3ff2fec
@@ -585,7 +585,8 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
|
||||
'effect': 'random',
|
||||
'color_temp': 100,
|
||||
'white_value': 50})
|
||||
with patch('homeassistant.components.light.mqtt.async_get_last_state',
|
||||
with patch('homeassistant.components.light.mqtt.schema_basic'
|
||||
'.async_get_last_state',
|
||||
return_value=mock_coro(fake_state)):
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, config)
|
||||
@@ -1063,3 +1064,20 @@ async def test_discovery_removal_light(hass, mqtt_mock, caplog):
|
||||
|
||||
state = hass.states.get('light.beer')
|
||||
assert state is None
|
||||
|
||||
|
||||
async def test_discovery_deprecated(hass, mqtt_mock, caplog):
|
||||
"""Test removal of discovered mqtt_json lights."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
await async_start(hass, 'homeassistant', {'mqtt': {}}, entry)
|
||||
data = (
|
||||
'{ "name": "Beer",'
|
||||
' "platform": "mqtt",'
|
||||
' "command_topic": "test_topic"}'
|
||||
)
|
||||
async_fire_mqtt_message(hass, 'homeassistant/light/bla/config',
|
||||
data)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get('light.beer')
|
||||
assert state is not None
|
||||
assert state.name == 'Beer'
|
||||
|
||||
@@ -93,18 +93,19 @@ from homeassistant.setup import async_setup_component
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE,
|
||||
ATTR_SUPPORTED_FEATURES)
|
||||
import homeassistant.components.light as light
|
||||
from homeassistant.components import light, mqtt
|
||||
from homeassistant.components.mqtt.discovery import async_start
|
||||
import homeassistant.core as ha
|
||||
|
||||
from tests.common import mock_coro, async_fire_mqtt_message
|
||||
from tests.common import mock_coro, async_fire_mqtt_message, MockConfigEntry
|
||||
|
||||
|
||||
async def test_fail_setup_if_no_command_topic(hass, mqtt_mock):
|
||||
"""Test if setup fails with no command topic."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
}
|
||||
})
|
||||
@@ -116,7 +117,8 @@ async def test_no_color_brightness_color_temp_white_val_if_no_topics(
|
||||
"""Test for no RGB, brightness, color temp, effect, white val or XY."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
@@ -152,7 +154,8 @@ async def test_controlling_state_via_topic(hass, mqtt_mock):
|
||||
"""Test the controlling of the state via topic."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
@@ -276,12 +279,13 @@ async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
|
||||
'color_temp': 100,
|
||||
'white_value': 50})
|
||||
|
||||
with patch('homeassistant.components.light.mqtt_json'
|
||||
with patch('homeassistant.components.light.mqtt.schema_json'
|
||||
'.async_get_last_state',
|
||||
return_value=mock_coro(fake_state)):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'brightness': True,
|
||||
@@ -308,7 +312,8 @@ async def test_sending_hs_color(hass, mqtt_mock):
|
||||
"""Test light.turn_on with hs color sends hs color parameters."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'hs': True,
|
||||
@@ -323,7 +328,8 @@ async def test_flash_short_and_long(hass, mqtt_mock):
|
||||
"""Test for flash length being sent when included."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
@@ -342,7 +348,8 @@ async def test_transition(hass, mqtt_mock):
|
||||
"""Test for transition time being sent when included."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
@@ -359,7 +366,8 @@ async def test_brightness_scale(hass, mqtt_mock):
|
||||
"""Test for brightness scaling."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_bright_scale',
|
||||
'command_topic': 'test_light_bright_scale/set',
|
||||
@@ -395,7 +403,8 @@ async def test_invalid_color_brightness_and_white_values(hass, mqtt_mock):
|
||||
"""Test that invalid color/brightness/white values are ignored."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
@@ -466,7 +475,8 @@ async def test_default_availability_payload(hass, mqtt_mock):
|
||||
"""Test availability by default payload with defined topic."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
@@ -495,7 +505,8 @@ async def test_custom_availability_payload(hass, mqtt_mock):
|
||||
"""Test availability by custom payload with defined topic."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_json',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'json',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
@@ -524,10 +535,11 @@ async def test_custom_availability_payload(hass, mqtt_mock):
|
||||
|
||||
async def test_discovery_removal(hass, mqtt_mock, caplog):
|
||||
"""Test removal of discovered mqtt_json lights."""
|
||||
await async_start(hass, 'homeassistant', {'mqtt': {}})
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
await async_start(hass, 'homeassistant', {'mqtt': {}}, entry)
|
||||
data = (
|
||||
'{ "name": "Beer",'
|
||||
' "platform": "mqtt_json",'
|
||||
' "schema": "json",'
|
||||
' "command_topic": "test_topic" }'
|
||||
)
|
||||
async_fire_mqtt_message(hass, 'homeassistant/light/bla/config',
|
||||
@@ -542,3 +554,20 @@ async def test_discovery_removal(hass, mqtt_mock, caplog):
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get('light.beer')
|
||||
assert state is None
|
||||
|
||||
|
||||
async def test_discovery_deprecated(hass, mqtt_mock, caplog):
|
||||
"""Test removal of discovered mqtt_json lights."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
await async_start(hass, 'homeassistant', {'mqtt': {}}, entry)
|
||||
data = (
|
||||
'{ "name": "Beer",'
|
||||
' "platform": "mqtt_json",'
|
||||
' "command_topic": "test_topic"}'
|
||||
)
|
||||
async_fire_mqtt_message(hass, 'homeassistant/light/bla/config',
|
||||
data)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get('light.beer')
|
||||
assert state is not None
|
||||
assert state.name == 'Beer'
|
||||
|
||||
@@ -31,11 +31,13 @@ from unittest.mock import patch
|
||||
from homeassistant.setup import async_setup_component
|
||||
from homeassistant.const import (
|
||||
STATE_ON, STATE_OFF, STATE_UNAVAILABLE, ATTR_ASSUMED_STATE)
|
||||
import homeassistant.components.light as light
|
||||
from homeassistant.components import light, mqtt
|
||||
from homeassistant.components.mqtt.discovery import async_start
|
||||
import homeassistant.core as ha
|
||||
|
||||
from tests.common import (
|
||||
async_fire_mqtt_message, assert_setup_component, mock_coro)
|
||||
async_fire_mqtt_message, assert_setup_component, mock_coro,
|
||||
MockConfigEntry)
|
||||
|
||||
|
||||
async def test_setup_fails(hass, mqtt_mock):
|
||||
@@ -43,19 +45,56 @@ async def test_setup_fails(hass, mqtt_mock):
|
||||
with assert_setup_component(0, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
}
|
||||
})
|
||||
assert hass.states.get('light.test') is None
|
||||
|
||||
with assert_setup_component(0, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_topic',
|
||||
}
|
||||
})
|
||||
assert hass.states.get('light.test') is None
|
||||
|
||||
with assert_setup_component(0, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_topic',
|
||||
'command_on_template': 'on',
|
||||
}
|
||||
})
|
||||
assert hass.states.get('light.test') is None
|
||||
|
||||
with assert_setup_component(0, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_topic',
|
||||
'command_off_template': 'off',
|
||||
}
|
||||
})
|
||||
assert hass.states.get('light.test') is None
|
||||
|
||||
|
||||
async def test_state_change_via_topic(hass, mqtt_mock):
|
||||
"""Test state change via topic."""
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'state_topic': 'test_light_rgb',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
@@ -96,7 +135,8 @@ async def test_state_brightness_color_effect_temp_white_change_via_topic(
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'effect_list': ['rainbow', 'colorloop'],
|
||||
'state_topic': 'test_light_rgb',
|
||||
@@ -205,13 +245,14 @@ async def test_optimistic(hass, mqtt_mock):
|
||||
'color_temp': 100,
|
||||
'white_value': 50})
|
||||
|
||||
with patch('homeassistant.components.light.mqtt_template'
|
||||
with patch('homeassistant.components.light.mqtt.schema_template'
|
||||
'.async_get_last_state',
|
||||
return_value=mock_coro(fake_state)):
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'command_on_template': 'on,'
|
||||
@@ -243,7 +284,8 @@ async def test_flash(hass, mqtt_mock):
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'command_on_template': 'on,{{ flash }}',
|
||||
@@ -261,7 +303,8 @@ async def test_transition(hass, mqtt_mock):
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'command_on_template': 'on,{{ transition }}',
|
||||
@@ -278,7 +321,8 @@ async def test_invalid_values(hass, mqtt_mock):
|
||||
with assert_setup_component(1, light.DOMAIN):
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'effect_list': ['rainbow', 'colorloop'],
|
||||
'state_topic': 'test_light_rgb',
|
||||
@@ -380,7 +424,8 @@ async def test_default_availability_payload(hass, mqtt_mock):
|
||||
"""Test availability by default payload with defined topic."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'command_on_template': 'on,{{ transition }}',
|
||||
@@ -410,7 +455,8 @@ async def test_custom_availability_payload(hass, mqtt_mock):
|
||||
"""Test availability by custom payload with defined topic."""
|
||||
assert await async_setup_component(hass, light.DOMAIN, {
|
||||
light.DOMAIN: {
|
||||
'platform': 'mqtt_template',
|
||||
'platform': 'mqtt',
|
||||
'schema': 'template',
|
||||
'name': 'test',
|
||||
'command_topic': 'test_light_rgb/set',
|
||||
'command_on_template': 'on,{{ transition }}',
|
||||
@@ -436,3 +482,41 @@ async def test_custom_availability_payload(hass, mqtt_mock):
|
||||
|
||||
state = hass.states.get('light.test')
|
||||
assert STATE_UNAVAILABLE == state.state
|
||||
|
||||
|
||||
async def test_discovery(hass, mqtt_mock, caplog):
|
||||
"""Test removal of discovered mqtt_json lights."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
await async_start(hass, 'homeassistant', {'mqtt': {}}, entry)
|
||||
data = (
|
||||
'{ "name": "Beer",'
|
||||
' "schema": "template",'
|
||||
' "command_topic": "test_topic",'
|
||||
' "command_on_template": "on",'
|
||||
' "command_off_template": "off"}'
|
||||
)
|
||||
async_fire_mqtt_message(hass, 'homeassistant/light/bla/config',
|
||||
data)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get('light.beer')
|
||||
assert state is not None
|
||||
assert state.name == 'Beer'
|
||||
|
||||
|
||||
async def test_discovery_deprecated(hass, mqtt_mock, caplog):
|
||||
"""Test removal of discovered mqtt_json lights."""
|
||||
entry = MockConfigEntry(domain=mqtt.DOMAIN)
|
||||
await async_start(hass, 'homeassistant', {'mqtt': {}}, entry)
|
||||
data = (
|
||||
'{ "name": "Beer",'
|
||||
' "platform": "mqtt_template",'
|
||||
' "command_topic": "test_topic",'
|
||||
' "command_on_template": "on",'
|
||||
' "command_off_template": "off"}'
|
||||
)
|
||||
async_fire_mqtt_message(hass, 'homeassistant/light/bla/config',
|
||||
data)
|
||||
await hass.async_block_till_done()
|
||||
state = hass.states.get('light.beer')
|
||||
assert state is not None
|
||||
assert state.name == 'Beer'
|
||||
|
||||
Reference in New Issue
Block a user