Skip to content

Commit

Permalink
Do not fail MQTT setup if vacuum's configured via yaml can't be valid…
Browse files Browse the repository at this point in the history
…ated (home-assistant#102325)

Add vacuum
  • Loading branch information
jbouwh authored Oct 19, 2023
1 parent f497bce commit 22c21fd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 70 deletions.
6 changes: 1 addition & 5 deletions homeassistant/components/mqtt/config_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
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 (
Expand Down Expand Up @@ -104,10 +103,7 @@
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]
Expand Down
56 changes: 24 additions & 32 deletions homeassistant/components/mqtt/vacuum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand All @@ -39,13 +38,13 @@
# 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,
Expand All @@ -63,6 +62,7 @@ def warn_for_deprecation_legacy_schema(
)


@callback
def validate_mqtt_vacuum_discovery(config_value: ConfigType) -> ConfigType:
"""Validate MQTT vacuum schema."""

Expand All @@ -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."""

Expand All @@ -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


Expand All @@ -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},
)
17 changes: 2 additions & 15 deletions homeassistant/components/mqtt/vacuum/schema_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@
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
Expand Down Expand Up @@ -201,17 +199,6 @@
}


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."""

Expand Down
12 changes: 0 additions & 12 deletions homeassistant/components/mqtt/vacuum/schema_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
)
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
Expand Down Expand Up @@ -156,17 +155,6 @@
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."""

Expand Down
8 changes: 2 additions & 6 deletions tests/components/mqtt/test_legacy_vacuum.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 22c21fd

Please sign in to comment.