Skip to content

Commit

Permalink
Fix entiy category for sensor fails mqtt setup
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouwh committed Nov 5, 2023
1 parent 8062059 commit 64c5764
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 30 deletions.
6 changes: 4 additions & 2 deletions homeassistant/components/mqtt/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)

DISCOVERY_SCHEMA = vol.All(
validate_sensor_entity_category,
validate_sensor_entity_category(binary_sensor.DOMAIN),
_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA),
)

PLATFORM_SCHEMA_MODERN = vol.All(validate_sensor_entity_category, _PLATFORM_SCHEMA_BASE)
PLATFORM_SCHEMA_MODERN = vol.All(
validate_sensor_entity_category(binary_sensor.DOMAIN), _PLATFORM_SCHEMA_BASE
)


async def async_setup_entry(
Expand Down
45 changes: 37 additions & 8 deletions homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
CONF_VALUE_TEMPLATE,
EntityCategory,
)
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback
from homeassistant.core import CALLBACK_TYPE, HomeAssistant, async_get_hass, callback
from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
Expand Down Expand Up @@ -208,14 +208,43 @@ def validate_device_has_at_least_one_identifier(value: ConfigType) -> ConfigType
)


def validate_sensor_entity_category(config: ConfigType) -> ConfigType:
def validate_sensor_entity_category(domain: str) -> Callable[[ConfigType], ConfigType]:
"""Check the sensor's entity category is not set to `config` which is invalid for sensors."""
if (
CONF_ENTITY_CATEGORY in config
and config[CONF_ENTITY_CATEGORY] == EntityCategory.CONFIG
):
raise vol.Invalid("Entity category `config` is invalid")
return config

# A guard was added to the core sensor platform with HA core 2023.11.0
# See: https://github.com/home-assistant/core/pull/101471
# A developers blog from october 2021 explains the correct uses of the entity category
# See:
# https://developers.home-assistant.io/blog/2021/10/26/config-entity/?_highlight=entity_category#entity-categories
#
# To limitate the impact of the change we use a grace period
# of 3 months for user to update there configs.

def _validate(config: ConfigType) -> ConfigType:
if (
CONF_ENTITY_CATEGORY in config
and config[CONF_ENTITY_CATEGORY] == EntityCategory.CONFIG
):
config_str = yaml_dump(config)
config.pop(CONF_ENTITY_CATEGORY)
_LOGGER.warning("Entity category `config` is invalid for sensors, ignoring")
hass = async_get_hass()
async_create_issue(
hass,
domain=DOMAIN,
issue_id="invalid_entity_category",
breaks_in_ha_version="2024.2.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="invalid_entity_category",
learn_more_url=(
f"https://www.home-assistant.io/integrations/{domain}.mqtt/"
),
translation_placeholders={"domain": domain, "config": config_str},
)
return config

return _validate


MQTT_ENTITY_DEVICE_INFO_SCHEMA = vol.All(
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/mqtt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@
# Deprecated in HA Core 2021.11.0 https://github.com/home-assistant/core/pull/54840
# Removed in HA Core 2023.6.0
cv.removed(CONF_LAST_RESET_TOPIC),
validate_sensor_entity_category,
validate_sensor_entity_category(sensor.DOMAIN),
_PLATFORM_SCHEMA_BASE,
)

DISCOVERY_SCHEMA = vol.All(
# Deprecated in HA Core 2021.11.0 https://github.com/home-assistant/core/pull/54840
# Removed in HA Core 2023.6.0
cv.removed(CONF_LAST_RESET_TOPIC),
validate_sensor_entity_category,
validate_sensor_entity_category(sensor.DOMAIN),
_PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA),
)

Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/mqtt/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
"title": "MQTT entities with auxiliary heat support found",
"description": "Entity `{entity_id}` has auxiliary heat support enabled, which has been deprecated for MQTT climate devices. Please adjust your configuration and remove deprecated config options from your configuration and restart Home Assistant to fix this issue."
},
"invalid_entity_category": {
"title": "An MQTT {domain} with an invalid entity category was found",
"description": "Home Assistant detected a manually configured `{domain}` that has an `entity_category` set to `config`. Config with invalid setting:\n\n```yaml\n{config}\n```\n\nWhen set, make sure `entity_category` for a `{domain}` is set to `diagnostic` or `None`. Update your configuration and restart Home Assistant to fix this issue."
},
"invalid_platform_config": {
"title": "Invalid config found for mqtt {domain} item",
"description": "Home Assistant detected an invalid config for a manually configured item.\n\nPlatform domain: **{domain}**\nConfiguration file: **{config_file}**\nNear line: **{line}**\nConfiguration found:\n```yaml\n{config}\n```\nError: **{error}**.\n\nMake sure the configuration is valid and [reload](/developer-tools/yaml) the manually configured MQTT items or restart Home Assistant to fix this issue."
Expand Down
45 changes: 27 additions & 18 deletions tests/components/mqtt/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2152,26 +2152,32 @@ async def test_setup_manual_mqtt_with_invalid_config(


@pytest.mark.parametrize(
"hass_config",
("hass_config", "entity_id"),
[
{
mqtt.DOMAIN: {
"sensor": {
"name": "test",
"state_topic": "test-topic",
"entity_category": "config",
(
{
mqtt.DOMAIN: {
"sensor": {
"name": "test",
"state_topic": "test-topic",
"entity_category": "config",
}
}
}
},
{
mqtt.DOMAIN: {
"binary_sensor": {
"name": "test",
"state_topic": "test-topic",
"entity_category": "config",
},
"sensor.test",
),
(
{
mqtt.DOMAIN: {
"binary_sensor": {
"name": "test",
"state_topic": "test-topic",
"entity_category": "config",
}
}
}
},
},
"binary_sensor.test",
),
],
)
@patch(
Expand All @@ -2181,10 +2187,13 @@ async def test_setup_manual_mqtt_with_invalid_entity_category(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
entity_id: str,
) -> None:
"""Test set up a manual sensor item with an invalid entity category."""
assert await mqtt_mock_entry()
assert "Entity category `config` is invalid" in caplog.text
assert "Entity category `config` is invalid for sensors, ignoring" in caplog.text
state = hass.states.get(entity_id)
assert state is not None


@patch("homeassistant.components.mqtt.PLATFORMS", [])
Expand Down

0 comments on commit 64c5764

Please sign in to comment.