Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
41963ef
Processing yaml config through entry setup
jbouwh May 11, 2022
b95abc3
Setup all platforms
jbouwh May 11, 2022
fea7183
Update homeassistant/components/mqtt/__init__.py
jbouwh May 12, 2022
f5f1ff9
adjust mock_mqtt - reference config from cache
jbouwh May 12, 2022
a7da58c
Fix test config entry override
jbouwh May 12, 2022
84b30ed
Add tests yaml setup
jbouwh May 13, 2022
0dacd33
additional tests
jbouwh May 13, 2022
91ada97
Introduce PLATFORM_SCHEMA_MODERN
jbouwh May 16, 2022
5acbec1
recover temporary MQTT_BASE_PLATFORM_SCHEMA
jbouwh May 16, 2022
6d56d48
Allow extra key in light base schema, restore test
jbouwh May 16, 2022
4447887
Fix test for exception on platform key
jbouwh May 16, 2022
95e316c
One deprecation message per platform
jbouwh May 17, 2022
4b7855a
Remove deprecation checks from modern schema
jbouwh May 17, 2022
25caa55
Update homeassistant/components/mqtt/fan.py
jbouwh May 18, 2022
b9c1cbf
Update homeassistant/components/mqtt/fan.py
jbouwh May 18, 2022
a2aeb35
Update homeassistant/components/mqtt/light/__init__.py
jbouwh May 18, 2022
8a5c436
Update homeassistant/components/mqtt/light/__init__.py
jbouwh May 18, 2022
f2b3763
Update homeassistant/components/mqtt/light/schema_json.py
jbouwh May 18, 2022
183b032
Update homeassistant/components/mqtt/light/schema_template.py
jbouwh May 18, 2022
aac9ba2
Update homeassistant/components/mqtt/mixins.py
jbouwh May 18, 2022
4a5ccf8
rename validate_modern_schema
jbouwh May 18, 2022
a48ebbb
Do not fail platform if a single config is broken
jbouwh May 18, 2022
7d4cc03
Update homeassistant/components/mqtt/__init__.py
jbouwh May 18, 2022
17bf635
Fix tests on asserting log
jbouwh May 18, 2022
717aeae
Update log. Make helper transparant, remove patch
jbouwh May 19, 2022
911c934
Perform parallel processing
jbouwh May 19, 2022
b948ad2
Update tests/components/mqtt/test_init.py
jbouwh May 19, 2022
52bcc23
Apply suggestions from code review
jbouwh May 19, 2022
c08eed5
Update homeassistant/components/mqtt/mixins.py
jbouwh May 19, 2022
94e1c60
black
jbouwh May 19, 2022
a9f7ae8
Fix tests and add #new_format anchor
jbouwh May 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
CONF_TLS_VERSION,
CONF_TOPIC,
CONF_WILL_MESSAGE,
CONFIG_ENTRY_IS_SETUP,
DATA_CONFIG_ENTRY_LOCK,
DATA_MQTT_CONFIG,
DATA_MQTT_RELOAD_NEEDED,
DEFAULT_BIRTH,
Expand Down Expand Up @@ -171,7 +173,6 @@
Platform.VACUUM,
]


CLIENT_KEY_AUTH_MSG = (
"client_key and client_cert must both be present in "
"the MQTT broker configuration"
Expand All @@ -187,7 +188,11 @@
required=True,
)

CONFIG_SCHEMA_BASE = vol.Schema(
PLATFORM_CONFIG_SCHEMA_BASE = vol.Schema(
{vol.Optional(component.value): vol.Coerce(list) for component in PLATFORMS}
)

CONFIG_SCHEMA_BASE = PLATFORM_CONFIG_SCHEMA_BASE.extend(
{
vol.Optional(CONF_CLIENT_ID): cv.string,
vol.Optional(CONF_KEEPALIVE, default=DEFAULT_KEEPALIVE): vol.All(
Expand Down Expand Up @@ -774,6 +779,19 @@ async def finish_dump(_):
),
)

# setup platforms and discovery
hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock()
Comment thread
jbouwh marked this conversation as resolved.
hass.data[CONFIG_ENTRY_IS_SETUP] = set()

async with hass.data[DATA_CONFIG_ENTRY_LOCK]:
Comment thread
jbouwh marked this conversation as resolved.
for component in PLATFORMS:
config_entries_key = f"{component}.mqtt"
if config_entries_key not in hass.data[CONFIG_ENTRY_IS_SETUP]:
hass.data[CONFIG_ENTRY_IS_SETUP].add(config_entries_key)
hass.async_add_job(
Comment thread
jbouwh marked this conversation as resolved.
Outdated
hass.config_entries.async_forward_entry_setup(entry, component)
)

if conf.get(CONF_DISCOVERY):
await _async_setup_discovery(hass, conf, entry)

Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/mqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
CONF_TLS_INSECURE = "tls_insecure"
CONF_TLS_VERSION = "tls_version"

CONFIG_ENTRY_IS_SETUP = "mqtt_config_entry_is_setup"
DATA_CONFIG_ENTRY_LOCK = "mqtt_config_entry_lock"
DATA_MQTT_CONFIG = "mqtt_config"
DATA_MQTT_RELOAD_NEEDED = "mqtt_reload_needed"

Expand Down
6 changes: 2 additions & 4 deletions homeassistant/components/mqtt/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
ATTR_DISCOVERY_TOPIC,
CONF_AVAILABILITY,
CONF_TOPIC,
CONFIG_ENTRY_IS_SETUP,
DATA_CONFIG_ENTRY_LOCK,
DOMAIN,
)

Expand Down Expand Up @@ -62,8 +64,6 @@

ALREADY_DISCOVERED = "mqtt_discovered_components"
PENDING_DISCOVERED = "mqtt_pending_components"
CONFIG_ENTRY_IS_SETUP = "mqtt_config_entry_is_setup"
DATA_CONFIG_ENTRY_LOCK = "mqtt_config_entry_lock"
DATA_CONFIG_FLOW_LOCK = "mqtt_discovery_config_flow_lock"
DISCOVERY_UNSUBSCRIBE = "mqtt_discovery_unsubscribe"
INTEGRATION_UNSUBSCRIBE = "mqtt_integration_discovery_unsubscribe"
Expand Down Expand Up @@ -258,9 +258,7 @@ async def discovery_done(_):
hass, MQTT_DISCOVERY_DONE.format(discovery_hash), None
)

