Do not fail MQTT setup if vacuum's configured via yaml can't be validated (#102325)

Add vacuum
pull/95722/head
Jan Bouwhuis 2023-10-19 20:11:09 +02:00 committed by GitHub
parent f497bcee3a
commit 22c21fdc18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 70 deletions

View File

@ -27,7 +27,6 @@ from . import (
sensor as sensor_platform,
switch as switch_platform,
update as update_platform,
vacuum as vacuum_platform,
water_heater as water_heater_platform,
)
from .const import (
@ -104,10 +103,7 @@ CONFIG_SCHEMA_BASE = vol.Schema(
cv.ensure_list,
[update_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
),
Platform.VACUUM.value: vol.All(
cv.ensure_list,
[vacuum_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
),
Platform.VACUUM.value: vol.All(cv.ensure_list, [dict]),
Platform.WATER_HEATER.value: vol.All(
cv.ensure_list,
[water_heater_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]

View File

@ -5,30 +5,29 @@
from __future__ import annotations
import functools
import logging
import voluptuous as vol
from homeassistant.components import vacuum
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, async_get_hass, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.typing import ConfigType
from ..const import DOMAIN
from ..mixins import async_setup_entry_helper
from ..mixins import async_mqtt_entry_helper
from .schema import CONF_SCHEMA, LEGACY, MQTT_VACUUM_SCHEMA, STATE
from .schema_legacy import (
DISCOVERY_SCHEMA_LEGACY,
PLATFORM_SCHEMA_LEGACY_MODERN,
async_setup_entity_legacy,
MqttVacuum,
)
from .schema_state import (
DISCOVERY_SCHEMA_STATE,
PLATFORM_SCHEMA_STATE_MODERN,
async_setup_entity_state,
MqttStateVacuum,
)
_LOGGER = logging.getLogger(__name__)
@ -39,13 +38,13 @@ MQTT_VACUUM_DOCS_URL = "https://www.home-assistant.io/integrations/vacuum.mqtt/"
# The legacy schema for MQTT vacuum was deprecated with HA Core 2023.8.0
# and will be removed with HA Core 2024.2.0
def warn_for_deprecation_legacy_schema(
hass: HomeAssistant, config: ConfigType, discovery_data: DiscoveryInfoType | None
hass: HomeAssistant, config: ConfigType, discovery: bool
) -> None:
"""Warn for deprecation of legacy schema."""
if config[CONF_SCHEMA] == STATE:
return
key_suffix = "yaml" if discovery_data is None else "discovery"
key_suffix = "discovery" if discovery else "yaml"
translation_key = f"deprecation_mqtt_legacy_vacuum_{key_suffix}"
async_create_issue(
hass,
@ -63,6 +62,7 @@ def warn_for_deprecation_legacy_schema(
)
@callback
def validate_mqtt_vacuum_discovery(config_value: ConfigType) -> ConfigType:
"""Validate MQTT vacuum schema."""
@ -71,9 +71,12 @@ def validate_mqtt_vacuum_discovery(config_value: ConfigType) -> ConfigType:
schemas = {LEGACY: DISCOVERY_SCHEMA_LEGACY, STATE: DISCOVERY_SCHEMA_STATE}
config: ConfigType = schemas[config_value[CONF_SCHEMA]](config_value)
hass = async_get_hass()
warn_for_deprecation_legacy_schema(hass, config, True)
return config
@callback
def validate_mqtt_vacuum_modern(config_value: ConfigType) -> ConfigType:
"""Validate MQTT vacuum modern schema."""
@ -85,6 +88,10 @@ def validate_mqtt_vacuum_modern(config_value: ConfigType) -> ConfigType:
STATE: PLATFORM_SCHEMA_STATE_MODERN,
}
config: ConfigType = schemas[config_value[CONF_SCHEMA]](config_value)
# The legacy schema for MQTT vacuum was deprecated with HA Core 2023.8.0
# and will be removed with HA Core 2024.2.0
hass = async_get_hass()
warn_for_deprecation_legacy_schema(hass, config, False)
return config
@ -103,28 +110,13 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up MQTT vacuum through YAML and through MQTT discovery."""
setup = functools.partial(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
)
await async_setup_entry_helper(hass, vacuum.DOMAIN, setup, DISCOVERY_SCHEMA)
async def _async_setup_entity(
hass: HomeAssistant,
async_add_entities: AddEntitiesCallback,
config: ConfigType,
config_entry: ConfigEntry,
discovery_data: DiscoveryInfoType | None = None,
) -> None:
"""Set up the MQTT vacuum."""
# The legacy schema for MQTT vacuum was deprecated with HA Core 2023.8.0
# and will be removed with HA Core 2024.2.0
warn_for_deprecation_legacy_schema(hass, config, discovery_data)
setup_entity = {
LEGACY: async_setup_entity_legacy,
STATE: async_setup_entity_state,
}
await setup_entity[config[CONF_SCHEMA]](
hass, config, async_add_entities, config_entry, discovery_data
await async_mqtt_entry_helper(
hass,
config_entry,
None,
vacuum.DOMAIN,
async_add_entities,
DISCOVERY_SCHEMA,
PLATFORM_SCHEMA_MODERN,
{"legacy": MqttVacuum, "state": MqttStateVacuum},
)

View File

@ -17,14 +17,12 @@ from homeassistant.components.vacuum import (
VacuumEntity,
VacuumEntityFeature,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_SUPPORTED_FEATURES, CONF_NAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.icon import icon_for_battery_level
from homeassistant.helpers.json import json_dumps
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.typing import ConfigType
from .. import subscription
from ..config import MQTT_BASE_SCHEMA
@ -201,17 +199,6 @@ _COMMANDS = {
}
async def async_setup_entity_legacy(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
config_entry: ConfigEntry,
discovery_data: DiscoveryInfoType | None,
) -> None:
"""Set up a MQTT Vacuum Legacy."""
async_add_entities([MqttVacuum(hass, config, config_entry, discovery_data)])
class MqttVacuum(MqttEntity, VacuumEntity):
"""Representation of a MQTT-controlled legacy vacuum."""

View File

@ -23,7 +23,6 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant, callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.json import json_dumps
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.json import json_loads_object
@ -156,17 +155,6 @@ PLATFORM_SCHEMA_STATE_MODERN = (
DISCOVERY_SCHEMA_STATE = PLATFORM_SCHEMA_STATE_MODERN.extend({}, extra=vol.REMOVE_EXTRA)
async def async_setup_entity_state(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
config_entry: ConfigEntry,
discovery_data: DiscoveryInfoType | None,
) -> None:
"""Set up a State MQTT Vacuum."""
async_add_entities([MqttStateVacuum(hass, config, config_entry, discovery_data)])
class MqttStateVacuum(MqttEntity, StateVacuumEntity):
"""Representation of a MQTT-controlled state vacuum."""

View File

@ -642,12 +642,8 @@ async def test_missing_templates(
caplog: pytest.LogCaptureFixture,
) -> None:
"""Test to make sure missing template is not allowed."""
with pytest.raises(AssertionError):
await mqtt_mock_entry()
assert (
"Invalid config for [mqtt]: some but not all values in the same group of inclusion"
in caplog.text
)
assert await mqtt_mock_entry()
assert "some but not all values in the same group of inclusion" in caplog.text
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG_2])