gwww / upb-lib

Universal Powerline Bus library - interacts with UPB PIMs
MIT License
4 stars 2 forks source link

Python UPB Powerline Interface library

Library for interacting with UPB PIM/CIM

https://github.com/gwww/upb-lib

Requirements

Description

This package is created as a library to interact with an UPB PIM. The motivation to write this was to use with the Home Assistant automation platform. The library can be used for writing other UPB applications. The IO with the PIM is asynchronous over TCP or over the serial port.

Installation

    $ pip install upb_lib

Overview

Simplest thing right now is when in the root of the git repo that you have cloned is to enter the command bin/simple. This program requires that the environment variable UPBPIM_URL set to indicate how to connect to the PIM. For example, serial:///dev/cu.KeySerial1 connects to the PIM on a serial port (serial://) /dev/cu/KeySerial1. On Windows something like serial://COM1 might work.

Also required is a UPStart export file. The bin/simple program looks for it in the same directory as where the program is (i.e.: bin) and assumes that it is named upb.upe.

Configuration

Initialization of the library takes the following parameters:

url: This is the PIM to connect to. It is formatted as a URL. Two formats are supported: serial://<device> where <device> is the serial/USB port on which the PIM is connected; tcp://<IP or domain>[:<port] where IP or domain is where the device is connected on the network (perhaps using ser2tcp or a PIM-U) and an optional port number with a default of 2101.

UPStartExportFile: the path of where to read the export file generated through File->Export on the UpStart utility. This is optional but recommended.

tx_count: (optional) The number of times every UPB message is transmitted (CNT portion of the control word). Defaults to 1. Setting the value to 2 may be required for networks that use a repeater.

flags: (optional) A string that contains a set of comma separated flags. Each flag can take the form of or =. Parse is simple with no escapes. So, values cannot contain commas or equals. Flags supported are:

First use of the API

Read the code in bin/simple. That is the short use of the API around. Beyond that look at the file lights.py and links.py. Any method in those files that has a description that starts with (Helper) are generally UPB actions.

Usage

Naming

UPB device name are a concatenation of the UPB network ID, the UPB device ID, and the channel number of the device. An underscore separates the values. For example, for a single channel UPB device number 42 on UPB network 142 the device name used in the library would be "142_42_0". For a multi-channel device the channel numbers start at 0.

UPB Links are named as a concatenation of the UPB network ID and the link number. For example, link number 6 on UPB network 142 would be "142_6".

See bin/simple for example code.

Transition Rate

Many UPB commands take a rate. This API supports the rate as a number of seconds, which is different than what the protocol uses. The protocol allows a set of distinct rates, listed below. For example in the UPB protocol if the rate 7 is sent to a device then the fade rate (for example) would be 20 seconds.

Since the API takes a value in seconds and any number of seconds can be specified, what the API does is convert the number of seconds to the closest rate. For example, if a rate of 24 seconds is passed to the API then the API will convert that to the nearest protocol rate value, which in this case is 7.

The values of the UPB protocol rate values and mapped to the number of seconds is as follows (at least for Simply Automated devices):

0 = Snap
1 = 0.8 seconds
2 = 1.6 seconds
3 = 3.3 seconds
4 = 5.0 seconds
5 = 6.6 seconds
6 = 10 seconds
7 = 20 seconds
8 = 30 seconds
9 = 1 minute
10 = 2 minutes
11 = 5 minutes
12 = 10 minutes
13 = 15 minutes
14 = 30 minutes
15 = 1 hour

Development

This project uses poetry for development dependencies. Installation instructions are on their website.

To get started developing:

git clone https://github.com/gwww/upb-lib.git
cd upb
poetry install
poetry shell # Or activate the created virtual environment
make test # to ensure everything installed properly

There is a Makefile in the root directory as well. The make command followed by one of the targets in the Makefile can be used. If you don't have or wish to use make the Makefile serves as examples of common commands that can be run.

Working with the UPB Protocol

Useful information on the UPB protocol can be found in the following documents:

  1. UPB System Description
  2. Powerline Interface Module Description

Using ser2net to proxy and examine commands sent by UpStart can also be insightful.