krahabb / meross_lan

Home Assistant integration for Meross devices
MIT License
385 stars 46 forks source link

Meross Smart Switch mss315 doesn't update in Meross app or Apple Home when toggled on/off in Home Assistant #382

Closed AthNIC closed 1 month ago

AthNIC commented 5 months ago

Version of the custom_component

Meross LAN integration v4.5.3 Downloaded via HACS

HA versions: Core 2024.1.6 Supervisor 2023.12.1 Operating System 11.4 Frontend 20240104.0

Describe the bug

Toggle the switch in HomeKit or the Meross app and everything is in sync, including Home Assistant.

Toggle the switch in Home Assistant (using the Meross LAN _Outlet toggle) and it does not show the state change in HomeKit or the Meross app. Pulling down to refresh in the Meross app will then pull through the change. Restarting the Apple Home app makes no difference, it never sees the state change.

To further investigate this, I added the Matter integration and added this switch via Matter also. Switch via the Matter outlet and everything syncs instantly (except the Meross LAN _Outlet which catches up a few seconds later).

The Matter switch toggle in Home Assistant works flawlessly across all three (Home Assistant, Apple Home app and the Meross app) regardless of which app toggles from one moment to the next.

Toggle with the Meross LAN _Outlet and even the Matter switch in Home Assistant doesn't see the state change.

I notice from the logs that the Meross LAN integration appears to be going out to the Meross cloud for the switching. Perhaps this explains why a refresh in the Meross app will update the current state, but Apple Home app (and HA Matter switch) never sees it?

Note: My Meross mts200b Thermostat also behaves in a very similar way. Updates on Home Assistant don't show in Apple Home until the app is relaunched, and in the Meross app the page has to be pulled down to force a refresh. The thermostat itself receives the updates from Home Assistant promptly. And as above, between the Meross app and the Apple Home app there are no issues, one changes the other almost immediately.

Debug log (device ID stripped and just the lines for the individual device under test to keep it short)

This log output covers the following events:

2024-01-31 21:15:20.755 DEBUG (MainThread) [custom_components.meross_lan] MerossDevice(Smart Switch (---)): polling start
2024-01-31 21:15:20.755 DEBUG (MainThread) [custom_components.meross_lan] MerossDevice(Smart Switch (---)): polling end
2024-01-31 21:15:29.247 DEBUG (MainThread) [custom_components.meross_lan] MerossMQTTConnection(3665894:mqtt-eu-2.meross.com:443): MQTT RECV device_id:(---) method:(PUSH) namespace:(Appliance.Control.ToggleX)
2024-01-31 21:15:32.856 DEBUG (MainThread) [custom_components.meross_lan] MerossMQTTConnection(3665894:mqtt-eu-2.meross.com:443): MQTT RECV device_id:(---) method:(PUSH) namespace:(Appliance.Control.ToggleX)
2024-01-31 21:15:40.042 DEBUG (MainThread) [custom_components.meross_lan] MerossMQTTConnection(3665894:mqtt-eu-2.meross.com:443): MQTT RECV device_id:(---) method:(PUSH) namespace:(Appliance.Control.ToggleX)
2024-01-31 21:15:50.756 DEBUG (MainThread) [custom_components.meross_lan] MerossDevice(Smart Switch (---)): polling start
2024-01-31 21:15:50.756 DEBUG (MainThread) [custom_components.meross_lan] MerossDevice(Smart Switch (---)): polling end
2024-01-31 21:15:51.041 DEBUG (MainThread) [custom_components.meross_lan] MerossMQTTConnection(3665894:mqtt-eu-2.meross.com:443): MQTT RECV device_id:(---) method:(PUSH) namespace:(Appliance.Control.ToggleX)
2024-01-31 21:16:09.160 DEBUG (SyncWorker_13) [custom_components.meross_lan] MerossMQTTConnection(3665894:mqtt-eu-2.meross.com:443): MQTT PUBLISH device_id:(---) method:(SET) namespace:(Appliance.Control.ToggleX)
2024-01-31 21:16:09.328 DEBUG (MainThread) [custom_components.meross_lan] MerossMQTTConnection(3665894:mqtt-eu-2.meross.com:443): MQTT RECV device_id:(---) method:(SETACK) namespace:(Appliance.Control.ToggleX)
2024-01-31 21:16:20.757 DEBUG (MainThread) [custom_components.meross_lan] MerossDevice(Smart Switch (---)): polling start
2024-01-31 21:16:20.757 DEBUG (MainThread) [custom_components.meross_lan] MerossDevice(Smart Switch (---)): polling end
2024-01-31 21:16:23.252 DEBUG (SyncWorker_10) [custom_components.meross_lan] MerossMQTTConnection(3665894:mqtt-eu-2.meross.com:443): MQTT PUBLISH device_id:(---) method:(SET) namespace:(Appliance.Control.ToggleX)
2024-01-31 21:16:23.415 DEBUG (MainThread) [custom_components.meross_lan] MerossMQTTConnection(3665894:mqtt-eu-2.meross.com:443): MQTT RECV device_id:(---) method:(SETACK) namespace:(Appliance.Control.ToggleX)
krahabb commented 5 months ago

