Sonoff /eWeLink component for original firmware

@Dino-Tech did you fixed your problem? If so, let us know what was the fix. thanks :slight_smile:

Thanks for following up with me. Haven’t sorted it out yet, but will post resolution here when resolved.

@peterbuga I also had the same issue… spent about 3 hours trying to fix this :rofl: yes i am a NOOOB.

But honestly brilliant work!! I am using sonos basic with firmware 2.6.0 and hassio 0.84.6 :slight_smile:

You’ve been great, I’ve tested 3 devices in their latest version available:

You’ve been great, I’ve tested 3 devices in their latest version available:
sonoff basic, sonoff ch4 pro R2, and Th10.

They work spectacularly !!!

@peterbuga Hey. Just to report that I have installed a couple of Sonoff POW switches and the on/off functions are working perfectly :slight_smile:
Running 2.61
Just a question. I think that to use the power usage sensor in the POW I need the latest version of the code. Last time you recommended I use the Websocket branch. Is that still the case or should I use the Master branch?
Thanks again for your work on this. Very very useful!

@Michaelrch I’ve sort of left websocket branch to deal with it a bit later and focused more on adding compatibility with more devices. I did merged the sensors into websocket but i never got the chance to test it, so if you want, you can give it a go and let me know too how it goes :blush: warning: you might get errors so make sure you make a backup of the current version you’ve got running now.

Thanks for the quick reply. That didn’t go quite to plan :expressionless:

I got this error on startup

2019-01-16 17:07:12 ERROR (MainThread) [homeassistant.components.switch] Error while setting up platform sonoff
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 124, in _async_setup_platform
    task = async_create_setup_task()
  File "/usr/local/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 89, in async_create_setup_task
    None, platform.setup_platform, hass, platform_config,
AttributeError: module 'custom_components.switch.sonoff' has no attribute 'setup_platform'

I’ll revert back for now I guess. I think I recall you saying that Websocket is better because it’s more responsive for state updates. Do I have that right? If so, I can report that it is very quick.

I don’t need the power reading from the Sonoff POW right now. I guess I’ll just wait for you to check it again. If you want it tested again, just let me know.
Thanks.

I’m having the exact same problem on a Orange Pi install in Armbian…so it is not macOS specific…

I would appreciate some guidance on what I should do to make it work? I have certifi already installed but I didn’t run it…maybe I need to, gonna try to find a solution… In which folder would I need to put the certificate?

Nice work! I think I can help you to expand your module.

I was curious if Ewelink on Android is based on the same API so I looked under the hood… turns out, they use React Native and we can access most of the source code. I only had a brief look, but it looks exactly like the protocol you’re implementing (although, for now, they obviously support more REST queries).

You can find the JavaScript file with the protocol here: https://gofile.io/?c=jCevO3

I think it might be even possible to RE their LAN mode (I think that once the device is in this mode, you can talk to it with a WebSocket, there is this URL in the source: “ws://” + n.deviceIP + “:8081” and they also implemented a method openLocalMode).

I will look more into this once I’m back from work, but it looks like it could be possible to duplicate most of Ewelink functionality.

@quarx well this is where i got some of the information, but as you can easily see it’s a real spaghetti code horror show :grin: it’s quite hard to figure out the WS messages so i preferred to sniff the app connection and deal with it from there (different devices have slightly different messages).

for me the local mode connection to the sonoff never worked, highly unreliable, so I dropped the idea of developing around this (i also think that it requires some kind of networking voodoo because in theory you first need to connect the devices to your account but after that you need to cut their internet connection to enter in local mode… if i’m right, it’s not very practical but feel free to give it a try! looking forward for it)

TBH I also never tried the LAN mode, because as far as I understand you need to disable WAN in order for it to start working.

However, I thought that maybe it’s just the way the app works (as long as you have access to the outside world it blocks local network discoverability so no device will show up on the list).

Ewelink has some instructions here: https://help.ewelink.cc/hc/en-us/articles/360007134171-LAN-Mode-Tutorial

Here is the interesting part: “When WiFi network restores, LAN mode won’t be disabled and you should be able to control the device.”
And another one: “When network status changes on your* cellphone *, LAN mode will be disabled automatically. You won’t be able to enter LAN mode again within a 5-minute heartbeat time.”

So maybe the “LAN mode” means access to LAN from the app. In theory, all the devices could be accessible, but the app just ignores them.

I will experiment with this… I want to use your code to trigger lighting with Xiaomi sensors, and the lower lag the better. I also want to stick with the original firmware to have access to the app (which with the recent update looks quite handsomely) and integrations. I think the Mi Home integration will be also available sooner than later and I’m curious to see if it will work with their gateway (Ewelink is already present on the list of third-party vendors in the Mi Home app)

@quarx Just FYI, I am doing exactly what you want to do and it’s very prompt already using Peter’s code. I have Xiaomi motion sensors and Sonoffs and Ikea Tradri lights being triggered off motion and they are both (Sonoff and TRÅDFRI) very quick to respond. The response time is around half a second, not more than a second.
FYI I am running the Websocket branch of the code.
HTH

@Michaelrch cool! Do you happen to know if the response time is mostly caused by the communication with the Ewelink server or Xiaomi adds up to it as well?

I’m working with an entirely new set-up (haven’t used HASS for a while now, but I will get back on board as soon as I get my new PI) and I still haven’t played with Xiaomi sensors. I decided to use Xiaomi devices mostly because I have Yeelights in my bedside lamps (the only place where I wanted to play with colors is my bedroom) and wanted to have a simple out-of-the-box solution to support physical switches (also their devices are simply pretty). That being said, I’m thinking about taking over the switches to control them on my own because of Mi Home issues (every now and then my automation stops working and I have to restart the Yeelights and disable/enable automation in the app to make it work again).

BTW anyone tried to RE Xiaomi gateway? I’m wondering if all the automation comes from a remote Chinese server or some of them are done locally.

@quarx I haven’t really tested it that systematically but FWIW the Sonoffs (web controlled) are just as quick to respond as the Ikea Tradfri lights (locally controlled via the Tradfri hub).

I only use the Xiaomi kit for sensors (motion, doors, temperature, light level, vibration) so I can’t comment on working with the Yeelights.

I have found the Xiaomi stuff pretty robust but it has dropped out once or twice in the last few months. I am using the China server because AFAIK you have to for the gateway and sensors.

I actually also have the Roborock 2 vacuum but that is on a EU server on a different account. I have my tablet logged into the EU account and my phone logged into the China account. Not ideal but no practical bother.

I just found this: https://bitekmindenhol.blog.hu/2018/08/31/sonoff_lan_uzemmod

Based on Google Translator it looks like in the LAN mode there is a UDP handshake and from there on all communication is based on the same protocol as when the app talks with the Ewelink server.

There is already a lan mode component for sonoff, its on the forum sonewhere.

For original firmware?

Yes, original f/ware.

1 Like
1 Like

Thanks, it’s a nice proof-of-concept!