Skip to content

Commit

Permalink
Add manager option to override mqtt server host/port target
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogeniola committed Jun 6, 2022
1 parent 7b89677 commit c02b8ff
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.4.6
0.4.4.7
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,14 @@ Anyway, feel free to contribute via donations!
</p>

## Changelog
#### 0.4.4.5
- Added Content-Type header to API calls
#### 0.4.4.7
- Added manager options to override default mqtt server host/port connection info.

<details>
<summary>Older</summary>
#### 0.4.4.5
- Added Content-Type header to API calls

#### 0.4.4.4
- Added support for MSG200

Expand Down
16 changes: 12 additions & 4 deletions meross_iot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
build_meross_device_from_known_types,
)
from meross_iot.http_api import MerossHttpClient
from meross_iot.model.constants import DEFAULT_MQTT_PORT, DEFAULT_COMMAND_TIMEOUT
from meross_iot.model.constants import DEFAULT_COMMAND_TIMEOUT, DEFAULT_MQTT_PORT
from meross_iot.model.enums import Namespace, OnlineStatus
from meross_iot.model.exception import (
CommandTimeoutError,
Expand Down Expand Up @@ -81,6 +81,7 @@ def __init__(
mqtt_skip_cert_validation: bool = False,
ca_cert: Optional[str] = None,
loop: Optional[AbstractEventLoop] = None,
mqtt_override_server: Optional[Tuple[str, int]] = None,
*args,
**kwords,
) -> None:
Expand All @@ -95,6 +96,9 @@ def __init__(
:param ca_cert: (Optional) Path to the PEM certificate to trust (Intermediate/CA)
:param loop: (Optional) Asyncio loop to use
:param args:
:param mqtt_override_server: (Optional) Tuple (hostname, port) of the MQTT server to use for MQTT connection.
When None (default), the hostname will be extracted by the domain/reserved domain
obtained via HTTP API, and port 443 will be used.
:param kwords:
"""

Expand Down Expand Up @@ -127,6 +131,7 @@ def __init__(
user_id=self._cloud_creds.user_id, app_id=self._app_id
)
self._user_topic = build_client_user_topic(user_id=self._cloud_creds.user_id)
self._override_mqtt_server = mqtt_override_server


def _get_client_from_domain_port(self, client: mqtt.Client) -> Tuple[Optional[str], Optional[int]]:
Expand Down Expand Up @@ -412,15 +417,13 @@ async def _async_enroll_new_http_dev(
device = None
abilities = None
if device_info.online_status == OnlineStatus.ONLINE:
mqtt_domain = extract_domain(device_info.domain)

try:
res_abilities = await self.async_execute_cmd(
destination_device_uuid=device_info.uuid,
method="GET",
namespace=Namespace.SYSTEM_ABILITY,
payload={},
mqtt_hostname=mqtt_domain,
mqtt_hostname=extract_domain(device_info.domain),
mqtt_port=DEFAULT_MQTT_PORT
)
abilities = res_abilities.get("ability")
Expand Down Expand Up @@ -766,6 +769,11 @@ async def async_execute_cmd(
:return:
"""
# Retrieve the mqtt client for the given domain:port broker
if self._override_mqtt_server is not None:
_LOGGER.debug("Overriding MQTT host/port as per manager parameter")
mqtt_hostname = self._override_mqtt_server[0]
mqtt_port = self._override_mqtt_server[1]

client = await self._async_get_create_mqtt_client(domain=mqtt_hostname, port=mqtt_port)
return await self.async_execute_cmd_client(client=client,
destination_device_uuid=destination_device_uuid,
Expand Down
15 changes: 1 addition & 14 deletions meross_iot/model/http/device.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import json
import logging
from datetime import datetime
from typing import Union, List

from meross_iot.model.constants import DEFAULT_MQTT_HOST, DEFAULT_MQTT_PORT
from meross_iot.model.enums import OnlineStatus
from meross_iot.model.shared import BaseDictPayload
import json


_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -63,17 +61,6 @@ def __init__(self,
self.domain = domain
self.reserved_domain = reserved_domain

@property
def mqtt_host(self):
if self.domain is not None:
return self.domain
if self.reserved_domain is not None:
return self.reserved_domain
return DEFAULT_MQTT_HOST

@property
def mqtt_port(self):
return DEFAULT_MQTT_PORT

def __repr__(self):
return json.dumps(self.__dict__, default=lambda x: x.isoformat() if isinstance(x,datetime) else x.name if(isinstance(x,OnlineStatus)) else "NOT-SERIALIZABLE")
Expand Down
4 changes: 2 additions & 2 deletions utilities/meross_sniffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from meross_iot.manager import MerossManager
from meross_iot.model.enums import Namespace, OnlineStatus
from meross_iot.utilities.mqtt import build_device_request_topic, build_client_response_topic, build_client_user_topic

from meross_iot.utilities.network import extract_domain

SNIFF_LOG_FILE = 'sniff.log'
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Expand Down Expand Up @@ -193,7 +193,7 @@ async def _main():
hashed_password,
selected_device.uuid,
ca_cert=None,
mqtt_host=selected_device.domain or "iot.meross.com"
mqtt_host=extract_domain(selected_device.domain) or "iot.meross.com"
)

print("Starting the sniffer...")
Expand Down

0 comments on commit c02b8ff

Please sign in to comment.