"""The tests for the MQTT JSON light platform. Configuration with RGB, brightness, color temp, effect, and XY: mqtt: light: schema: json name: mqtt_json_light_1 state_topic: "home/rgb1" command_topic: "home/rgb1/set" brightness: true color_temp: true effect: true rgb: true xy: true Configuration with RGB, brightness, color temp and effect: mqtt: light: schema: json name: mqtt_json_light_1 state_topic: "home/rgb1" command_topic: "home/rgb1/set" brightness: true color_temp: true effect: true rgb: true Configuration with RGB, brightness and color temp: mqtt: light: schema: json name: mqtt_json_light_1 state_topic: "home/rgb1" command_topic: "home/rgb1/set" brightness: true rgb: true color_temp: true Configuration with RGB, brightness: mqtt: light: schema: json name: mqtt_json_light_1 state_topic: "home/rgb1" command_topic: "home/rgb1/set" brightness: true rgb: true Config without RGB: mqtt: light: schema: json name: mqtt_json_light_1 state_topic: "home/rgb1" command_topic: "home/rgb1/set" brightness: true Config without RGB and brightness: light: platform: mqtt_json name: mqtt_json_light_1 state_topic: "home/rgb1" command_topic: "home/rgb1/set" Config with brightness and scale: light: platform: mqtt_json name: test state_topic: "mqtt_json_light_1" command_topic: "mqtt_json_light_1/set" brightness: true brightness_scale: 99 """ import copy import json from unittest.mock import call, patch import pytest from homeassistant.components import light, mqtt from homeassistant.components.mqtt.light.schema_basic import ( MQTT_LIGHT_ATTRIBUTES_BLOCKED, ) from homeassistant.const import ( ATTR_ASSUMED_STATE, ATTR_SUPPORTED_FEATURES, STATE_OFF, STATE_ON, STATE_UNKNOWN, Platform, ) import homeassistant.core as ha from homeassistant.setup import async_setup_component from .test_common import ( help_test_availability_when_connection_lost, help_test_availability_without_topic, help_test_custom_availability_payload, help_test_default_availability_payload, help_test_discovery_broken, help_test_discovery_removal, help_test_discovery_update, help_test_discovery_update_attr, help_test_discovery_update_unchanged, help_test_encoding_subscribable_topics, help_test_entity_debug_info_message, help_test_entity_device_info_remove, help_test_entity_device_info_update, help_test_entity_device_info_with_connection, help_test_entity_device_info_with_identifier, help_test_entity_id_update_discovery_update, help_test_entity_id_update_subscriptions, help_test_publishing_with_custom_encoding, help_test_reloadable, help_test_reloadable_late, help_test_setting_attribute_via_mqtt_json_message, help_test_setting_attribute_with_template, help_test_setting_blocked_attribute_via_mqtt_json_message, help_test_setup_manual_entity_from_yaml, help_test_unique_id, help_test_update_with_json_attrs_bad_json, help_test_update_with_json_attrs_not_dict, ) from tests.common import async_fire_mqtt_message, mock_restore_cache from tests.components.light import common DEFAULT_CONFIG = { mqtt.DOMAIN: { light.DOMAIN: { "schema": "json", "name": "test", "command_topic": "test-topic", } } } # Test deprecated YAML configuration under the platform key # Scheduled to be removed in HA core 2022.12 DEFAULT_CONFIG_LEGACY = copy.deepcopy(DEFAULT_CONFIG[mqtt.DOMAIN]) DEFAULT_CONFIG_LEGACY[light.DOMAIN]["platform"] = mqtt.DOMAIN @pytest.fixture(autouse=True) def light_platform_only(): """Only setup the light platform to speed up tests.""" with patch("homeassistant.components.mqtt.PLATFORMS", [Platform.LIGHT]): yield class JsonValidator: """Helper to compare JSON.""" def __init__(self, jsondata): """Initialize JSON validator.""" self.jsondata = jsondata def __eq__(self, other): """Compare JSON data.""" return json.loads(self.jsondata) == json.loads(other) async def test_fail_setup_if_no_command_topic(hass, caplog): """Test if setup fails with no command topic.""" assert not await async_setup_component( hass, mqtt.DOMAIN, {mqtt.DOMAIN: {light.DOMAIN: {"schema": "json", "name": "test"}}}, ) assert ( "Invalid config for [mqtt]: required key not provided @ data['mqtt']['light'][0]['command_topic']. Got None." in caplog.text ) @pytest.mark.parametrize("deprecated", ("color_temp", "hs", "rgb", "xy")) async def test_fail_setup_if_color_mode_deprecated(hass, caplog, deprecated): """Test if setup fails if color mode is combined with deprecated config keys.""" supported_color_modes = ["color_temp", "hs", "rgb", "rgbw", "rgbww", "xy"] config = { light.DOMAIN: { "brightness": True, "color_mode": True, "command_topic": "test_light_rgb/set", "name": "test", "schema": "json", "supported_color_modes": supported_color_modes, } } config[light.DOMAIN][deprecated] = True assert not await async_setup_component( hass, mqtt.DOMAIN, {mqtt.DOMAIN: config}, ) assert ( "Invalid config for [mqtt]: color_mode must not be combined with any of" in caplog.text ) @pytest.mark.parametrize( "supported_color_modes,error", [ (["onoff", "rgb"], "Unknown error calling mqtt CONFIG_SCHEMA"), (["brightness", "rgb"], "Unknown error calling mqtt CONFIG_SCHEMA"), (["unknown"], "Invalid config for [mqtt]: value must be one of [