Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework MQTT config merging and adding defaults #90529

Merged
merged 7 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
10 changes: 5 additions & 5 deletions homeassistant/components/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
# Fetch configuration and add default values
hass_config = await conf_util.async_hass_config_yaml(hass)
mqtt_yaml = PLATFORM_CONFIG_SCHEMA_BASE(hass_config.get(DOMAIN, {}))
client = MQTT(hass, entry, conf)
if DOMAIN in hass.data:
mqtt_data = get_mqtt_data(hass)
mqtt_data.config = mqtt_yaml
mqtt_data.client = client
else:
hass.data[DATA_MQTT] = mqtt_data = MqttData(config=mqtt_yaml)
hass.data[DATA_MQTT] = mqtt_data = MqttData(config=mqtt_yaml, client=client)
client.start(mqtt_data)

await async_create_certificate_temp_files(hass, dict(entry.data))
mqtt_data.client = MQTT(hass, entry, conf)
# Restore saved subscriptions
if mqtt_data.subscriptions_to_restore:
mqtt_data.client.async_restore_tracked_subscriptions(
Expand Down Expand Up @@ -282,7 +284,7 @@ async def async_publish_service(call: ServiceCall) -> None:
)
return

assert mqtt_data.client is not None and msg_topic is not None
assert msg_topic is not None
await mqtt_data.client.async_publish(msg_topic, payload, qos, retain)

hass.services.async_register(
Expand Down Expand Up @@ -518,7 +520,6 @@ def unsubscribe() -> None:
def is_connected(hass: HomeAssistant) -> bool:
"""Return if MQTT client is connected."""
mqtt_data = get_mqtt_data(hass)
assert mqtt_data.client is not None
return mqtt_data.client.connected


Expand All @@ -536,7 +537,6 @@ async def async_remove_config_entry_device(
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload MQTT dump and publish service when the config entry is unloaded."""
mqtt_data = get_mqtt_data(hass)
assert mqtt_data.client is not None
mqtt_client = mqtt_data.client

# Unload publish and dump services.
Expand Down
24 changes: 13 additions & 11 deletions homeassistant/components/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
from .models import (
AsyncMessageCallbackType,
MessageCallbackType,
MqttData,
PublishMessage,
PublishPayloadType,
ReceiveMessage,
Expand Down Expand Up @@ -116,7 +117,6 @@ async def async_publish(
f"Cannot publish to topic '{topic}', MQTT is not enabled"
)
mqtt_data = get_mqtt_data(hass)
assert mqtt_data.client is not None
outgoing_payload = payload
if not isinstance(payload, bytes):
if not encoding:
Expand Down Expand Up @@ -167,7 +167,6 @@ async def async_subscribe(
f"Cannot subscribe to topic '{topic}', MQTT is not enabled"
)
mqtt_data = get_mqtt_data(hass)
assert mqtt_data.client is not None
# Support for a deprecated callback type was removed with HA core 2023.3.0
# The signature validation code can be removed from HA core 2023.5.0
non_default = 0
Expand Down Expand Up @@ -377,21 +376,18 @@ async def async_cleanup(self) -> None:
class MQTT:
"""Home Assistant MQTT client."""

_mqttc: mqtt.Client
_mqttc: mqtt.Client = None # type: ignore[assignment]
jbouwh marked this conversation as resolved.
Show resolved Hide resolved
_last_subscribe: float
_mqtt_data: MqttData

def __init__(
self,
hass: HomeAssistant,
config_entry: ConfigEntry,
conf: ConfigType,
self, hass: HomeAssistant, config_entry: ConfigEntry, conf: ConfigType
) -> None:
"""Initialize Home Assistant MQTT client."""
self._mqtt_data = get_mqtt_data(hass)

self.hass = hass
self.config_entry = config_entry
self.conf = conf

self._simple_subscriptions: dict[str, list[Subscription]] = {}
self._wildcard_subscriptions: list[Subscription] = []
self.connected = False
Expand All @@ -417,8 +413,6 @@ def ha_started(_: Event) -> None:

self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, ha_started)

self.init_client()

async def async_stop_mqtt(_event: Event) -> None:
"""Stop MQTT component."""
await self.async_disconnect()
Expand All @@ -427,6 +421,14 @@ async def async_stop_mqtt(_event: Event) -> None:
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, async_stop_mqtt)
)

def start(
self,
mqtt_data: MqttData,
) -> None:
"""Start Home Assistant MQTT client."""
self._mqtt_data = mqtt_data
self.init_client()

@property
def subscriptions(self) -> list[Subscription]:
"""Return the tracked subscriptions."""
Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/mqtt/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,6 @@ async def async_will_remove_from_hass(self) -> None:
def available(self) -> bool:
"""Return if the device is available."""
mqtt_data = get_mqtt_data(self.hass)
assert mqtt_data.client is not None
client = mqtt_data.client
if not client.connected and not self.hass.is_stopping:
return False
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/mqtt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ def write_state_request(self, entity: Entity) -> None:
class MqttData:
"""Keep the MQTT entry data."""

client: MQTT
config: ConfigType
client: MQTT | None = None
debug_info_entities: dict[str, EntityDebugInfo] = field(default_factory=dict)
debug_info_triggers: dict[tuple[str, str], TriggerDebugInfo] = field(
default_factory=dict
Expand Down