Skip to content

Commit

Permalink
implement local MQTT broker init/pairing sessions (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
krahabb committed Dec 16, 2023
1 parent ec4f1a0 commit 21bde26
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
4 changes: 4 additions & 0 deletions custom_components/meross_lan/devices/mss.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ async def async_shutdown(self):
await super().async_shutdown()
self._sensor_consumption = None # type: ignore

def _handle_Appliance_Control_ConsumptionConfig(self, header: dict, payload: dict):
# processed at the MQTTConnection message handling
pass

def _handle_Appliance_Control_ConsumptionX(self, header: dict, payload: dict):
_sensor_consumption = self._sensor_consumption
# we'll look through the device array values to see
Expand Down
17 changes: 6 additions & 11 deletions custom_components/meross_lan/meross_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,10 @@ def _handle_generic_array(
key = get_namespacekey(header[mc.KEY_NAMESPACE])
self._parse__array(key, payload[key])

def _handle_Appliance_Control_Bind(self, header: dict, payload: dict):
# processed at the MQTTConnection message handling
pass

def _handle_Appliance_System_Ability(self, header: dict, payload: dict):
# This is only requested when we want to update a config_entry due
# to a detected fw change or whatever...
Expand Down Expand Up @@ -1030,17 +1034,8 @@ def _handle_Appliance_System_DNDMode(self, header: dict, payload: dict):
self.entity_dnd.update_onoff(payload[mc.KEY_DNDMODE][mc.KEY_MODE])

def _handle_Appliance_System_Clock(self, header: dict, payload: dict):
# this is part of initial flow over MQTT
# we'll try to set the correct time in order to avoid
# having NTP opened to setup the device
# Note: I actually see this NS only on mss310 plugs
# (msl120j bulb doesnt have it)
if self.mqtt_locallyactive and (header[mc.KEY_METHOD] == mc.METHOD_PUSH):
self.mqtt_request(
mc.NS_APPLIANCE_SYSTEM_CLOCK,
mc.METHOD_PUSH,
{mc.KEY_CLOCK: {mc.KEY_TIMESTAMP: int(time())}},
)
# processed at the MQTTConnection message handling
pass

def _handle_Appliance_System_Time(self, header: dict, payload: dict):
if header[mc.KEY_METHOD] == mc.METHOD_PUSH:
Expand Down
74 changes: 52 additions & 22 deletions custom_components/meross_lan/meross_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,28 +362,58 @@ async def async_mqtt_message(self, msg):
header,
payload,
)
else:
if (
(namespace == mc.NS_APPLIANCE_CONTROL_BIND)
and (method == mc.METHOD_SET)
and self.allow_mqtt_publish
):
# this transaction appears when a device (firstly)
# connects to an MQTT broker and tries to 'register'
# itself. Our guess right now is to just SETACK
# trying fix #346. We should maybe be careful
# if this connection is a cloud one but I guess
# the Meross cloud brokers are already managing
# and filtering out this message
await self.async_mqtt_publish(
device_id,
namespace,
mc.METHOD_SETACK,
{},
self.profile.key,
None,
messageid,
)
elif self.id is CONF_PROFILE_ID_LOCAL:
# special processing for local broker
# this code is experimental and is needed to give
# our broker some transaction management for devices
# trying to bind to non-Meross MQTT brokers
if method == mc.METHOD_PUSH:
if namespace == mc.NS_APPLIANCE_CONTROL_CONSUMPTIONCONFIG:
# this message too is published by mss switches
# and it appears newer mss315 could abort their connection
# if not replied (see #346)
await self.async_mqtt_publish(
device_id,
namespace,
method,
payload,
self.profile.key,
None,
messageid,
)
elif namespace == mc.NS_APPLIANCE_SYSTEM_CLOCK:
# this is part of initial flow over MQTT
# we'll try to set the correct time in order to avoid
# having NTP opened to setup the device
# Note: I actually see this NS only on mss310 plugs
# (msl120j bulb doesnt have it)
await self.async_mqtt_publish(
device_id,
namespace,
method,
{mc.KEY_CLOCK: {mc.KEY_TIMESTAMP: int(time())}},
self.profile.key,
None,
messageid,
)
elif method == mc.METHOD_SET:
if namespace == mc.NS_APPLIANCE_CONTROL_BIND:
# this transaction appears when a device (firstly)
# connects to an MQTT broker and tries to 'register'
# itself. Our guess right now is to just SETACK
# trying fix #346. We should maybe be careful
# if this connection is a cloud one but I guess
# the Meross cloud brokers are already managing
# and filtering out this message
await self.async_mqtt_publish(
device_id,
namespace,
mc.METHOD_SETACK,
{},
self.profile.key,
None,
messageid,
)

if device := ApiProfile.devices.get(device_id):
if device._mqtt_connection == self:
Expand Down

0 comments on commit 21bde26

Please sign in to comment.