Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error running script #130

Open
vrm42 opened this issue Jun 22, 2020 · 25 comments
Open

error running script #130

vrm42 opened this issue Jun 22, 2020 · 25 comments

Comments

@vrm42
Copy link

vrm42 commented Jun 22, 2020

  • pySonoffLAN version: 1.1.4
  • Python version: 3.7, 3.8
  • Operating System: fedora22, armbian buster, centos 8

Description

installed software with
pip3 install pysonofflanr3
when I run it without options it runs but when I run it with any command I get
root@orangepipc:/usr/src/pysonofflan# pysonofflanr3 discover
2020-06-22 19:39:08,421 - info: Attempting to discover Sonoff LAN Mode devices on the local network, please wait...
Exception in thread zeroconf-ServiceBrowser__ewelink._tcp.local._3007312992:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 917, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.7/dist-packages/zeroconf/init.py", line 1679, in run
state_change=service_type_state_change[1],
File "/usr/local/lib/python3.7/dist-packages/zeroconf/init.py", line 1433, in fire
h(**kwargs)
File "/usr/local/lib/python3.7/dist-packages/zeroconf/init.py", line 1531, in on_change
listener.add_service(*args)
File "/usr/local/lib/python3.7/dist-packages/pysonofflanr3-1.1.4-py3.7.egg/pysonofflanr3/discover.py", line 40, in add_service
ip = utils.parseAddress(info.address) + ":" + str(info.port)
AttributeError: 'ServiceInfo' object has no attribute 'address'

What I Did

Tried to clone git, install it with
python3 setup.py install
but I get the same results

Thanks!
vrm

@ricardojlrufino
Copy link

Same error (firmware 3.4.1):
AttributeError: 'ServiceInfo' object has no attribute 'address'

A thing if device is not reporting
avahi-browse -a ↵ 1
+ wlp2s0 IPv4 eWeLink_XXXXX _ewelink._tcp local

dig eWeLink_XXXXX.local TXT

; <<>> DiG 9.11.3-1ubuntu1.12-Ubuntu <<>> eWeLink_XXXXX.local TXT
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 63340
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;eWeLink_XXXXX.local.      IN      TXT

;; Query time: 18 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Sun Jun 28 18:27:49 -03 2020
;; MSG SIZE  rcvd: 53

@vrm42
Copy link
Author

vrm42 commented Jun 30, 2020

My error log indicates Zeroconf as the source of the issue so I downgraded Zeroconf to 0.26.1 from 0.27.1 and now everything is fine. Hope it helps.

@Tahutipai
Copy link

Tahutipai commented Sep 30, 2020

My error log indicates Zeroconf as the source of the issue so I downgraded Zeroconf to 0.26.1 from 0.27.1 and now everything is fine. Hope it helps.

For those unsure how to downgrade, I had success with the following:
pip3 uninstall zeroconf ; pip3 install -Iv zeroconf==0.26.1

@mattmofr
Copy link

Hi everyone,
Downgrading zeroconf solves the issue. Discover works fine.
However when using --host with 'state' or 'on' functions, it returns

error : Missing api_key for encrypted device

Any idea on how to fix that ?

Thanks for your help

@fernand0
Copy link

fernand0 commented Dec 1, 2020

About zeroconf, when installing from pypi it downloads an old version of requirements and it doesn't work. Maybe it would be a good a idea make a 'release' to get the program working without having to downgrade zeroconf?

About missing api_key, you need to kwnow your api. An easy way is to put the sonoff in 'pairing' mode and to connect to http://10.10.7.1/device ... You'll get some parameters needed to run pysonofflanr3, which you can pass as command arguments.

@mattmofr
Copy link

mattmofr commented Dec 6, 2020

About zeroconf, when installing from pypi it downloads an old version of requirements and it doesn't work. Maybe it would be a good a idea make a 'release' to get the program working without having to downgrade zeroconf?

