mirror of
https://github.com/home-assistant/core.git
synced 2025-07-30 18:58:04 +02:00
Move all mqtt callbacks to be global and also renamed with a better name.
This commit is contained in:
@ -12,7 +12,7 @@ from homeassistant.components.mqtt.subscription import (
|
||||
async_unsubscribe_topics,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.core import HomeAssistant, async_get_hass
|
||||
|
||||
from .const import (
|
||||
DEVICE_ALREADY_DISCOVERED,
|
||||
@ -23,17 +23,21 @@ from .const import (
|
||||
from .discovery import CreateDiscovery
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up PG LAB Electronics integration from a config entry."""
|
||||
|
||||
# define the call back for pglab module to publish a mqtt message
|
||||
async def mqtt_publish(topic: str, payload: str, qos: int, retain: bool) -> None:
|
||||
async def mqtt_publish_callback(
|
||||
topic: str, payload: str, qos: int, retain: bool
|
||||
) -> None:
|
||||
"""Define the call back for pglab module to publish a mqtt message."""
|
||||
hass = async_get_hass()
|
||||
await mqtt.async_publish(hass, topic, payload, qos, retain)
|
||||
|
||||
# define the call back for pglab module to subscribe to a mqtt message
|
||||
async def mqtt_subscribe(
|
||||
sub_state: Sub_State, topic: str, callback_func: Subcribe_CallBack
|
||||
) -> Sub_State:
|
||||
|
||||
async def mqtt_subscribe_callback(
|
||||
sub_state: Sub_State,
|
||||
topic: str,
|
||||
callback_func: Subcribe_CallBack,
|
||||
) -> Sub_State:
|
||||
"""Define the call back for pglab module to subscribe to a mqtt topic."""
|
||||
|
||||
async def discovery_message_received(msg: ReceiveMessage) -> None:
|
||||
callback_func(msg.topic, msg.payload)
|
||||
|
||||
@ -44,15 +48,25 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
}
|
||||
}
|
||||
|
||||
hass = async_get_hass()
|
||||
sub_state = async_prepare_subscribe_topics(hass, sub_state, topics)
|
||||
await async_subscribe_topics(hass, sub_state)
|
||||
return sub_state
|
||||
|
||||
async def mqtt_unsubscribe(sub_state: Sub_State) -> None:
|
||||
|
||||
async def mqtt_unsubscribe_callback(sub_state: Sub_State) -> None:
|
||||
"""Define the call back for pglab module to unsubscribe to a topic."""
|
||||
hass = async_get_hass()
|
||||
async_unsubscribe_topics(hass, sub_state)
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up PG LAB Electronics integration from a config entry."""
|
||||
|
||||
# create a mqtt client for pglab used for pglab python module
|
||||
pglab_mqtt = Client(mqtt_publish, mqtt_subscribe, mqtt_unsubscribe)
|
||||
pglab_mqtt = Client(
|
||||
mqtt_publish_callback, mqtt_subscribe_callback, mqtt_unsubscribe_callback
|
||||
)
|
||||
|
||||
# preparing the discovery module
|
||||
hass.data[DEVICE_ALREADY_DISCOVERED] = {}
|
||||
|
Reference in New Issue
Block a user