-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from azogue/fix/typing
🐛 Fix compatibility for Python < 3.10 and more typing
- Loading branch information
Showing
10 changed files
with
130 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,55 @@ | ||
from typing import Any, Callable, Optional | ||
import warnings | ||
from logging import Logger | ||
from typing import Any, Awaitable, Callable, Optional | ||
|
||
from gmqtt import Client as MQTTClient | ||
|
||
try: | ||
from uvicorn.config import logger | ||
|
||
log_info = logger | ||
except ImportError: | ||
import logging | ||
|
||
log_info = logging.getLogger() | ||
# client: MQTTClient, topic: str, payload: bytes, qos: int, properties: Any | ||
MQTTMessageHandler = Callable[[MQTTClient, str, bytes, int, Any], Awaitable[Any]] | ||
# client: MQTTClient, flags: int, rc: int, properties: Any | ||
MQTTConnectionHandler = Callable[[MQTTClient, int, int, Any], Any] | ||
|
||
|
||
class MQTTHandlers: | ||
def __init__(self, client: MQTTClient): | ||
def __init__(self, client: MQTTClient, logger: Logger): | ||
self._logger = logger | ||
self.client = client | ||
self.user_message_handler: Optional[Callable[..., Any]] = None | ||
self.user_connect_handler: Optional[Callable[..., Any]] = None | ||
self.user_message_handler: Optional[MQTTMessageHandler] = None | ||
self.user_connect_handler: Optional[MQTTConnectionHandler] = None | ||
|
||
def on_message(self, handler: Callable) -> Callable[..., Any]: | ||
log_info.info("on_message handler accepted") | ||
def on_message(self, handler: MQTTMessageHandler) -> MQTTMessageHandler: | ||
self._logger.info("on_message handler accepted") | ||
self.user_message_handler = handler | ||
return handler | ||
|
||
def on_subscribe(self, handler: Callable) -> Callable[..., Any]: | ||
""" | ||
Decorator method is used to obtain subscribed topics and properties. | ||
""" | ||
log_info.info("on_subscribe handler accepted") | ||
self._logger.info("on_subscribe handler accepted") | ||
self.client.on_subscribe = handler | ||
return handler | ||
|
||
def on_disconnect(self, handler: Callable) -> Callable[..., Any]: | ||
self.client.on_disconnect = handler | ||
return handler | ||
|
||
def on_connect(self, handler: Callable) -> Callable[..., Any]: | ||
log_info.info("on_connect handler accepted") | ||
def on_connect(self, handler: MQTTConnectionHandler) -> MQTTConnectionHandler: | ||
self._logger.info("on_connect handler accepted") | ||
self.user_connect_handler = handler | ||
return handler | ||
|
||
# TODO: Remove these unused properties on v3.0 | ||
@property | ||
def get_user_message_handler(self) -> Optional[Callable[..., Any]]: | ||
def get_user_message_handler(self) -> Optional[MQTTMessageHandler]: # pragma: no cover | ||
warnings.warn( | ||
"Deprecated property. Access to .user_message_handler", DeprecationWarning, stacklevel=1 | ||
) | ||
return self.user_message_handler | ||
|
||
@property | ||
def get_user_connect_handler(self) -> Optional[Callable[..., Any]]: | ||
def get_user_connect_handler(self) -> Optional[MQTTConnectionHandler]: # pragma: no cover | ||
warnings.warn( | ||
"Deprecated property. Access to .user_connect_handler", DeprecationWarning, stacklevel=1 | ||
) | ||
return self.user_connect_handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.