async def async_subscribe(hass: HomeAssistantType, topic: str, msg_callback: MessageCallbackType, qos: int = DEFAULT_QOS, encoding: str = 'utf-8'): """Subscribe to an MQTT topic. Call the return value to unsubscribe. """ # Count callback parameters which don't have a default value non_default = 0 if msg_callback: non_default = sum(p.default == inspect.Parameter.empty for _, p in inspect.signature(msg_callback).parameters.items()) wrapped_msg_callback = msg_callback # If we have 3 paramaters with no default value, wrap the callback if non_default == 3: _LOGGER.info( "Signature of MQTT msg_callback '%s.%s' is deprecated", inspect.getmodule(msg_callback).__name__, msg_callback.__name__) wrapped_msg_callback = wrap_msg_callback(msg_callback) async_remove = await hass.data[DATA_MQTT].async_subscribe( topic, catch_log_exception( wrapped_msg_callback, lambda msg: "Exception in {} when handling msg on '{}': '{}'".format( msg_callback.__name__, msg.topic, msg.payload)), qos, encoding) return async_remove
def async_dispatcher_connect(hass: HomeAssistantType, signal: str, target: Callable[..., Any]) -> Callable[[], None]: """Connect a callable function to a signal. This method must be run in the event loop. """ if DATA_DISPATCHER not in hass.data: hass.data[DATA_DISPATCHER] = {} if signal not in hass.data[DATA_DISPATCHER]: hass.data[DATA_DISPATCHER][signal] = [] wrapped_target = catch_log_exception( target, lambda *args: "Exception in {} when dispatching '{}': {}".format( target.__name__, signal, args)) hass.data[DATA_DISPATCHER][signal].append(wrapped_target) @callback def async_remove_dispatcher() -> None: """Remove signal listener.""" try: hass.data[DATA_DISPATCHER][signal].remove(wrapped_target) except (KeyError, ValueError): # KeyError is key target listener did not exist # ValueError if listener did not exist within signal _LOGGER.warning( "Unable to remove unknown dispatcher %s", target) return async_remove_dispatcher
async def async_subscribe(hass: HomeAssistantType, topic: str, msg_callback: MessageCallbackType, qos: int = DEFAULT_QOS, encoding: str = 'utf-8'): """Subscribe to an MQTT topic. Call the return value to unsubscribe. """ async_remove = await hass.data[DATA_MQTT].async_subscribe( topic, catch_log_exception( msg_callback, lambda topic, msg, qos: "Exception in {} when handling msg on '{}': '{}'".format( msg_callback.__name__, topic, msg)), qos, encoding) return async_remove
async def async_subscribe( hass: HomeAssistant, topic: str, msg_callback: AsyncMessageCallbackType | MessageCallbackType | DeprecatedMessageCallbackType | AsyncDeprecatedMessageCallbackType, qos: int = DEFAULT_QOS, encoding: str | None = "utf-8", ): """Subscribe to an MQTT topic. Call the return value to unsubscribe. """ # Count callback parameters which don't have a default value non_default = 0 if msg_callback: non_default = sum( p.default == inspect.Parameter.empty for _, p in inspect.signature(msg_callback).parameters.items()) wrapped_msg_callback = msg_callback # If we have 3 parameters with no default value, wrap the callback if non_default == 3: module = inspect.getmodule(msg_callback) _LOGGER.warning( "Signature of MQTT msg_callback '%s.%s' is deprecated", module.__name__ if module else "<unknown>", msg_callback.__name__, ) wrapped_msg_callback = wrap_msg_callback( cast(DeprecatedMessageCallbackType, msg_callback)) async_remove = await hass.data[DATA_MQTT].async_subscribe( topic, catch_log_exception( wrapped_msg_callback, lambda msg: (f"Exception in {msg_callback.__name__} when handling msg on " f"'{msg.topic}': '{msg.payload}'"), ), qos, encoding, ) return async_remove
async def async_subscribe( hass: HomeAssistantType, topic: str, msg_callback: MessageCallbackType, qos: int = DEFAULT_QOS, encoding: Optional[str] = "utf-8", ): """Subscribe to an MQTT topic. Call the return value to unsubscribe. """ # Count callback parameters which don't have a default value non_default = 0 if msg_callback: non_default = sum( p.default == inspect.Parameter.empty for _, p in inspect.signature(msg_callback).parameters.items() ) wrapped_msg_callback = msg_callback # If we have 3 parameters with no default value, wrap the callback if non_default == 3: _LOGGER.warning( "Signature of MQTT msg_callback '%s.%s' is deprecated", inspect.getmodule(msg_callback).__name__, msg_callback.__name__, ) wrapped_msg_callback = wrap_msg_callback(msg_callback) async_remove = await hass.data[DATA_MQTT].async_subscribe( topic, catch_log_exception( wrapped_msg_callback, lambda msg: "Exception in {} when handling msg on '{}': '{}'".format( msg_callback.__name__, msg.topic, msg.payload ), ), qos, encoding, ) return async_remove
async def async_subscribe( hass: HomeAssistantType, topic: str, msg_callback: MessageCallbackType, qos: int = DEFAULT_QOS, encoding: Optional[str] = "utf-8", ): """Subscribe to an MQTT topic. Call the return value to unsubscribe. """ async_remove = await hass.data[DATA_AMPIO][DATA_AMPIO_API].async_subscribe( topic, catch_log_exception( msg_callback, lambda msg: (f"Exception in {msg_callback.__name__} when handling msg on " f"'{msg.topic}': '{msg.payload}'"), ), qos, encoding, ) return async_remove