Apply common entity schema for MQTT Scene (#96949)

This commit is contained in:
Jan Bouwhuis 2023-07-22 18:00:27 +02:00 committed by GitHub
parent d4f301f4a3
commit 9a5774a95d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 187 additions and 16 deletions

View File

@ -9,19 +9,16 @@ import voluptuous as vol
from homeassistant.components import scene from homeassistant.components import scene
from homeassistant.components.scene import Scene from homeassistant.components.scene import Scene
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_ICON, CONF_NAME, CONF_PAYLOAD_ON, CONF_UNIQUE_ID from homeassistant.const import CONF_NAME, CONF_PAYLOAD_ON
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from .client import async_publish
from .config import MQTT_BASE_SCHEMA from .config import MQTT_BASE_SCHEMA
from .const import CONF_COMMAND_TOPIC, CONF_ENCODING, CONF_QOS, CONF_RETAIN from .const import CONF_COMMAND_TOPIC, CONF_ENCODING, CONF_QOS, CONF_RETAIN
from .mixins import ( from .mixins import (
CONF_ENABLED_BY_DEFAULT, MQTT_ENTITY_COMMON_SCHEMA,
CONF_OBJECT_ID,
MQTT_AVAILABILITY_SCHEMA,
MqttEntity, MqttEntity,
async_setup_entry_helper, async_setup_entry_helper,
) )
@ -30,20 +27,16 @@ from .util import valid_publish_topic
DEFAULT_NAME = "MQTT Scene" DEFAULT_NAME = "MQTT Scene"
DEFAULT_RETAIN = False DEFAULT_RETAIN = False
ENTITY_ID_FORMAT = scene.DOMAIN + ".{}"
PLATFORM_SCHEMA_MODERN = MQTT_BASE_SCHEMA.extend( PLATFORM_SCHEMA_MODERN = MQTT_BASE_SCHEMA.extend(
{ {
vol.Required(CONF_COMMAND_TOPIC): valid_publish_topic, vol.Required(CONF_COMMAND_TOPIC): valid_publish_topic,
vol.Optional(CONF_ICON): cv.icon, vol.Optional(CONF_NAME): vol.Any(cv.string, None),
vol.Optional(CONF_NAME): cv.string,
vol.Optional(CONF_PAYLOAD_ON): cv.string, vol.Optional(CONF_PAYLOAD_ON): cv.string,
vol.Optional(CONF_UNIQUE_ID): cv.string,
vol.Optional(CONF_RETAIN, default=DEFAULT_RETAIN): cv.boolean, vol.Optional(CONF_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
vol.Optional(CONF_OBJECT_ID): cv.string,
# CONF_ENABLED_BY_DEFAULT is not added by default because
# we are not using the common schema here
vol.Optional(CONF_ENABLED_BY_DEFAULT, default=True): cv.boolean,
} }
).extend(MQTT_AVAILABILITY_SCHEMA.schema) ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
DISCOVERY_SCHEMA = PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.REMOVE_EXTRA) DISCOVERY_SCHEMA = PLATFORM_SCHEMA_MODERN.extend({}, extra=vol.REMOVE_EXTRA)
@ -97,7 +90,6 @@ class MqttScene(
def _setup_from_config(self, config: ConfigType) -> None: def _setup_from_config(self, config: ConfigType) -> None:
"""(Re)Setup the entity.""" """(Re)Setup the entity."""
self._config = config
def _prepare_subscribe_topics(self) -> None: def _prepare_subscribe_topics(self) -> None:
"""(Re)Subscribe to topics.""" """(Re)Subscribe to topics."""
@ -110,8 +102,7 @@ class MqttScene(
This method is a coroutine. This method is a coroutine.
""" """
await async_publish( await self.async_publish(
self.hass,
self._config[CONF_COMMAND_TOPIC], self._config[CONF_COMMAND_TOPIC],
self._config[CONF_PAYLOAD_ON], self._config[CONF_PAYLOAD_ON],
self._config[CONF_QOS], self._config[CONF_QOS],

View File

@ -1,5 +1,6 @@
"""The tests for the MQTT scene platform.""" """The tests for the MQTT scene platform."""
import copy import copy
from typing import Any
from unittest.mock import patch from unittest.mock import patch
import pytest import pytest
@ -16,10 +17,23 @@ from .test_common import (
help_test_discovery_broken, help_test_discovery_broken,
help_test_discovery_removal, help_test_discovery_removal,
help_test_discovery_update, help_test_discovery_update,
help_test_discovery_update_attr,
help_test_discovery_update_unchanged, help_test_discovery_update_unchanged,
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_publishing_with_custom_encoding,
help_test_reloadable, help_test_reloadable,
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_unique_id, help_test_unique_id,
help_test_unload_config_entry_with_platform, help_test_unload_config_entry_with_platform,
help_test_update_with_json_attrs_bad_json,
help_test_update_with_json_attrs_not_dict,
) )
from tests.common import mock_restore_cache from tests.common import mock_restore_cache
@ -241,6 +255,172 @@ async def test_discovery_broken(
) )
async def test_setting_attribute_via_mqtt_json_message(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry, scene.DOMAIN, DEFAULT_CONFIG
)
async def test_setting_blocked_attribute_via_mqtt_json_message(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_blocked_attribute_via_mqtt_json_message(
hass, mqtt_mock_entry, scene.DOMAIN, DEFAULT_CONFIG, None
)
async def test_setting_attribute_with_template(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test the setting of attribute via MQTT with JSON payload."""
await help_test_setting_attribute_with_template(
hass, mqtt_mock_entry, scene.DOMAIN, DEFAULT_CONFIG
)
async def test_update_with_json_attrs_not_dict(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_not_dict(
hass,
mqtt_mock_entry,
caplog,
scene.DOMAIN,
DEFAULT_CONFIG,
)
async def test_update_with_json_attrs_bad_json(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test attributes get extracted from a JSON result."""
await help_test_update_with_json_attrs_bad_json(
hass,
mqtt_mock_entry,
caplog,
scene.DOMAIN,
DEFAULT_CONFIG,
)
async def test_discovery_update_attr(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test update of discovered MQTTAttributes."""
await help_test_discovery_update_attr(
hass,
mqtt_mock_entry,
caplog,
scene.DOMAIN,
DEFAULT_CONFIG,
)
async def test_entity_device_info_with_connection(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test MQTT button device registry integration."""
await help_test_entity_device_info_with_connection(
hass, mqtt_mock_entry, scene.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_with_identifier(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test MQTT button device registry integration."""
await help_test_entity_device_info_with_identifier(
hass, mqtt_mock_entry, scene.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_update(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test device registry update."""
await help_test_entity_device_info_update(
hass, mqtt_mock_entry, scene.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_device_info_remove(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test device registry remove."""
await help_test_entity_device_info_remove(
hass, mqtt_mock_entry, scene.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_id_update_discovery_update(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test MQTT discovery update when entity_id is updated."""
await help_test_entity_id_update_discovery_update(
hass, mqtt_mock_entry, scene.DOMAIN, DEFAULT_CONFIG
)
async def test_entity_debug_info_message(
hass: HomeAssistant, mqtt_mock_entry: MqttMockHAClientGenerator
) -> None:
"""Test MQTT debug info."""
await help_test_entity_debug_info_message(
hass,
mqtt_mock_entry,
scene.DOMAIN,
DEFAULT_CONFIG,
scene.SERVICE_TURN_ON,
command_payload="test-payload-on",
state_topic=None,
)
@pytest.mark.parametrize(
("service", "topic", "parameters", "payload", "template"),
[
(scene.SERVICE_TURN_ON, "command_topic", None, "test-payload-on", None),
],
)
async def test_publishing_with_custom_encoding(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
service: str,
topic: str,
parameters: dict[str, Any],
payload: str,
template: str | None,
) -> None:
"""Test publishing MQTT payload with different encoding."""
domain = scene.DOMAIN
config = DEFAULT_CONFIG
await help_test_publishing_with_custom_encoding(
hass,
mqtt_mock_entry,
caplog,
domain,
config,
service,
topic,
parameters,
payload,
template,
)
async def test_reloadable( async def test_reloadable(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_client_mock: MqttMockPahoClient, mqtt_client_mock: MqttMockPahoClient,