« Saving email attachments with pre-pended YYYY-MM-DD_ | Main | Head, meet wall - Do not use a mutable as a default parameter in python »
Friday
Oct312014

Controlling a powerUSB power strip from python on Mac OS X

I needed to inexpensively control some electronics hardware in my office. I found that powerUSB sells a reasonably priced USB controlled power strip with three controllable AC ports. (A fourth port is always on.)

From their website you can download a Mac software package that includes a GUI app (works fine, but not useful for anything scripted) and a command line tool (works fine for controlling, but I could never figure out how to get it to read outlet states back to me). If the command line utility had worked better I probably would have been fine with calling it from python. So, to fix that I wrote a package to control the powerUSB outlets from python.

The github repository is here.

The PyPI package is here.

You will need to have swig tools installed. (If you're using Enthought's Canopy you can install swig from their package manager.)

You will also need to have some version of libusb-1.0 installed. No doubt one could easily modify the pwrusb package to work against other USB libraries, but I chose libusb-1.0 because it is so easily installed from homebrew with brew install libusb.

Installing the pwrusb package is super easy from PyPI:

pip install pwrusb

Note that to avoid any possible copyright issues, I did not include any of the powerUSB code in the distribution. Instead, the necessary code and library will be downloaded from their website downloads section automatically during build & install. Only problem will be when they inevitably change the paths on their website.

You address a single outlet at a time, e.g.:

In [1]: import pwrusb

In [2]: bank = 0

In [3]: for outlet in [1, 2, 3]:
   ...:     print "outlet {} is {}".format(outlet, pwrusb.get_single_outlet_state(bank, outlet))
   ...:     
outlet 1 is 0
outlet 2 is 0
outlet 3 is 0

In [4]: pwrusb.set_single_outlet_state(bank, 2, True)

In [5]: for outlet in [1, 2, 3]:
    print "outlet {} is {}".format(outlet, pwrusb.get_single_outlet_state(bank, outlet))
   ...:     
outlet 1 is 0
outlet 2 is 1
outlet 3 is 0

In [6]: pwrusb.set_single_outlet_state(bank, 2, False)

In [7]: for outlet in [1, 2, 3]:
    print "outlet {} is {}".format(outlet, pwrusb.get_single_outlet_state(bank, outlet))
   ...:     
outlet 1 is 0
outlet 2 is 0
outlet 3 is 0