Hello @AthNIC, It's strange no one else raised the same issue since these 'homekit supported' devices are here since a long time. At any rate, this might be explained by the fact that meross_lan is using the 'cloud' mqtt protocol and the device firmware , maybe, is deciding that, since it receives commands through an internet connection (it considers it's cloud mqtt transport different from the local ones), it is better to not advertise state updates over the lan (and so the matter apps don't receive the immediate state update) On the other hand, when it receives a command locally (over matter), it also broadcasts the state update so that the LAN is updated always.

In this regard, meross_lan uses the 'legacy' stacks (MQTT through Meross cloud or direct HTTP) and doesn't 'sense' any matter status update (I guess matter protocol provides support for pushing instant state updates)

This way, meross_lan could sense instant state updates only when connected through MQTT. If not, the HTTP protocol, goes through timed poll queries

I don't really know, expecially since I'm almost totally ignorant about matter but, whatever, I don't think the issue lies in matter but in how the device fw decides to publish status updates.

From your analysis it really looks as if the MATTER stack is almost totally separated from the 'legacy' stacks in the device. This way, when you send commands through MATTER and the status changes, this is not propagated to the MQTT stack (so that meross_lan could receive the instant state update) The same applies in reverse, if you send commands through the legacy stacks (MQTT, HTTP which are those used by meross_lan) the device doesn't broadcast it's state change.

It would be nice to have this further investigated by other users too, so to discover if it is just a 'glitch' (but you have 2 different devices exposing the same behavior!) or if it is 'by design' i.e. the device stacks don't communicate between each other

Time to implement support for MATTER in meross_lan too....

AthNIC commented 5 months ago

Hi @krahabb

I'm at a loss to be honest. I'm very new to the whole smart home thing, only a couple of weeks in with a thermostat, some TRVs and a few smart plugs.

My original goal was to keep it simple, to which end I deliberately looked for HomeKit devices so they do not need an Internet connection in order to function. That's coming from a basic security standpoint, and also practical; if the Internet connection is ever offline, I don't want my home to be uncontrollable.

I ran up an instance of HAOS just out of curiosity because I'd seen a lot of noise about it online. I downloaded the VMware ESXi .ova file as my starting point. Steep learning curve coming from nothing, but I quickly saw the potential and over a week or so, got it to something useful, at least in terms of the nice ability to view history graphs for each TRV as well as the Meross thermostat and monitor batteries and WiFi levels etc. There's a lot of extra data and functionality in Home Assistant that isn't available in the Meross app - thanks in no small part to your hard work on the Meross_LAN integration - so thank you for this!

I also got smart meter DCC data coming in thanks to the Hildebrand Glow integration, and some random stuff like HP printer cartridge levels and broadband status etc.

Anyhow, I've spent the last day since your reply running up an instance of Kali Linux and attaching this to a switch port I can mirror traffic to from the rest of the network, then looking for the hardware address of a specific smart plug to see if I can find out what is (or more specifically isn't) happening. I didn't want to reply without trying to find out more for you.

Not really had much joy though sadly, because most of the data is obviously (and thankfully) encrypted and the network is so noisy, and with the HA polling etc, it's impossible to just monitor in real time as I push switches in various apps to see what happens.