About missing api_key, you need to kwnow your api. An easy way is to put the sonoff in 'pairing' mode and to connect to http://10.10.7.1/device ... You'll get some parameters needed to run pysonofflanr3, which you can pass as command arguments.

Thanks @fernand0 !

Works great through command line.

I'm now trying to use the python3 library and I manage to use the code provided in the the Readme.rst.

However when I try to call the turn_on function, it doesn't seem to work...
FYI : I'm totally newbie with asyncio ;)

Here's my code:


import logging
from pysonofflanr3 import SonoffSwitch

logging.basicConfig(filename='sonoff_switch.log', filemode='w', level=logging.DEBUG, format='%(asctime)-15s (%(threadName)-9s) %(message)s',)

async def print_state_callback(device):
    if device.basic_info is not None:
        print("ON" if device.is_on else "OFF")
        if device.is_on:
            await device.turn_off
        else:
            print("Turning the switch on")
            logging.debug('Turning the switch on')
            await device.turn_on()
        device.shutdown_event_loop()

myDevice = SonoffSwitch(
    host="192.168.1.XX",
    callback_after_update=print_state_callback,
    api_key='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
)

Any idea on what I'm doing wrong ?

Thanks for your help.

Matt

@fernand0
Copy link

fernand0 commented Dec 7, 2020

I'm not familiar with asyncio but it seems to me that you are not making a call. Just in case it is of use for you, here you have my own code:

import configparser
import time
import sys
import logging
import os
import pysonofflanr3.cli


if __name__ == "__main__": 

    logging.basicConfig(stream=sys.stdout, 
            level=logging.INFO, 
            format='%(asctime)s %(message)s')

    HOME = os.path.expanduser("~")
    CONFIGDIR = f'{HOME}/.config'
    section = 'Estudio' # Some section

    config = configparser.ConfigParser()
    config.read(f'{CONFIGDIR}/configSonoff')

    api_key = config.get(section,'api_key')
    device_id = config.get(section,'device_id')
    host = config.get(section,'host')


    config = {'host':host,    'device_id':device_id, 'api_key':api_key}
    if len(sys.argv)>1:
       command = sys.argv[1] 
       pysonofflanr3.cli.switch_device(config, None, command)
    else:
       print("We need a command, changing the state")
       pysonofflanr3.cli.switch_device(config, None, "")

@mattmofr
Copy link

mattmofr commented Dec 7, 2020

Hi @fernand0 ,

Thanks for your code. I managed to make it work with your code !

In the first place I wanted to create an instance of SonoffSwitch and then use its functions to turn it on or off instead of calling the cli.switch_device() with the configuration of the SonoffSwitch as an argument.

But I guess I'm not expert enough in Python to understand how to do so.

Thanks again for your help.

Best regards,

Matt

@Masterz69
Copy link

Have any information - how and where to obtain api_key ?

@fernand0
Copy link

Hello,

you can obtain your parameters, for example, setting the device in pairing mode and then, you can connect to the access point of the device, and pointing your browser to http://10.10.7.1/device

@Masterz69
Copy link

Hello,

you can obtain your parameters, for example, setting the device in pairing mode and then, you can connect to the access point of the device, and pointing your browser to http://10.10.7.1/device

I've tried that way - but no one my laptop or mobile see device AP when in pairing mode (S26 with 3+ version SW).

Managed to intercept traffic using SSL proxy.
When using "apikey" from SSL traffic: Padding is incorrect.

@Pitj3
Copy link

Pitj3 commented Jan 22, 2021

Having the same issue as @Masterz69 . "error: Error updating service for device xxxxxxxxxx: Padding is incorrect. Probably wrong API key."

@dauheeIRL
Copy link

Having the same issue as @Masterz69 . "error: Error updating service for device xxxxxxxxxx: Padding is incorrect. Probably wrong API key."

did you get the api key?

@Pitj3
Copy link

Pitj3 commented Jan 22, 2021

Yeah I got the API Key from ewelinkapi

