mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 03:37:07 +00:00
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
This commit is contained in:
parent
1c4ba61725
commit
33ede351f0
@ -290,24 +290,6 @@ class SetupEntity(Protocol):
|
|||||||
"""Define setup_entities type."""
|
"""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(
|
async def async_setup_entry_helper(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
domain: str,
|
domain: str,
|
||||||
@ -359,14 +341,7 @@ async def async_setup_entry_helper(
|
|||||||
return
|
return
|
||||||
if domain not in config_yaml:
|
if domain not in config_yaml:
|
||||||
return
|
return
|
||||||
await asyncio.gather(
|
await asyncio.gather(*[async_setup(config) for config in config_yaml[domain]])
|
||||||
*[
|
|
||||||
async_setup(config)
|
|
||||||
for config in await async_get_platform_config_from_yaml(
|
|
||||||
hass, domain, config_yaml
|
|
||||||
)
|
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
# discover manual configured MQTT items
|
# discover manual configured MQTT items
|
||||||
mqtt_data.reload_handlers[domain] = _async_setup_entities
|
mqtt_data.reload_handlers[domain] = _async_setup_entities
|
||||||
@ -800,6 +775,7 @@ class MqttDiscoveryDeviceUpdate(ABC):
|
|||||||
self.hass, self._device_id, self._config_entry_id
|
self.hass, self._device_id, self._config_entry_id
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
async def async_update(self, discovery_data: MQTTDiscoveryPayload) -> None:
|
async def async_update(self, discovery_data: MQTTDiscoveryPayload) -> None:
|
||||||
"""Handle the update of platform specific parts, extend to the platform."""
|
"""Handle the update of platform specific parts, extend to the platform."""
|
||||||
|
|
||||||
|
@ -3062,3 +3062,16 @@ async def test_link_config_entry(hass, tmp_path, caplog):
|
|||||||
# reload manual configured items and assert again
|
# reload manual configured items and assert again
|
||||||
await help_test_reload_with_config(hass, caplog, tmp_path, config_manual)
|
await help_test_reload_with_config(hass, caplog, tmp_path, config_manual)
|
||||||
assert _check_entities() == 2
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user