Fix entity category for sensor fails mqtt sensor platform setup (#103449)

This commit is contained in:
Jan Bouwhuis
2023-11-08 00:03:47 +01:00
committed by GitHub
parent a11091890f
commit cbccdbc6fa
5 changed files with 125 additions and 33 deletions

View File

@ -30,7 +30,12 @@ from homeassistant.const import (
import homeassistant.core as ha
from homeassistant.core import CoreState, HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr, entity_registry as er, template
from homeassistant.helpers import (
device_registry as dr,
entity_registry as er,
issue_registry as ir,
template,
)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import async_get_platforms
from homeassistant.helpers.typing import ConfigType
@ -42,6 +47,7 @@ from .test_common import help_all_subscribe_calls
from tests.common import (
MockConfigEntry,
MockEntity,
async_capture_events,
async_fire_mqtt_message,
async_fire_time_changed,
mock_restore_cache,
@ -2152,26 +2158,20 @@ 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",
),
],
)
@patch(
@ -2181,10 +2181,52 @@ 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."""
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
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
assert len(events) == 1
@pytest.mark.parametrize(
("config", "entity_id"),
[
(
{
"name": "test",
"state_topic": "test-topic",
"entity_category": "config",
},
"sensor.test",
),
],
)
@patch(
"homeassistant.components.mqtt.PLATFORMS", [Platform.BINARY_SENSOR, Platform.SENSOR]
)
async def test_setup_discovery_mqtt_with_invalid_entity_category(
hass: HomeAssistant,
mqtt_mock_entry: MqttMockHAClientGenerator,
caplog: pytest.LogCaptureFixture,
config: dict[str, Any],
entity_id: str,
) -> None:
"""Test set up a discovered sensor item with an invalid entity category."""
events = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
assert await mqtt_mock_entry()
domain = entity_id.split(".")[0]
json_config = json.dumps(config)
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", json_config)
await hass.async_block_till_done()
assert "Entity category `config` is invalid for sensors, ignoring" in caplog.text
state = hass.states.get(entity_id)
assert state is not None
assert len(events) == 0
@patch("homeassistant.components.mqtt.PLATFORMS", [])