I have to add that I'm using a Sonoff TX Wi-Fi Smart Wall Switch and not a regular sonoff switch.

@Pitj3
Copy link

Pitj3 commented Jan 22, 2021

If I use the "devicekey" that is returned from ewelinkapi it does work, but I can only find 1 switch on my wall switch, even though it has 2 switches

Edit:
You can supply an outlet to your SonoffSwitch, it works now!

@dauheeIRL
Copy link

dauheeIRL commented Jan 22, 2021 via email

@Rain365
Copy link

Rain365 commented May 5, 2021

#130 (comment)

Hello,

mattmo92270 was on the good way.

I make some research about the littel part of they programm and i find the solution.

I see, the async def look like a while True. And if we wont, send a command to the light, we have to finish the cycle and stop the cycle.

async def print_state_callback(device):
    if device.basic_info is not None:
        print("ON" if device.is_on else "OFF")
        if device.is_on:
            await device.turn_off
        else:
            print("Turning the switch on")
            logging.debug('Turning the switch on')
            await device.turn_on()
        device.shutdown_event_loop()

for make that, i make this solution (i change the name of the function. but the result stay the same:

async def on_state_callback(device):
    if device.basic_info is not None:
        if device.is_on:
            device.shutdown_event_loop()
        elif device.is_off:
            await device.turn_on()

@JacekKac
Copy link

JacekKac commented Aug 3, 2022

my problems with running the script: raspian pi4b

pi@raspberrypi:~ $ /home/pi/.local/bin/pysonofflanr3 discover
Traceback (most recent call last):
File "/home/pi/.local/bin/pysonofflanr3", line 6, in
from pysonofflanr3.cli import cli
File "/home/pi/.local/lib/python2.7/site-packages/pysonofflanr3/init.py", line 57, in
from .client import SonoffLANModeClient
File "/home/pi/.local/lib/python2.7/site-packages/pysonofflanr3/client.py", line 44
host: str,
^
SyntaxError: invalid syntax

@fernand0
Copy link

fernand0 commented Aug 7, 2022

my problems with running the script: raspian pi4b

pi@raspberrypi:~ $ /home/pi/.local/bin/pysonofflanr3 discover Traceback (most recent call last): File "/home/pi/.local/bin/pysonofflanr3", line 6, in from pysonofflanr3.cli import cli File "/home/pi/.local/lib/python2.7/site-packages/pysonofflanr3/init.py", line 57, in from .client import SonoffLANModeClient File "/home/pi/.local/lib/python2.7/site-packages/pysonofflanr3/client.py", line 44 host: str, ^ SyntaxError: invalid syntax

Maybe the problem is that you are using python 2.7 instead of a modern 3.X version?

@JacekKac
Copy link

JacekKac commented Aug 8, 2022

it helped.
sudo update-alternatives --config python choosing python3, pip3 install and it's working

@fernand0
Copy link

fernand0 commented Aug 8, 2022

Nice!

@Elio-Chedid
Copy link

Having the same issue as @Masterz69 . "error: Error updating service for device xxxxxxxxxx: Padding is incorrect. Probably wrong API key."

I know I'm 3 years late but did you find the solution? I'm having the same problem.

@Masterz69
Copy link

Having the same issue as @Masterz69 . "error: Error updating service for device xxxxxxxxxx: Padding is incorrect. Probably wrong API key."

I know I'm 3 years late but did you find the solution? I'm having the same problem.

No. Not using anymore.

@dauheeIRL
Copy link

should work if API key is correct. Also I have a script that uses the guts of this project but I've updated to overcome a couple issues - this library is no longer maintained

@Elio-Chedid
Copy link

should work if API key is correct. Also I have a script that uses the guts of this project but I've updated to overcome a couple issues - this library is no longer maintained

I intercepted the request between ewelink and the device using SSL proxy and got the API key so it should be correct. Is there any python library I can use to communicate with sonoff pow elite through lan?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests