Sonoff /eWeLink component for original firmware

@Patrick123 I’m happy to let you know that I just finished a 90% implementation of the B1 a few days ago, try the sonoff-bulb branch and also take a look a few quirks still present here that i’m going to tackle them as different issues if i have someone’s eager to test them.

it looks like you have a veeeery old version of the component, you’ll definitely appreciate the speed of new one :sunglasses:

enjoy :wink:

Thank you for your prompt response Peter,

I downloaded it and copied it across. Looking at my logs, I get this message:

error from callback <bound method Sonoff.on_message of <custom_components.sonoff.Sonoff object at 0x6ff94c30>>: local variable 'state' referenced before assignment

Attempting to switch the light on, I get this error:

local variable 'params' referenced before assignment
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/websocket_api/commands.py", line 122, in handle_call_service
    connection.context(msg))
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1138, in async_call
    self._execute_service(handler, service_call))
  File "/usr/local/lib/python3.7/site-packages/homeassistant/core.py", line 1160, in _execute_service
    await handler.func(service_call)
  File "/usr/local/lib/python3.7/site-packages/homeassistant/components/light/__init__.py", line 285, in async_handle_light_on_service
    await light.async_turn_on(**pars)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/sonoff/light.py", line 220, in turn_on
    params.update({"state" : "on"})
UnboundLocalError: local variable 'params' referenced before assignment

FYI, a small typo line 338 in the init.py should be switches not switchs

EDIT
I solved the first issue by adding an “state = ‘off’” at line 348 of init.py
and the second issue by initialising params at line 110 of light.py to some arbitary value.

params = {
    "channel0": "202", # cool
    "channel1": "202", # white
    "channel2": "0", # red
    "channel3": "0", # green	
    "channel4": "0" # blue
}

@Patrick123 thanks for the reply!
as I kept mentioning, I don’t have the devices and pretty much everything i do is without any kind of testing. Clearly none of the guys who tested the B1 branch didn’t had a device that exposes more outlets (switches per device) and weird enough… none of them tested the case to simply just turn the light from off to on :smile: but if you turned on the device with an automation like brightness/color it would’ve turned on just fine.

i pushed (what i assume to be) a cleaner fix. can you please give it a try?

@peterbuga I understand that you are working blindly, but very impressive what you have done. All is working fine, HA is still complaining about the state variable not being set though.
Placed it into this light entity card and is working like a dream. Thanks again for your work. https://github.com/ljmerza/light-entity-card

Will continue testing tomorrow, the SO is demanding my attention.

@Patrick123 i think i fixed the ‘state’ variable but i’ve done no test to see if it actually works! for time being my sonoff devices are not available/accessible anymore. feel free to try it :grimacing:

@peterbuga, Looking good!, no complaints regarding the state, apart from the normal untested component complaint.

