DEV Community

Fernando Tricas García
Fernando Tricas García

Posted on

1

Managing my sonoff from my own program

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, "")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Image of Stellar post

From Hackathon to Funded - Stellar Dev Diaries Ep. 1 🎥

Ever wondered what it takes to go from idea to funding? In episode 1 of the Stellar Dev Diaries, we hear how the Freelii team did just that. Check it out and follow along to see the rest of their dev journey!

Watch the video