I can obviously filter saved results, but it's almost impossible to see the wood from the trees when I've no idea what I'm really looking for.

I have found that Matter uses two ports: udp/5353 mDNS udp/5540 Matter

I can see encrypted messages happening. Very short and succinct compared to the IPv4 polling and controls from HA, which I can see in clear text.

All the IPv6 Matter commands run over fe80:: addresses, so they're all very much local and contained within the LAN, which is nice to confirm. Not sure how Matter keeps device status in sync on Apple Home and HA, but it appears the Meross app updates itself in combination with the Meross cloud somehow (and it's slightly slower than Apple Home / HA updates because of this).

Communications from the device to the Meross cloud are going to 54.195.70.227 port 443, which is an Amazon AWS instance in Ireland (the "local" UK AWS data centre I think). It's all [thankfully] TLSv1.2 encrypted though, so I've no idea about the payload.

The devices are also using MDNS (udp/5353) on the IPv4 network with respect to multicast.

I don't think there's anything here you probably don't already know, but that's as far as I've got with it.

I wish you all the best with any future Matter update to Meross_LAN - I wonder if it'll be possible for this to be interoperable with the existing HA Matter (Beta) integration? I imagine given the nature of Matter that should be fine by default (fingers crossed).

Thank you for the reply and your time, I do appreciate it.

krahabb commented 5 months ago

Thank you @AthNIC, To be honest, when Matter came to HA (and the Meross devices supporting it) I thought it would have put an end to meross_lan usefulness since, being the devices 'natively' supported by the HA core Matter component, all of the features of meross_lan would have become obsolete or just duplicate of an existing future-proof solution. Now, I've never digged into the Matter protocol and the support offered by HA and by Meross too since I thought (and still I think) that they would increasingly 'closed the gap' with the legacy Meross (cloud and non-cloud) features so I still don't know how mature they are. We'll see, what would be reasonable to do. Duplicating the HA Matter support would be a bit useless so to say but maybe there's still space to keep meross_lan improving 'side-to-side'.

In an ideal world, if the Matter features are mature enough, user of Meross devices should just drop meross_lan and use those since, as a matter of fact, it's the way to go in order to simplify the whole IoT-smarthome management.

Nevertheless, thank you for contributing, since this gives me precious insights about the 'state of the art' (and actual issues in Meross devices integration)

AthNIC commented 5 months ago

Hi @krahabb

Indeed, I can see how that would be a preferred outcome. Unfortunately at the moment, the core Matter component only allows the toggle of the switch on the mss315 smart plugs (the only Matter devices I currently have). The only way to see all the energy monitoring data these provide is via your Meross_LAN integration. I imagine this is because that data actually only exists in the Meross cloud for the longer term storage, and on the device itself in any given moment, and I'm not sure if that's shared (or even shareable at all) via Matter.

On the side note of the Thermostat's occasional transgressions, that is not a Matter device, it's a HomeKit device, though the glitches could be related. That said, I just tried it a few seconds ago and it's behaving very nicely right now - worst case an occasional pull down refresh in the HA or Meross apps to update the current setting value display, Apple Home was responding instantly, no matter where the input came from.

It seems we're at a very important moment in the story of these devices, and hopefully going forwards Matter will become the standard that enables everything to just work together seamlessly. We can live in hope anyway!

At the moment though, it's a mess. It took me a couple of months to try and understand what it all meant before I went off and purchased anything. Zigbee, Z-Wave, HomeKit, border routers, hubs, gateways, Matter, Thread, fabrics, etc. etc. It's truely mindblowing coming into all this with zero prior knowledge.

fuomag9 commented 4 months ago

@AthNIC one of the biggest issues at the moment is that matter does not currently support power monitoring on smart plugs (too hard to implement right?) so we're stuck at waiting for a revision PLUS a firmware update if any will ever come for ALL current devices. So meross_lan is the only way to read that data without using the cloud

AthNIC commented 4 months ago

@fuomag9 thank you for the information, that's interesting to know. Energy monitoring seems to be getting more common in devices and it's a fairly obvious thing to want to do in a smart home, so hopefully future updates in this regard to the Matter standard (and indeed corresponding firmwares) will eventually happen. Meanwhile, meross_lan is doing a very good job of exposing the available data to HA.