alexa
A smart home skill is a router and a contract

Four vendors live in my flat and every one of them shipped an app. The bulbs have one, the thermostat has another, the door lock has a third, and none of them can be persuaded that the others exist. The obvious fix is to put one thing in front of all of them, and since I already talk to a speaker in the hallway it may as well be Alexa.
The result is a single Lambda function. Everything below is from home-automation, which is a weekend project and reads like one.
The directive Alexa sends
A smart home skill never sees the conversation. Alexa does the language part and sends your function a directive, which is a small JSON document with a namespace, a name, and the endpoint it applies to.
{
"directive": {
"header": {
"namespace": "Alexa.PowerController",
"name": "TurnOn",
"payloadVersion": "3",
"correlationToken": "..."
},
"endpoint": {
"scope": { "type": "BearerToken", "token": "..." },
"endpointId": "kitchen-ceiling"
},
"payload": {}
}
}
Every directive looks like that. Alexa.BrightnessController / SetBrightness
carries a number in the payload, Alexa.Discovery / Discover has no endpoint
at all, and Alexa / ReportState wants the current state back. Perhaps twenty
pairs in total for a flat like mine.
The router half
Twenty pairs is about the size at which a dispatch chain starts to rot. My
first version was a chain of if statements in the handler, and by the third
capability it was the only file I ever touched. That's usually a sign the seam
is in the wrong place.
So the pairs live in a table and each module puts itself in it:
_HANDLERS = {}
def register(namespace, name):
def decorator(function):
_HANDLERS[(namespace, name)] = function
return function
return decorator
def resolve(namespace, name):
try:
return _HANDLERS[(namespace, name)]
except KeyError:
raise errors.UnsupportedOperation(
"no handler for {0}.{1}".format(namespace, name)
)
A capability is then just a module:
@routing.register("Alexa.PowerController", "TurnOn")
def turn_on(directive):
return _set_power(directive, ON)
@routing.register("Alexa.PowerController", "TurnOff")
def turn_off(directive):
return _set_power(directive, OFF)
The handler imports the package for the side effect, resolves, and calls. It doesn't know anything about power or brightness or locks, and adding a capability means one new file plus one import line.
The contract half, which is the one that hurts
It took me two evenings to understand that Alexa does not probe your function to find out what it can do. It asks once, during discovery, and you answer with a capability document per endpoint:
def _interface(namespace, properties):
return {
"type": "AlexaInterface",
"interface": namespace,
"version": "3",
"properties": {
"supported": [{"name": name} for name in properties],
"retrievable": True,
"proactivelyReported": True,
},
}
Whatever you claim there, Alexa believes. Claim BrightnessController on a bulb
that only switches and the utterance will be understood, routed, and sent to
you. Then your handler raises and the app says the device is not responding,
which is true and no help at all, because the mistake was made weeks earlier in
a different file.
The reverse is worse in its own way. Implement a directive and forget to advertise it, and the utterance never reaches you, with no error anywhere. Alexa just says she doesn't know how, and you go looking through CloudWatch for a request that was never made.
So the capability list and the handler table are two halves of the same thing and have to be edited together. I haven't found a way to make the code enforce that. I've started to suspect the answer is a test that walks both tables and diffs them, but for now it's a line in the README and the memory of those two evenings.
The bit I would tell myself in January
The device model matters more than the skill. Once every bulb, lock and sensor in the flat is described by the same handful of fields (an id, a type, a room, and how to reach it), the skill collapses into a translation layer of about two hundred lines over that model. Most of the code sits underneath it.


