DEV Community


Managing my sonoff from my own program

fernand0 profile image Fernando Tricas García ・1 min read

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__": 

            format='%(asctime)s %(message)s')

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

    config = configparser.ConfigParser()'{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)
       print("We need a command, changing the state")
       pysonofflanr3.cli.switch_device(config, None, "")
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Editor guide