Move all mqtt callbacks to be global and also renamed with a better name.
parent
d0962407b7
commit
d94d8010d5
|
@ -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,36 +23,50 @@ from .const import (
|
|||
from .discovery import CreateDiscovery
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
topics = {
|
||||
"pglab_subscribe_topic": {
|
||||
"topic": topic,
|
||||
"msg_callback": discovery_message_received,
|
||||
}
|
||||
}
|
||||
|
||||
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_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."""
|
||||
|
||||
# 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:
|
||||
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 discovery_message_received(msg: ReceiveMessage) -> None:
|
||||
callback_func(msg.topic, msg.payload)
|
||||
|
||||
topics = {
|
||||
"pglab_subscribe_topic": {
|
||||
"topic": topic,
|
||||
"msg_callback": discovery_message_received,
|
||||
}
|
||||
}
|
||||
|
||||
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_unsubscribe_topics(hass, sub_state)
|
||||
|
||||
# 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] = {}
|
||||
|
|
Loading…
Reference in New Issue