ouimeaux 0.2 released: Now with Motion events!

Finally had a chance to fire up Wireshark and figure out how WeMo Motions and Switches talk to each other. I've worked the results of my discovery into ouimeaux 0.2. Now you can register callbacks to be notified whenever a Motion or Switch changes state (i.e., when motion is detected or a switch is switched). I also cleaned up a few bugs with the UDP server, so you should be able to discover periodically now without it throwing an error.

https://pypi.python.org/pypi/ouimeaux/0.2
https://github.com/iancmcc/ouimeaux

Read More...

Controlling Belkin WeMo switch with a Raspberry Pi

Here's a simple example of a way to use the ouimeaux library. The RPi's running a server that discovers a WeMo switch by name (specified on the command line), then essentially forwards switch input from the GPIO pin to the WeMo. With a power supply circuit to let it run off 110 AC and a nicer switch, you could mount this in the wall to control your WeMo a little quicker than pulling out your phone, for those cases when you're already in the room. Said my sister: "Congratulations, you've reinvented the light switch."

The switch circuit is the simplest possible; example circuitry and code can be found here.

Read More...

ouimeaux: Command line and Python API for WeMo hacking

I bought a bunch of Belkin WeMo Switches over Christmas, and while I appreciate that my lights now have IP addresses, controlling them can really only be done through the WeMo iPhone app, which (besides having a few understandable bugs) is slow to start up. More to the point, I had no way to hook the switches up to any other events beyond using IFTTT, which seems to have a one-switch limit.

Luckily, WeMo devices appear to provide a fairly complete SOAP API. Isaac Kelly posted a proof of concept discovering and controlling switches, so obviously more direct control was possible. Hacking together a decent library only took a couple of nights.

Thus: ouimeaux. It uses gevent for async I/O and the minimal UPnP functionality required (broadcasting M-SEARCH requests, listening for responses), and it uses requests to communicate directly with the devices. Service stubs are built on the fly and provide easy attribute access to actions (for example, switch.basicevent.SetBinaryState(BinaryState=1)). There's also a handy explain() method to print out all services, actions and arguments. The only useful things I had time to figure out how to do—turn switches on and off, get current switch state—I pulled up into methods on the switch object itself: switch.on(), switch.off(), switch.get_state(). The documentation gives more details.

Once that was written, a simple command line script was straightforward. Installing the package will get you the wemo script in your PATH. wemo list to discover names of devices, wemo switch "Switch Name" on to turn it on. Nothing else so far, but it's highly hackable.

I hope to explore the Motion API next, and provide the ability to listen to events from Motion devices. Firmware uploading is exposed too; might be possible to intercept and modify to get an SSH server running.

Read More...

pyxdeco: Python eXtraordinary Decorators

Decorators in Python are limited to action when the function they wrap is actually called. Which is fine and all, but they can be more useful. Or at least could be, if they allowed for hooks at definition or instance creation. I've packaged up a technique I've been using to do just that and released it as pyxdeco.

I wrote the code when trying to register instance methods as event listeners automatically when the instance was created. Decorator syntax made the most sense by far:

...
@on('click')
def click_listener(self):
    ...
...but Python decorators can't be used that way, since the decorator wouldn't be invoked until click_listener was actually called.

This package adds the ability to define decorators to be called at any of the three points of the function's existence: when the function is defined, when an instance of the class containing the function is created, and when the function is itself called (the last is of course not new, just a regular decorator, but is provided for consistency and completeness).

Code is on github, downloads available from pypi. Suggestions for examples or future development welcome.

Read More...

Decorator Factories (and decorator factory factories)

Decorators are a simple, handy way of implementing certain features in any language, but many people find them perplexing. When you start talking about functions that generate decorators, it gets a little twisty; when you then make helpful functions to create functions that create decorators, you might have to stop for a minute to unclench your brain. Really, though, the concept is quite simple if you take it step by step, so here it is (after the jump), hopefully demystified.

Read More...

Released: txrestapi 0.1

Sorry updates have been horribly infrequent; I both had a kid and moved to Austin (in that order) in the last few months. With that out of the way, to the code...

I got frustrated chaining twisted.web.resource.Resource classes to provide a complex REST API service, so I wrote a new package that lets you do it with regular expressions and decorators, like:

@GET('^/path/to/api/method$')
    def on_api_method(self, request):
       ...
I'm pretty fond of it already. Code is on GitHub, package is available from pypi — and, of course, via easy_install. Full docs are at both sites, quick example after the jump.

Read More...

Notes on Teaching JavaScript

For the past few months, I've been tutoring a friend in JavaScript, and I've rediscovered some trite knowledge anew: there's no better way to improve in a subject than to teach it.

My friend and I come at programming JS from different directions: he's primarily a Web designer who uses jQuery to enhance pages that can stand on their own, while I'm a Python/Zope developer who's currently using ExtJS as the framework for a full-blown web application. I did a lot of thinking before our first session about how to approach the undertaking—should I teach to a framework, since that would yield the most immediate benefits? If so, which? Would I be able to explain best practices without going laboriously over never-again-used fundamentals of programming? Would our sessions even be worthwhile, since hundreds of tutorials are freely available?

Luckily my tutee was enthusiastic about learning from the bottom up, which was significantly easier for me, since that's how I learned myself. Despite the familiar trajectory, our sessions have been pretty educational for me, too, in part because they've highlighted the difficulties (and rewards) of teaching JavaScript, as distinct from those involved in learning it.

Read More...