Month: August 2015

Setup.py – Configure Switches Using Crontab

Overview

Setup is both a utility command and a library implemented in python. It is initialized by the json config file defining the switches and their schemas (described on the previous blog post).

 json                   registers    +---------------------+
config +----> setup.py +-----------> |                     |
                    +                |   Crontab daemon    |
                    |                |                     |
                    |                +---------+-----------+
                    |                          |            
                    |                          |  calls     
                    |                          |  at XX:YY  
                    |                          |            
                    |    direct                v            
                    +--------------------->  turn.py        
                         call                               
The program reads each chunk of definitions in the config file translating the schemas into ‘turn on’ and ‘turn off’ commands which are registered in the crontab daemon of linux. In case the actual time is within an interval, the ‘turn on’ command is called directly. Registered on/off commands are run by crontab at the specified time e.g. sunset. The turn.py command is used to turn on and off, it is also a simple python program – presented in “Using python to control telldus”. The setup command might be run from the command line by supplying the json config file or if using it as a library interfacing it with other programs or a webserver.


Design

The setup.py program is divided into three classes SetupSchema, SetupCron and SetupTurn. The class SetupSchema is called from main to parse the configuration json file (described in former post). For each new definition of a switch a callback is executed to interested instances. In this case there are two instances interested, one from the class SetupCron and the other from the class SetupTurn – see image below. The class SetupCron registers ‘turn on’/’turn off’ commands in the crontab daemon, the class SetupTurn on the other side checks if the switch shall be on/off right now and executes turn.py directly.

setup.py                                 
+---------------------------------------+
|                subscribes             |
|                                       |
|                    +--> SetupCron.py  |
|                    |                  |
|  SetupSchema.py    |                  |
|                    |                  |
|                    +--> SetupTurn.py  |
|                                       |
+---------------------------------------+

Defining a Configuration Schema File for the Switches

There are a number of switches in the house, most of them controlling lamps but also other things like the printer for example. Most of the switches are of the brand ‘Nexa’ and selflearning which makes it easy to control using the tellstick.
A new switch has to be configured in two different ways. The first configuration file is the tellstick.conf used by the telldus service daemon. In this configuration file, the id is specified as well as the name and the protocol of the switch. It is quite easy to find the protocol name of the 433 MHz communication by looking into wikis/forums of telldus.
The second configuration file is the one defining the on/off intervals of the switches. First of all having a separate file defining the intervals gives the possibility to differ between intervals at normal occasion and trip/vacation.
The format of the configuration file is json-format since it is very easy to read directly into a python program. In more detail it is a list of all switches where each switch has a structure containing a textual description, the device id in telldus, and the schema/interval defined as either always (on), never (on), sun, night, or : 

{
   "text" : "textual description e.g. lamp in working room",
   "name" : "device id in telldusd",   "schema" : schema either "always", "never", "sun", "night", ":"
}
...


The schema/interval sun is defined as turning switch on 40 minutes before sunset and then turning it off at a defined bed time which is one o´clock at night.

Here is an excerpt from the existing normal.json of the house:

      {
        "text" : "Prydnadslampa 1",
        "name" : "wallswitch3",
        "schema" : "sun"
      },
      {
        "text" : "Pappas sänglampa",
        "name" : "wallswitch4",
        "schema" : "always"
      },
      {
        "text" : "Linneas sofflampa",
        "name" : "wallswitch5",
        "schema" : "night"
      }

In vacation.json the second switch above, the schema is changed from ‘always’ to ‘sun’ to give an illusion of somebody in the bedroom (does it fool anybody? – probably not).