hass.data[DATA_CONFIG_ENTRY_LOCK] = asyncio.Lock()
hass.data[DATA_CONFIG_FLOW_LOCK] = asyncio.Lock()
hass.data[CONFIG_ENTRY_IS_SETUP] = set()

hass.data[ALREADY_DISCOVERED] = {}
hass.data[PENDING_DISCOVERED] = {}
Expand Down
11 changes: 9 additions & 2 deletions homeassistant/components/mqtt/fan.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from .mixins import (
MQTT_ENTITY_COMMON_SCHEMA,
MqttEntity,
async_get_platform_config_from_yaml,
async_setup_entry_helper,
async_setup_platform_helper,
)
Expand Down Expand Up @@ -201,7 +202,7 @@ async def async_setup_platform(
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up MQTT fan through configuration.yaml."""
"""Set up MQTT fan through configuration.yaml (deprecated)."""
Comment thread
jbouwh marked this conversation as resolved.
Outdated
await async_setup_platform_helper(
hass, fan.DOMAIN, config, async_add_entities, _async_setup_entity
)
Expand All @@ -212,7 +213,13 @@ async def async_setup_entry(
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up MQTT fan dynamically through MQTT discovery."""
"""Set up MQTT fan through configuration.yaml and dynamically through MQTT discovery."""
# load and initialize platform config from configuration.yaml
for config in await async_get_platform_config_from_yaml(
hass, fan.DOMAIN, PLATFORM_SCHEMA
):
await _async_setup_entity(hass, async_add_entities, config, config_entry)
Comment thread
jbouwh marked this conversation as resolved.
Outdated
# setup for discovery
setup = functools.partial(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
)
Expand Down
23 changes: 19 additions & 4 deletions homeassistant/components/mqtt/light/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import voluptuous as vol

from homeassistant.components import light
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

from ..mixins import async_setup_entry_helper, async_setup_platform_helper
from ..mixins import (
async_get_platform_config_from_yaml,
async_setup_entry_helper,
async_setup_platform_helper,
)
from .schema import CONF_SCHEMA, MQTT_LIGHT_SCHEMA_SCHEMA
from .schema_basic import (
DISCOVERY_SCHEMA_BASIC,
Expand Down Expand Up @@ -66,14 +71,24 @@ async def async_setup_platform(
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up MQTT light through configuration.yaml."""
"""Set up MQTT light through configuration.yaml (deprecated)."""
await async_setup_platform_helper(
hass, light.DOMAIN, config, async_add_entities, _async_setup_entity
)


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up MQTT light dynamically through MQTT discovery."""
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up MQTT light through configuration.yaml and dynamically through MQTT discovery."""
Comment thread
jbouwh marked this conversation as resolved.
Outdated
# load and initialize platform config from configuration.yaml
for config in await async_get_platform_config_from_yaml(
hass, light.DOMAIN, PLATFORM_SCHEMA
):
await _async_setup_entity(hass, async_add_entities, config, config_entry)
# setup for discovery
setup = functools.partial(
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
)
Expand Down
31 changes: 31 additions & 0 deletions homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import voluptuous as vol

from homeassistant.config import async_hass_config_yaml
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
ATTR_CONFIGURATION_URL,
Expand All @@ -23,6 +24,7 @@
CONF_ICON,
CONF_MODEL,
CONF_NAME,
CONF_PLATFORM,
CONF_UNIQUE_ID,
CONF_VALUE_TEMPLATE,
)
Expand Down Expand Up @@ -237,6 +239,35 @@ async def __call__(
"""Define setup_entities type."""


async def async_get_platform_config_from_yaml(
hass: HomeAssistant, domain: str, schema: vol.Schema
) -> list[ConfigType]:
"""Return a list of validated configurations for the platform read from configuration.yaml."""

def check_schema(config: dict, schema: vol.Schema) -> ConfigType:
"""Update the platform for schema compatibility and check the schema."""
if CONF_PLATFORM in config:
error_string = f"Invalid keyword 'platform' found, please remove it from your configuration: {json.dumps(config)}"
raise ValueError(error_string)
config[CONF_PLATFORM] = DOMAIN
Comment thread
jbouwh marked this conversation as resolved.
Outdated
return schema(config)

config_yaml = await async_hass_config_yaml(hass)
if not (integration_config := config_yaml.get(DOMAIN)) or not (
platform_configs := integration_config.get(domain)
):
return []
errors = []
try:
config = [check_schema(config, schema) for config in platform_configs]
except (ValueError, vol.MultipleInvalid) as exc:
errors.append(exc)
if errors:
raise vol.MultipleInvalid(errors)
Comment thread
jbouwh marked this conversation as resolved.
Outdated

return config


async def async_setup_entry_helper(hass, domain, async_setup, schema):
"""Set up entity, automation or tag creation dynamically through MQTT discovery."""

Expand Down