Move manual configuration of MQTT sensor to the integration key (#72276)

Add sensor
This commit is contained in:
Jan Bouwhuis 2022-05-23 16:29:45 +02:00 committed by GitHub
parent 204e26a1b5
commit d0556e6dd1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 5 deletions

View file

@ -203,6 +203,7 @@ PLATFORM_CONFIG_SCHEMA_BASE = vol.Schema(
vol.Optional(Platform.SCENE.value): cv.ensure_list, vol.Optional(Platform.SCENE.value): cv.ensure_list,
vol.Optional(Platform.SELECT.value): cv.ensure_list, vol.Optional(Platform.SELECT.value): cv.ensure_list,
vol.Optional(Platform.SIREN.value): cv.ensure_list, vol.Optional(Platform.SIREN.value): cv.ensure_list,
vol.Optional(Platform.SENSOR.value): cv.ensure_list,
vol.Optional(Platform.SWITCH.value): cv.ensure_list, vol.Optional(Platform.SWITCH.value): cv.ensure_list,
vol.Optional(Platform.VACUUM.value): cv.ensure_list, vol.Optional(Platform.VACUUM.value): cv.ensure_list,
vol.Optional(Platform.NUMBER.value): cv.ensure_list, vol.Optional(Platform.NUMBER.value): cv.ensure_list,

View file

@ -1,6 +1,7 @@
"""Support for MQTT sensors.""" """Support for MQTT sensors."""
from __future__ import annotations from __future__ import annotations
import asyncio
from datetime import timedelta from datetime import timedelta
import functools import functools
import logging import logging
@ -41,8 +42,10 @@ from .mixins import (
MQTT_ENTITY_COMMON_SCHEMA, MQTT_ENTITY_COMMON_SCHEMA,
MqttAvailability, MqttAvailability,
MqttEntity, MqttEntity,
async_get_platform_config_from_yaml,
async_setup_entry_helper, async_setup_entry_helper,
async_setup_platform_helper, async_setup_platform_helper,
warn_for_legacy_schema,
) )
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -86,7 +89,7 @@ def validate_options(conf):
return conf return conf
_PLATFORM_SCHEMA_BASE = mqtt.MQTT_RO_PLATFORM_SCHEMA.extend( _PLATFORM_SCHEMA_BASE = mqtt.MQTT_RO_SCHEMA.extend(
{ {
vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA, vol.Optional(CONF_DEVICE_CLASS): DEVICE_CLASSES_SCHEMA,
vol.Optional(CONF_EXPIRE_AFTER): cv.positive_int, vol.Optional(CONF_EXPIRE_AFTER): cv.positive_int,
@ -99,12 +102,19 @@ _PLATFORM_SCHEMA_BASE = mqtt.MQTT_RO_PLATFORM_SCHEMA.extend(
} }
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema) ).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
PLATFORM_SCHEMA = vol.All( PLATFORM_SCHEMA_MODERN = vol.All(
cv.deprecated(CONF_LAST_RESET_TOPIC),
_PLATFORM_SCHEMA_BASE, _PLATFORM_SCHEMA_BASE,
validate_options, validate_options,
) )
# Configuring MQTT Sensors under the sensor platform key is deprecated in HA Core 2022.6
PLATFORM_SCHEMA = vol.All(
cv.deprecated(CONF_LAST_RESET_TOPIC),
cv.PLATFORM_SCHEMA.extend(_PLATFORM_SCHEMA_BASE.schema),
validate_options,
warn_for_legacy_schema(sensor.DOMAIN),
)
DISCOVERY_SCHEMA = vol.All( DISCOVERY_SCHEMA = vol.All(
cv.deprecated(CONF_LAST_RESET_TOPIC), cv.deprecated(CONF_LAST_RESET_TOPIC),
_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA), _PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA),
@ -118,7 +128,8 @@ async def async_setup_platform(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up MQTT sensors through configuration.yaml.""" """Set up MQTT sensors configured under the fan platform key (deprecated)."""
# Deprecated in HA Core 2022.6
await async_setup_platform_helper( await async_setup_platform_helper(
hass, sensor.DOMAIN, config, async_add_entities, _async_setup_entity hass, sensor.DOMAIN, config, async_add_entities, _async_setup_entity
) )
@ -129,7 +140,17 @@ async def async_setup_entry(
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Set up MQTT sensors dynamically through MQTT discovery.""" """Set up MQTT sensor through configuration.yaml and dynamically through MQTT discovery."""
# load and initialize platform config from configuration.yaml
await asyncio.gather(
*(
_async_setup_entity(hass, async_add_entities, config, config_entry)
for config in await async_get_platform_config_from_yaml(
hass, sensor.DOMAIN, PLATFORM_SCHEMA_MODERN
)
)
)
# setup for discovery
setup = functools.partial( setup = functools.partial(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry _async_setup_entity, hass, async_add_entities, config_entry=config_entry
) )

View file

@ -55,6 +55,7 @@ from .test_common import (
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_setting_blocked_attribute_via_mqtt_json_message,
help_test_setup_manual_entity_from_yaml,
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,
@ -1106,3 +1107,15 @@ async def test_encoding_subscribable_topics(
attribute_value, attribute_value,
skip_raw_test=True, skip_raw_test=True,
) )
async def test_setup_manual_entity_from_yaml(hass, caplog, tmp_path):
"""Test setup manual configured MQTT entity."""
platform = sensor.DOMAIN
config = copy.deepcopy(DEFAULT_CONFIG[platform])
config["name"] = "test"
del config["platform"]
await help_test_setup_manual_entity_from_yaml(
hass, caplog, tmp_path, platform, config
)
assert hass.states.get(f"{platform}.test") is not None