pysonofflan icon indicating copy to clipboard operation
pysonofflan copied to clipboard

error running script

Open vrm42 opened this issue 4 years ago • 25 comments

  • 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

vrm42 avatar Jun 22 '20 19:06 vrm42

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

ricardojlrufino avatar Jun 28 '20 21:06 ricardojlrufino

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.

vrm42 avatar Jun 30 '20 07:06 vrm42

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

Tahutipai avatar Sep 30 '20 22:09 Tahutipai

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

mattmofr avatar Nov 27 '20 23:11 mattmofr

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.

fernand0 avatar Dec 01 '20 16:12 fernand0

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

mattmofr avatar Dec 06 '20 22:12 mattmofr

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, "")

fernand0 avatar Dec 07 '20 10:12 fernand0

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

mattmofr avatar Dec 07 '20 11:12 mattmofr

Have any information - how and where to obtain api_key ?

Masterz69 avatar Jan 09 '21 19:01 Masterz69

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

fernand0 avatar Jan 10 '21 11:01 fernand0

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.

Masterz69 avatar Jan 10 '21 18:01 Masterz69

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

Pitj3 avatar Jan 22 '21 19:01 Pitj3

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?

dauheeIRL avatar Jan 22 '21 20:01 dauheeIRL

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 avatar Jan 22 '21 20:01 Pitj3

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!

Pitj3 avatar Jan 22 '21 22:01 Pitj3

OK, thats good. for "strip" devices you need to specify outlet where first device=0 is default. Are you using CLI, if so can pass it in. for command line, it wasn't included but I have forked for that, and ther ewas another person did it also. Ideally, just use cli, as it very fast in comparison

On Fri, Jan 22, 2021 at 10:30 PM Roderick Griffioen < [email protected]> wrote:

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

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/mattsaxon/pysonofflan/issues/130#issuecomment-765720012, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7UWOPPWEV3TTNHORJ6ACLS3H367ANCNFSM4OE7QM2Q .

dauheeIRL avatar Jan 22 '21 23:01 dauheeIRL

https://github.com/mattsaxon/pysonofflan/issues/130#issuecomment-739577327

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()

Rain365 avatar May 05 '21 19:05 Rain365

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

JacekKac avatar Aug 03 '22 17:08 JacekKac

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?

fernand0 avatar Aug 07 '22 10:08 fernand0

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

JacekKac avatar Aug 08 '22 08:08 JacekKac

Nice!

fernand0 avatar Aug 08 '22 09:08 fernand0

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.

Elio-Chedid avatar Mar 29 '24 08:03 Elio-Chedid

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.

Masterz69 avatar Mar 29 '24 11:03 Masterz69

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

dauheeIRL avatar Mar 29 '24 12:03 dauheeIRL

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?

Elio-Chedid avatar Mar 29 '24 13:03 Elio-Chedid