Add MQTT integration discovery (#41332)
* Add MQTT integration discovery * Add script/hassfest/mqtt.py * Unsubscribe if config entry exists * Add homeassistant/generated/mqtt.py * Fix bad loop * Improve tests * Improve tests * Apply suggestions from code review Co-authored-by: Fabian Affolter <mail@fabian-affolter.ch> * Prevent initiating multiple config flows Co-authored-by: Fabian Affolter <mail@fabian-affolter.ch>
This commit is contained in:
1 parent
3f263d5cbe
commit
343e5d64b8
21 files changed
+400
-24
No files matched your search
@@ -11,6 +11,7 @@ from . import (
|
||||
dependencies,
|
||||
json,
|
||||
manifest,
|
||||
mqtt,
|
||||
requirements,
|
||||
services,
|
||||
ssdp,
|
||||
@@ -25,6 +26,7 @@ INTEGRATION_PLUGINS = [
|
||||
config_flow,
|
||||
dependencies,
|
||||
manifest,
|
||||
mqtt,
|
||||
services,
|
||||
ssdp,
|
||||
translations,
|
||||
|
||||
@@ -33,6 +33,11 @@ def validate_integration(config: Config, integration: Integration):
|
||||
"config_flow",
|
||||
"HomeKit information in a manifest requires a config flow to exist",
|
||||
)
|
||||
if integration.manifest.get("mqtt"):
|
||||
integration.add_error(
|
||||
"config_flow",
|
||||
"MQTT information in a manifest requires a config flow to exist",
|
||||
)
|
||||
if integration.manifest.get("ssdp"):
|
||||
integration.add_error(
|
||||
"config_flow",
|
||||
@@ -51,6 +56,7 @@ def validate_integration(config: Config, integration: Integration):
|
||||
"async_step_discovery" in config_flow
|
||||
or "async_step_hassio" in config_flow
|
||||
or "async_step_homekit" in config_flow
|
||||
or "async_step_mqtt" in config_flow
|
||||
or "async_step_ssdp" in config_flow
|
||||
or "async_step_zeroconf" in config_flow
|
||||
)
|
||||
@@ -91,6 +97,7 @@ def generate_and_validate(integrations: Dict[str, Integration], config: Config):
|
||||
if not (
|
||||
integration.manifest.get("config_flow")
|
||||
or integration.manifest.get("homekit")
|
||||
or integration.manifest.get("mqtt")
|
||||
or integration.manifest.get("ssdp")
|
||||
or integration.manifest.get("zeroconf")
|
||||
):
|
||||
|
||||
@@ -38,6 +38,7 @@ MANIFEST_SCHEMA = vol.Schema(
|
||||
vol.Required("domain"): str,
|
||||
vol.Required("name"): str,
|
||||
vol.Optional("config_flow"): bool,
|
||||
vol.Optional("mqtt"): [str],
|
||||
vol.Optional("zeroconf"): [
|
||||
vol.Any(
|
||||
str,
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Generate MQTT file."""
|
||||
from collections import defaultdict
|
||||
import json
|
||||
from typing import Dict
|
||||
|
||||
from .model import Config, Integration
|
||||
|
||||
BASE = """
|
||||
\"\"\"Automatically generated by hassfest.
|
||||
|
||||
To update, run python3 -m script.hassfest
|
||||
\"\"\"
|
||||
|
||||
# fmt: off
|
||||
|
||||
MQTT = {}
|
||||
""".strip()
|
||||
|
||||
|
||||
def generate_and_validate(integrations: Dict[str, Integration]):
|
||||
"""Validate and generate MQTT data."""
|
||||
|
||||
data = defaultdict(list)
|
||||
|
||||
for domain in sorted(integrations):
|
||||
integration = integrations[domain]
|
||||
|
||||
if not integration.manifest:
|
||||
continue
|
||||
|
||||
mqtt = integration.manifest.get("mqtt")
|
||||
|
||||
if not mqtt:
|
||||
continue
|
||||
|
||||
for topic in mqtt:
|
||||
data[domain].append(topic)
|
||||
|
||||
return BASE.format(json.dumps(data, indent=4))
|
||||
|
||||
|
||||
def validate(integrations: Dict[str, Integration], config: Config):
|
||||
"""Validate MQTT file."""
|
||||
mqtt_path = config.root / "homeassistant/generated/mqtt.py"
|
||||
config.cache["mqtt"] = content = generate_and_validate(integrations)
|
||||
|
||||
if config.specific_integrations:
|
||||
return
|
||||
|
||||
with open(str(mqtt_path)) as fp:
|
||||
if fp.read().strip() != content:
|
||||
config.add_error(
|
||||
"mqtt",
|
||||
"File mqtt.py is not up to date. Run python3 -m script.hassfest",
|
||||
fixable=True,
|
||||
)
|
||||
return
|
||||
|
||||
|
||||
def generate(integrations: Dict[str, Integration], config: Config):
|
||||
"""Generate MQTT file."""
|
||||
mqtt_path = config.root / "homeassistant/generated/mqtt.py"
|
||||
with open(str(mqtt_path), "w") as fp:
|
||||
fp.write(f"{config.cache['mqtt']}\n")
|
||||
Reference in new issue
Block a user