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

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay