mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 08:17:08 +00:00
Filter MQTT switch JSON attributes (#52290)
This commit is contained in:
parent
de4cfb0ce2
commit
74e61ab7f7
@ -32,6 +32,13 @@ from .. import mqtt
|
|||||||
from .debug_info import log_messages
|
from .debug_info import log_messages
|
||||||
from .mixins import MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, async_setup_entry_helper
|
from .mixins import MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, async_setup_entry_helper
|
||||||
|
|
||||||
|
MQTT_SWITCH_ATTRIBUTES_BLOCKED = frozenset(
|
||||||
|
{
|
||||||
|
switch.ATTR_CURRENT_POWER_W,
|
||||||
|
switch.ATTR_TODAY_ENERGY_KWH,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
DEFAULT_NAME = "MQTT Switch"
|
DEFAULT_NAME = "MQTT Switch"
|
||||||
DEFAULT_PAYLOAD_ON = "ON"
|
DEFAULT_PAYLOAD_ON = "ON"
|
||||||
DEFAULT_PAYLOAD_OFF = "OFF"
|
DEFAULT_PAYLOAD_OFF = "OFF"
|
||||||
@ -78,6 +85,8 @@ async def _async_setup_entity(
|
|||||||
class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
||||||
"""Representation of a switch that can be toggled using MQTT."""
|
"""Representation of a switch that can be toggled using MQTT."""
|
||||||
|
|
||||||
|
_attributes_extra_blocked = MQTT_SWITCH_ATTRIBUTES_BLOCKED
|
||||||
|
|
||||||
def __init__(self, hass, config, config_entry, discovery_data):
|
def __init__(self, hass, config, config_entry, discovery_data):
|
||||||
"""Initialize the MQTT switch."""
|
"""Initialize the MQTT switch."""
|
||||||
self._state = False
|
self._state = False
|
||||||
|
@ -6,6 +6,7 @@ from unittest.mock import patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components import switch
|
from homeassistant.components import switch
|
||||||
|
from homeassistant.components.mqtt.switch import MQTT_SWITCH_ATTRIBUTES_BLOCKED
|
||||||
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_OFF, STATE_ON
|
from homeassistant.const import ATTR_ASSUMED_STATE, STATE_OFF, STATE_ON
|
||||||
import homeassistant.core as ha
|
import homeassistant.core as ha
|
||||||
from homeassistant.setup import async_setup_component
|
from homeassistant.setup import async_setup_component
|
||||||
@ -29,6 +30,7 @@ from .test_common import (
|
|||||||
help_test_entity_id_update_subscriptions,
|
help_test_entity_id_update_subscriptions,
|
||||||
help_test_setting_attribute_via_mqtt_json_message,
|
help_test_setting_attribute_via_mqtt_json_message,
|
||||||
help_test_setting_attribute_with_template,
|
help_test_setting_attribute_with_template,
|
||||||
|
help_test_setting_blocked_attribute_via_mqtt_json_message,
|
||||||
help_test_unique_id,
|
help_test_unique_id,
|
||||||
help_test_update_with_json_attrs_bad_JSON,
|
help_test_update_with_json_attrs_bad_JSON,
|
||||||
help_test_update_with_json_attrs_not_dict,
|
help_test_update_with_json_attrs_not_dict,
|
||||||
@ -246,6 +248,13 @@ async def test_setting_attribute_via_mqtt_json_message(hass, mqtt_mock):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_setting_blocked_attribute_via_mqtt_json_message(hass, mqtt_mock):
|
||||||
|
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||||
|
await help_test_setting_blocked_attribute_via_mqtt_json_message(
|
||||||
|
hass, mqtt_mock, switch.DOMAIN, DEFAULT_CONFIG, MQTT_SWITCH_ATTRIBUTES_BLOCKED
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def test_setting_attribute_with_template(hass, mqtt_mock):
|
async def test_setting_attribute_with_template(hass, mqtt_mock):
|
||||||
"""Test the setting of attribute via MQTT with JSON payload."""
|
"""Test the setting of attribute via MQTT with JSON payload."""
|
||||||
await help_test_setting_attribute_with_template(
|
await help_test_setting_attribute_with_template(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user