| 1 |
"""Component for interfacing to Lutron Homeworks Series 4 and 8 systems. |
| 2 |
|
| 3 |
For more details about this component, please refer to the documentation at |
| 4 |
https://home-assistant.io/components/homeworks/ |
| 5 |
""" |
| 6 |
import logging |
| 7 |
import voluptuous as vol |
| 8 |
from homeassistant.const import ( |
| 9 |
CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP) |
| 10 |
import homeassistant.helpers.config_validation as cv |
| 11 |
from homeassistant.helpers.entity import Entity |
| 12 |
|
| 13 |
REQUIREMENTS = ['pyhomeworks==0.0.4'] |
| 14 |
|
| 15 |
_LOGGER = logging.getLogger(__name__) |
| 16 |
|
| 17 |
DOMAIN = 'homeworks' |
| 18 |
|
| 19 |
HOMEWORKS_CONTROLLER = 'homeworks' |
| 20 |
|
| 21 |
CONFIG_SCHEMA = vol.Schema({ |
| 22 |
DOMAIN: vol.Schema({ |
| 23 |
vol.Required(CONF_HOST): cv.string, |
| 24 |
vol.Required(CONF_PORT): cv.port, |
| 25 |
}), |
| 26 |
}, extra=vol.ALLOW_EXTRA) |
| 27 |
|
| 28 |
|
| 29 |
def setup(hass, base_config): |
| 30 |
"""Start Homeworks controller.""" |
| 31 |
from pyhomeworks.pyhomeworks import Homeworks |
| 32 |
|
| 33 |
class HomeworksController(Homeworks): |
| 34 |
"""Interface between HASS and Homeworks controller.""" |
| 35 |
|
| 36 |
def __init__(self, host, port): |
| 37 |
"""Host and port of Lutron Homeworks controller.""" |
| 38 |
Homeworks.__init__(self, host, port, self.callback) |
| 39 |
self._subscribers = {} |
| 40 |
|
| 41 |
def subscribe(self, device): |
| 42 |
"""Add a device to subscribe to events.""" |
| 43 |
if device.addr not in self._subscribers: |
| 44 |
self._subscribers[device.addr] = [] |
| 45 |
self._subscribers[device.addr].append(device) |
| 46 |
if device.is_light: |
| 47 |
self.request_dimmer_level(device.addr) |
| 48 |
|
| 49 |
def callback(self, msg_type, values): |
| 50 |
"""Dispatch state changes.""" |
| 51 |
_LOGGER.debug('callback: %s, %s', msg_type, values) |
| 52 |
addr = values[0] |
| 53 |
for sub in self._subscribers.get(addr, []): |
| 54 |
_LOGGER.debug("callback: %s", sub) |
| 55 |
if sub.callback(msg_type, values): |
| 56 |
sub.schedule_update_ha_state() |
| 57 |
|
| 58 |
config = base_config.get(DOMAIN) |
| 59 |
host = config[CONF_HOST] |
| 60 |
port = config[CONF_PORT] |
| 61 |
|
| 62 |
controller = HomeworksController(host, port) |
| 63 |
hass.data[HOMEWORKS_CONTROLLER] = controller |
| 64 |
|
| 65 |
def cleanup(event): |
| 66 |
controller.close() |
| 67 |
|
| 68 |
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, cleanup) |
| 69 |
return True |
| 70 |
|
| 71 |
|
| 72 |
class HomeworksDevice(Entity): |
| 73 |
"""Base class of a Homeworks device.""" |
| 74 |
|
| 75 |
is_light = False |
| 76 |
|
| 77 |
def __init__(self, controller, addr, name): |
| 78 |
"""Controller, address, and name of the device.""" |
| 79 |
self._addr = addr |
| 80 |
self._name = name |
| 81 |
self._controller = controller |
| 82 |
|
| 83 |
async def async_added_to_hass(self): |
| 84 |
"""Register callback.""" |
| 85 |
self.hass.async_add_job(self._controller.subscribe, self) |
| 86 |
|
| 87 |
@property |
| 88 |
def addr(self): |
| 89 |
"""Device address.""" |
| 90 |
return self._addr |
| 91 |
|
| 92 |
@property |
| 93 |
def name(self): |
| 94 |
"""Device name.""" |
| 95 |
return self._name |
| 96 |
|
| 97 |
@property |
| 98 |
def should_poll(self): |
| 99 |
"""No need to poll.""" |
| 100 |
return False |
| 101 |
|
| 102 |
def callback(self, msg_type, values): |
| 103 |
"""Run when Homeworks device changes state.""" |
| 104 |
pass |