Cleanup code for parsing yaml MQTT config (#86944)

* Cleanup code for parsing yaml configs

* Add abstractmethod decorator to async_update

* Replace get() with default-list ensured by schema
pull/86954/head
Jan Bouwhuis 2023-01-31 08:24:33 +01:00 committed by GitHub
parent 1c4ba61725
commit 33ede351f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 26 deletions

View File

@ -290,24 +290,6 @@ class SetupEntity(Protocol):
"""Define setup_entities type."""
async def async_get_platform_config_from_yaml(
hass: HomeAssistant,
platform_domain: str,
config_yaml: ConfigType | None = None,
) -> list[ConfigType]:
"""Return a list of validated configurations for the domain."""
platform_configs: Any | None
mqtt_data = get_mqtt_data(hass)
if config_yaml is None:
config_yaml = mqtt_data.config
if not config_yaml:
return []
if not (platform_configs := config_yaml.get(platform_domain)):
return []
assert isinstance(platform_configs, list)
return platform_configs
async def async_setup_entry_helper(
hass: HomeAssistant,
domain: str,
@ -359,14 +341,7 @@ async def async_setup_entry_helper(
return
if domain not in config_yaml:
return
await asyncio.gather(
*[
async_setup(config)
for config in await async_get_platform_config_from_yaml(
hass, domain, config_yaml
)
]
)
await asyncio.gather(*[async_setup(config) for config in config_yaml[domain]])
# discover manual configured MQTT items
mqtt_data.reload_handlers[domain] = _async_setup_entities
@ -800,6 +775,7 @@ class MqttDiscoveryDeviceUpdate(ABC):
self.hass, self._device_id, self._config_entry_id
)
@abstractmethod
async def async_update(self, discovery_data: MQTTDiscoveryPayload) -> None:
"""Handle the update of platform specific parts, extend to the platform."""

View File

@ -3062,3 +3062,16 @@ async def test_link_config_entry(hass, tmp_path, caplog):
# reload manual configured items and assert again
await help_test_reload_with_config(hass, caplog, tmp_path, config_manual)
assert _check_entities() == 2
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SENSOR])
@pytest.mark.parametrize(
"config_manual",
[
{"mqtt": {"sensor": []}},
{"mqtt": {"broker": "test"}},
],
)
async def test_setup_manual_entity_from_yaml(hass: HomeAssistant, config_manual):
"""Test setup with empty platform keys."""
await help_test_setup_manual_entity_from_yaml(hass, config_manual)