mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Do not fail MQTT setup if vacuum's configured via yaml can't be validated (#102325)
Add vacuum
This commit is contained in:
parent
f497bcee3a
commit
22c21fdc18
@ -27,7 +27,6 @@ from . import (
|
|||||||
sensor as sensor_platform,
|
sensor as sensor_platform,
|
||||||
switch as switch_platform,
|
switch as switch_platform,
|
||||||
update as update_platform,
|
update as update_platform,
|
||||||
vacuum as vacuum_platform,
|
|
||||||
water_heater as water_heater_platform,
|
water_heater as water_heater_platform,
|
||||||
)
|
)
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -104,10 +103,7 @@ CONFIG_SCHEMA_BASE = vol.Schema(
|
|||||||
cv.ensure_list,
|
cv.ensure_list,
|
||||||
[update_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
[update_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
||||||
),
|
),
|
||||||
Platform.VACUUM.value: vol.All(
|
Platform.VACUUM.value: vol.All(cv.ensure_list, [dict]),
|
||||||
cv.ensure_list,
|
|
||||||
[vacuum_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
|
||||||
),
|
|
||||||
Platform.WATER_HEATER.value: vol.All(
|
Platform.WATER_HEATER.value: vol.All(
|
||||||
cv.ensure_list,
|
cv.ensure_list,
|
||||||
[water_heater_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
[water_heater_platform.PLATFORM_SCHEMA_MODERN], # type: ignore[has-type]
|
||||||
|
@ -5,30 +5,29 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import functools
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components import vacuum
|
from homeassistant.components import vacuum
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
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 ..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 import CONF_SCHEMA, LEGACY, MQTT_VACUUM_SCHEMA, STATE
|
||||||
from .schema_legacy import (
|
from .schema_legacy import (
|
||||||
DISCOVERY_SCHEMA_LEGACY,
|
DISCOVERY_SCHEMA_LEGACY,
|
||||||
PLATFORM_SCHEMA_LEGACY_MODERN,
|
PLATFORM_SCHEMA_LEGACY_MODERN,
|
||||||
async_setup_entity_legacy,
|
MqttVacuum,
|
||||||
)
|
)
|
||||||
from .schema_state import (
|
from .schema_state import (
|
||||||
DISCOVERY_SCHEMA_STATE,
|
DISCOVERY_SCHEMA_STATE,
|
||||||
PLATFORM_SCHEMA_STATE_MODERN,
|
PLATFORM_SCHEMA_STATE_MODERN,
|
||||||
async_setup_entity_state,
|
MqttStateVacuum,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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
|
# The legacy schema for MQTT vacuum was deprecated with HA Core 2023.8.0
|
||||||
# and will be removed with HA Core 2024.2.0
|
# and will be removed with HA Core 2024.2.0
|
||||||
def warn_for_deprecation_legacy_schema(
|
def warn_for_deprecation_legacy_schema(
|
||||||
hass: HomeAssistant, config: ConfigType, discovery_data: DiscoveryInfoType | None
|
hass: HomeAssistant, config: ConfigType, discovery: bool
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Warn for deprecation of legacy schema."""
|
"""Warn for deprecation of legacy schema."""
|
||||||
if config[CONF_SCHEMA] == STATE:
|
if config[CONF_SCHEMA] == STATE:
|
||||||
return
|
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}"
|
translation_key = f"deprecation_mqtt_legacy_vacuum_{key_suffix}"
|
||||||
async_create_issue(
|
async_create_issue(
|
||||||
hass,
|
hass,
|
||||||
@ -63,6 +62,7 @@ def warn_for_deprecation_legacy_schema(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
def validate_mqtt_vacuum_discovery(config_value: ConfigType) -> ConfigType:
|
def validate_mqtt_vacuum_discovery(config_value: ConfigType) -> ConfigType:
|
||||||
"""Validate MQTT vacuum schema."""
|
"""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}
|
schemas = {LEGACY: DISCOVERY_SCHEMA_LEGACY, STATE: DISCOVERY_SCHEMA_STATE}
|
||||||
config: ConfigType = schemas[config_value[CONF_SCHEMA]](config_value)
|
config: ConfigType = schemas[config_value[CONF_SCHEMA]](config_value)
|
||||||
|
hass = async_get_hass()
|
||||||
|
warn_for_deprecation_legacy_schema(hass, config, True)
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
@callback
|
||||||
def validate_mqtt_vacuum_modern(config_value: ConfigType) -> ConfigType:
|
def validate_mqtt_vacuum_modern(config_value: ConfigType) -> ConfigType:
|
||||||
"""Validate MQTT vacuum modern schema."""
|
"""Validate MQTT vacuum modern schema."""
|
||||||
|
|
||||||
@ -85,6 +88,10 @@ def validate_mqtt_vacuum_modern(config_value: ConfigType) -> ConfigType:
|
|||||||
STATE: PLATFORM_SCHEMA_STATE_MODERN,
|
STATE: PLATFORM_SCHEMA_STATE_MODERN,
|
||||||
}
|
}
|
||||||
config: ConfigType = schemas[config_value[CONF_SCHEMA]](config_value)
|
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
|
return config
|
||||||
|
|
||||||
|
|
||||||
@ -103,28 +110,13 @@ async def async_setup_entry(
|
|||||||
async_add_entities: AddEntitiesCallback,
|
async_add_entities: AddEntitiesCallback,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up MQTT vacuum through YAML and through MQTT discovery."""
|
"""Set up MQTT vacuum through YAML and through MQTT discovery."""
|
||||||
setup = functools.partial(
|
await async_mqtt_entry_helper(
|
||||||
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
|
hass,
|
||||||
)
|
config_entry,
|
||||||
await async_setup_entry_helper(hass, vacuum.DOMAIN, setup, DISCOVERY_SCHEMA)
|
None,
|
||||||
|
vacuum.DOMAIN,
|
||||||
|
async_add_entities,
|
||||||
async def _async_setup_entity(
|
DISCOVERY_SCHEMA,
|
||||||
hass: HomeAssistant,
|
PLATFORM_SCHEMA_MODERN,
|
||||||
async_add_entities: AddEntitiesCallback,
|
{"legacy": MqttVacuum, "state": MqttStateVacuum},
|
||||||
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
|
|
||||||
)
|
)
|
||||||
|
@ -17,14 +17,12 @@ from homeassistant.components.vacuum import (
|
|||||||
VacuumEntity,
|
VacuumEntity,
|
||||||
VacuumEntityFeature,
|
VacuumEntityFeature,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
|
||||||
from homeassistant.const import ATTR_SUPPORTED_FEATURES, CONF_NAME
|
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
|
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.icon import icon_for_battery_level
|
||||||
from homeassistant.helpers.json import json_dumps
|
from homeassistant.helpers.json import json_dumps
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType
|
||||||
|
|
||||||
from .. import subscription
|
from .. import subscription
|
||||||
from ..config import MQTT_BASE_SCHEMA
|
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):
|
class MqttVacuum(MqttEntity, VacuumEntity):
|
||||||
"""Representation of a MQTT-controlled legacy vacuum."""
|
"""Representation of a MQTT-controlled legacy vacuum."""
|
||||||
|
|
||||||
|
@ -23,7 +23,6 @@ from homeassistant.const import (
|
|||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
||||||
from homeassistant.helpers.json import json_dumps
|
from homeassistant.helpers.json import json_dumps
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
from homeassistant.util.json import json_loads_object
|
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)
|
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):
|
class MqttStateVacuum(MqttEntity, StateVacuumEntity):
|
||||||
"""Representation of a MQTT-controlled state vacuum."""
|
"""Representation of a MQTT-controlled state vacuum."""
|
||||||
|
|
||||||
|
@ -642,12 +642,8 @@ async def test_missing_templates(
|
|||||||
caplog: pytest.LogCaptureFixture,
|
caplog: pytest.LogCaptureFixture,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test to make sure missing template is not allowed."""
|
"""Test to make sure missing template is not allowed."""
|
||||||
with pytest.raises(AssertionError):
|
assert await mqtt_mock_entry()
|
||||||
await mqtt_mock_entry()
|
assert "some but not all values in the same group of inclusion" in caplog.text
|
||||||
assert (
|
|
||||||
"Invalid config for [mqtt]: some but not all values in the same group of inclusion"
|
|
||||||
in caplog.text
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG_2])
|
@pytest.mark.parametrize("hass_config", [DEFAULT_CONFIG_2])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user