Debian Codegraph @

ha/binary_sensor/homeworks.py

raw
1 """Component for interfacing to Lutron Homeworks keypads.
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.components.binary_sensor import (
9 BinarySensorDevice, PLATFORM_SCHEMA)
10 from homeassistant.components.homeworks import (
11 HomeworksDevice, HOMEWORKS_CONTROLLER)
12 from homeassistant.const import CONF_NAME
13 import homeassistant.helpers.config_validation as cv
14
15 DEPENDENCIES = ['homeworks']
16 REQUIREMENTS = ['pyhomeworks==0.0.4']
17
18 _LOGGER = logging.getLogger(__name__)
19
20 EVENT_BUTTON_PRESSED = 'button_pressed'
21 CONF_KEYPADS = 'keypads'
22 CONF_ADDR = 'addr'
23 CONF_BUTTONS = 'buttons'
24
25 BUTTON_SCHEMA = vol.Schema({cv.positive_int: cv.string})
26 BUTTONS_SCHEMA = vol.Schema({
27 vol.Required(CONF_ADDR): cv.string,
28 vol.Required(CONF_NAME): cv.string,
29 vol.Required(CONF_BUTTONS): vol.All(cv.ensure_list, [BUTTON_SCHEMA])
30 })
31 PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
32 vol.Required(CONF_KEYPADS): vol.All(cv.ensure_list, [BUTTONS_SCHEMA])
33 })
34
35
36 def setup_platform(hass, config, add_entities, discover_info=None):
37 """Set up the Homeworks keypads."""
38 controller = hass.data[HOMEWORKS_CONTROLLER]
39 devs = []
40 for keypad in config.get(CONF_KEYPADS):
41 name = keypad.get(CONF_NAME)
42 addr = keypad.get(CONF_ADDR)
43 buttons = keypad.get(CONF_BUTTONS)
44 for button in buttons:
45 # FIX: This should be done differently
46 for num, title in button.items():
47 devname = name + '_' + title
48 dev = HomeworksKeypad(controller, addr, num, devname)
49 devs.append(dev)
50 add_entities(devs, True)
51 return True
52
53
54 class HomeworksKeypad(HomeworksDevice, BinarySensorDevice):
55 """Homeworks Keypad."""
56
57 def __init__(self, controller, addr, num, name):
58 """Create keypad with addr, num, and name."""
59 HomeworksDevice.__init__(self, controller, addr, name)
60 self._num = num
61 self._state = None
62
63 @property
64 def is_on(self):
65 """Return state of the button."""
66 return self._state
67
68 def callback(self, msg_type, values):
69 """Dispatch messages from the controller."""
70 from pyhomeworks.pyhomeworks import (
71 HW_BUTTON_PRESSED, HW_BUTTON_RELEASED)
72
73 old_state = self._state
74 if msg_type == HW_BUTTON_PRESSED and values[1] == self._num:
75 self.hass.bus.fire(EVENT_BUTTON_PRESSED,
76 {'entity_id': self.entity_id})
77 self._state = True
78 elif msg_type == HW_BUTTON_RELEASED and values[1] == self._num:
79 self._state = False
80 return old_state != self._state