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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up