I also tested having the switch off, i.e. offline. Now I don’t know if it is related to you or to the Light component( the one I’m using: https://github.com/ljmerza/light-entity-card ) to indicate that the light/s are offline/unavailable.

So far absolutely no complaints! Thanks again.

As a test you could (re)start HA with the device offline, it should definitely be marked as “unavailable” by HA (if you don’t get this in your card, than this might be the problem, nonetheless the best way is to check the state in the devices list)

If you take a sonoff device offline while HA is running, from my previous tests it’ll take ~2 minutes before itead servers actually report that the device is actually offline, bottom line is that HA should report it unavailble after a while (probably some other improvements could be done to better detect the availability but i’m not going to dig to much into this right now).
You can easily test this with the app also, just unplug a device and no matter how many times you refresh the device it’ll show up online for a while. The trick comes when you try to toggle it and receives back an error and this forces itead servers to refresh the state of all your devices.
In a new version of component i’ve been working on, i’ve implemented something similar and basic to send a test-check on polling periods and see if they’re still online, but I’m not particular sure this is the right approach yet as it might cause to much stress on the websocket on particular huge number of devices.

Yes, in the states, it is marked unavailable, and the normal switch component shows that my other light is “unavailable”, so it is definitely the component, that is not indicating as such.

I am going to power it up now (take it online), and see if the state reflects accurately after 2 or 3 minutes.

Yes! I checked and the states now reflect on, (they are set to switch on by default if I lose Internet connectivity) and the component reflected as such.

I have to say, I’m starting to get a bit confused of your tests and what you are trying to achieve (or report as a bug) but unless it wasn’t clear until now this component works only with an active internet connection as well as any other sonoff device, therefore => no internet, no proper state reporting

Peter I think for selling new product to a hight price they will update only new devices so I think your best work for this beautiful custom component will be good for long time and I think you will able to do better this component don’t worry. If they want they do before a firmware as sonoff 3.0 but they didn’t want so when there was the bug to update with tasmota with wifi they updated so thank you again for your beautiful job

hi in 0.92 something changed please update i have this error message that in 0.91.4 i have not 2019-04-26 11:06:05 ERROR (Thread-22) [websocket] error from callback <bound method Sonoff.on_message of <custom_components.sonoff.Sonoff object at 0x7138d8f0>>: ‘NoneType’ object has no attribute ‘attributes’

Seeing these as well. But seems like switches are working normally.

@Hdjweb @forums2012 someone else reported this recently here https://github.com/peterbuga/HASS-sonoff-ewelink/issues/100 it would help me if someone could provide the longer HA error log not just the message displayed in UI

Can I say a big thank you for this, I’m new to HA after using IFTTT and Stringify for a few months I came to HA when Stringify anounced it was shutting down. The fact that this addon still uses the Sonoff cloud suits me at this time, I also have smart life devices that also require the cloud.

I’ve never got round to flashing my sonoff devices and the other sonoff addon for HA looked a bit complicated for me as a newbie so @peterbuga your addon is perfect for me.

Thanks

1 Like

How can I provide the longer error log? I have been using HA for a while but I still don’t know how to do some very basic things, hehe.
This is the error I get:
2019-04-27 01:52:47 ERROR (Thread-2) [websocket] error from callback <bound method Sonoff.on_message of <custom_components.sonoff.Sonoff object at 0x72a96570>>: not all arguments converted during string formatting

add this to configuration.yaml and restart HA

logger:
  default: warning
  logs:
    custom_components.sonoff: debug
    custom_components.sonoff.switch: debug
    custom_components.sonoff.light: debug
    custom_components.sonoff.sensor: debug

ps: the last 3 entries might not work, remove them if they throw further errors, i haven’t test them yet :upside_down_face:

Ok full log is:

2019-05-03 12:45:00 ERROR (Thread-22) [websocket] error from callback <bound method Sonoff.on_message of <custom_components.sonoff.Sonoff object at 0x6924aa70>>: ‘NoneType’ object has no attribute ‘attributes’
2019-05-03 12:45:02 WARNING (MainThread) [homeassistant.setup] Setup of group is taking over 10 seconds.
2019-05-03 12:45:02 ERROR (Thread-22) [websocket] error from callback <bound method Sonoff.on_message of <custom_components.sonoff.Sonoff object at 0x6924aa70>>: ‘NoneType’ object has no attribute ‘attributes’
2019-05-03 12:45:05 ERROR (Thread-22) [websocket] error from callback <bound method Sonoff.on_message of <custom_components.sonoff.Sonoff object at 0x6924aa70>>: ‘NoneType’ object has no attribute ‘attributes’
2019-05-03 12:45:06 ERROR (Thread-22) [websocket] error from callback <bound method Sonoff.on_message of <custom_components.sonoff.Sonoff object at 0x6924aa70>>: ‘NoneType’ object has no attribute ‘attributes’
2019-05-03 12:45:07 ERROR (Thread-22) [websocket] error from callback <bound method Sonoff.on_message of <custom_components.sonoff.Sonoff object at 0x6924aa70>>: ‘NoneType’ object has no attribute ‘attributes’
2019-05-03 12:45:26 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 241, in _step
result = coro.throw(exc)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/device_tracker/init.py”, line 352, in async_update_config
dev_id, device)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/device_tracker/init.py”, line 742, in update_config
with open(path, ‘a’) as out:
PermissionError: [Errno 13] Permission denied: ‘/home/homeassistant/.homeassistant/known_devices.yaml’
2019-05-03 12:45:26 ERROR (MainThread) [homeassistant.core] Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “/usr/lib/python3.5/asyncio/tasks.py”, line 241, in _step
result = coro.throw(exc)
File “/srv/homeassistant/lib/python3.5/site-packages/homeassistant/components/device_tracker/init.py”, line 352, in async_update_config
dev_id, device)
File “/usr/lib/python3.5/asyncio/futures.py”, line 380, in iter
yield self # This tells Task to wait for completion.
File “/usr/lib/python3.5/asyncio/tasks.py”, line 304, in _wakeup
future.result()
File “/usr/lib/python3.5/asyncio/futures.py”, line 293, in result
raise self._exception
File “/usr/lib/python3.5/concurrent/futures/thread.py”, line 55, in run
result = self.fn(*self.args, **self.kwargs)

If you want I put logger in configuration.yaml and I have other message do you want to analyze???I can see that there is a problem with api key

And this is my log.

2019-05-03 17:04:42 DEBUG (MainThread) [custom_components.sonoff] Create the main object
2019-05-03 17:04:44 INFO (MainThread) [custom_components.sonoff] Found websocket address: eu-pconnect2.coolkit.cc
2019-05-03 17:04:44 DEBUG (Thread-2) [custom_components.sonoff] (re)init websocket
2019-05-03 17:04:44 DEBUG (Thread-2) [custom_components.sonoff] websocket msg: {"error":0,"apikey":"0099a1eb-xxxx-XXXX-9ade-fe34ed876f4b","config":{"hb":1,"hbInterval":145},"sequence":"15568958846129222"}