|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: "Checklist for creating a platform" |
| 4 | +description: "A list of things to pay attention to when code reviewing a platform." |
| 5 | +date: 2017-01-15 14:09 -0800 |
| 6 | +sidebar: true |
| 7 | +comments: false |
| 8 | +sharing: true |
| 9 | +footer: true |
| 10 | +--- |
| 11 | + |
| 12 | +A checklist of things to do when you're adding a new platform. |
| 13 | + |
| 14 | +### {% linkable_title 1. Requirements %} |
| 15 | + |
| 16 | + 1. Requirement version pinned: `REQUIREMENTS = ['phue==0.8.1']` |
| 17 | + 2. If requirement hosted on GitHub: |
| 18 | + - Point at a zip archive of a release tag or commit SHA. |
| 19 | + - Add version found in zip-archive as hash to url. |
| 20 | + |
| 21 | +```python |
| 22 | +REQUIREMENTS = [ |
| 23 | + 'http://github.com/technicalpickles/python-nest' |
| 24 | + '/archive/e6c9d56a8df455d4d7746389811f2c1387e8cb33.zip' |
| 25 | + '#python-nest==3.0.3'] |
| 26 | +``` |
| 27 | + |
| 28 | +### {% linkable_title 2. Dependencies %} |
| 29 | + |
| 30 | + 1. If you depend on a component for the connection, add it to your dependencies: `DEPENDENCIES = ['nest']` |
| 31 | + |
| 32 | +### {% linkable_title 3. Configuration %} |
| 33 | + |
| 34 | + 1. Volutpuous schema present for config validation |
| 35 | + 2. Voluptuous schema extends schema from component<br>(e.g. `light.hue.PLATFORM_SCHEMA` extends `light.PLATFORM_SCHEMA`) |
| 36 | + 3. Default parameters specified in voluptuous schema, not in `setup_platform(…)` |
| 37 | + 4. Schema using as many generic config keys as possible from `homeassistant.const` |
| 38 | + |
| 39 | +```python |
| 40 | +import voluptuous as vol |
| 41 | + |
| 42 | +from homeassistant.const import CONF_FILENAME, CONF_HOST |
| 43 | +from homeassistant.components.light import PLATFORM_SCHEMA |
| 44 | +import homeassistant.helpers.config_validation as cv |
| 45 | + |
| 46 | +CONF_ALLOW_UNREACHABLE = 'allow_unreachable' |
| 47 | +DEFAULT_UNREACHABLE = False |
| 48 | + |
| 49 | +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ |
| 50 | + vol.Required(CONF_HOST): cv.string, |
| 51 | + vol.Optional(CONF_ALLOW_UNREACHABLE, |
| 52 | + default=DEFAULT_UNREACHABLE): cv.boolean, |
| 53 | + vol.Optional(CONF_FILENAME): cv.string, |
| 54 | +}) |
| 55 | +``` |
| 56 | + |
| 57 | +### {% linkable_title 4. Setup Platform %} |
| 58 | + |
| 59 | + 1. Test if passed in info (user/pass/host etc.) works. |
| 60 | + 2. Group your calls to `add_devices` if possible. |
| 61 | + 3. If platform adds extra services, format should be `<component>.<platform>_<service name>`. |
| 62 | + |
| 63 | +### {% linkable_title 5. Entity %} |
| 64 | + |
| 65 | + 1. Extend entity from component, e.g. `class HueLight(Light)` |
| 66 | + 2. Do not call `update()` in constructor, use `add_devices(devices, True)` instead. |
| 67 | + 3. Do not do any I/O inside properties. Cache values inside `update()` instead. |
0 commit comments