mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
Move manual configuration of MQTT number to the integration key (#72272)
Add number
This commit is contained in:
parent
c2612d1ade
commit
623abb1147
@ -202,6 +202,7 @@ PLATFORM_CONFIG_SCHEMA_BASE = vol.Schema(
|
|||||||
vol.Optional(Platform.LOCK.value): cv.ensure_list,
|
vol.Optional(Platform.LOCK.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,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
"""Configure number in a device through MQTT topic."""
|
"""Configure number in a device through MQTT topic."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import asyncio
|
||||||
import functools
|
import functools
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
@ -40,8 +41,10 @@ from .debug_info import log_messages
|
|||||||
from .mixins import (
|
from .mixins import (
|
||||||
MQTT_ENTITY_COMMON_SCHEMA,
|
MQTT_ENTITY_COMMON_SCHEMA,
|
||||||
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__)
|
||||||
@ -72,7 +75,7 @@ def validate_config(config):
|
|||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
_PLATFORM_SCHEMA_BASE = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend(
|
_PLATFORM_SCHEMA_BASE = mqtt.MQTT_RW_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
vol.Optional(CONF_COMMAND_TEMPLATE): cv.template,
|
vol.Optional(CONF_COMMAND_TEMPLATE): cv.template,
|
||||||
vol.Optional(CONF_MAX, default=DEFAULT_MAX_VALUE): vol.Coerce(float),
|
vol.Optional(CONF_MAX, default=DEFAULT_MAX_VALUE): vol.Coerce(float),
|
||||||
@ -88,11 +91,18 @@ _PLATFORM_SCHEMA_BASE = mqtt.MQTT_RW_PLATFORM_SCHEMA.extend(
|
|||||||
},
|
},
|
||||||
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
|
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = vol.All(
|
PLATFORM_SCHEMA_MODERN = vol.All(
|
||||||
_PLATFORM_SCHEMA_BASE,
|
_PLATFORM_SCHEMA_BASE,
|
||||||
validate_config,
|
validate_config,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Configuring MQTT Number under the number platform key is deprecated in HA Core 2022.6
|
||||||
|
PLATFORM_SCHEMA = vol.All(
|
||||||
|
cv.PLATFORM_SCHEMA.extend(_PLATFORM_SCHEMA_BASE.schema),
|
||||||
|
validate_config,
|
||||||
|
warn_for_legacy_schema(number.DOMAIN),
|
||||||
|
)
|
||||||
|
|
||||||
DISCOVERY_SCHEMA = vol.All(
|
DISCOVERY_SCHEMA = vol.All(
|
||||||
_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA),
|
_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA),
|
||||||
validate_config,
|
validate_config,
|
||||||
@ -105,7 +115,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 number through configuration.yaml."""
|
"""Set up MQTT number configured under the number platform key (deprecated)."""
|
||||||
|
# Deprecated in HA Core 2022.6
|
||||||
await async_setup_platform_helper(
|
await async_setup_platform_helper(
|
||||||
hass, number.DOMAIN, config, async_add_entities, _async_setup_entity
|
hass, number.DOMAIN, config, async_add_entities, _async_setup_entity
|
||||||
)
|
)
|
||||||
@ -116,7 +127,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 number dynamically through MQTT discovery."""
|
"""Set up MQTT number 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, number.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
|
||||||
)
|
)
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
"""The tests for mqtt number component."""
|
"""The tests for mqtt number component."""
|
||||||
|
import copy
|
||||||
import json
|
import json
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
@ -50,6 +51,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,
|
||||||
@ -729,3 +731,15 @@ async def test_encoding_subscribable_topics(
|
|||||||
attribute,
|
attribute,
|
||||||
attribute_value,
|
attribute_value,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def test_setup_manual_entity_from_yaml(hass, caplog, tmp_path):
|
||||||
|
"""Test setup manual configured MQTT entity."""
|
||||||
|
platform = number.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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user