In the previous post we saw some ideas about Managing my sonoff device from command-line.
But my objective was to integrate this management in other applications, so I needed to digg into the code.
The module has several interesting funcions, such as pysonofflanr3.cli.switch_device()
which is just what I wanted to do. So, I needed some way to store configuration (configParser
), the parameters known from my previous experiments, and some syntactic sugar:
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, "")
Top comments (0)