File: homeworks.py

package info (click to toggle)
pyhomeworks 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144 kB
  • sloc: python: 433; makefile: 7
file content (80 lines) | stat: -rw-r--r-- 2,746 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Component for interfacing to Lutron Homeworks keypads.

For more details about this component, please refer to the documentation at
https://home-assistant.io/components/homeworks/
"""
import logging
import voluptuous as vol
from homeassistant.components.binary_sensor import (
    BinarySensorDevice, PLATFORM_SCHEMA)
from homeassistant.components.homeworks import (
    HomeworksDevice, HOMEWORKS_CONTROLLER)
from homeassistant.const import CONF_NAME
import homeassistant.helpers.config_validation as cv

DEPENDENCIES = ['homeworks']
REQUIREMENTS = ['pyhomeworks==0.0.4']

_LOGGER = logging.getLogger(__name__)

EVENT_BUTTON_PRESSED = 'button_pressed'
CONF_KEYPADS = 'keypads'
CONF_ADDR = 'addr'
CONF_BUTTONS = 'buttons'

BUTTON_SCHEMA = vol.Schema({cv.positive_int: cv.string})
BUTTONS_SCHEMA = vol.Schema({
    vol.Required(CONF_ADDR): cv.string,
    vol.Required(CONF_NAME): cv.string,
    vol.Required(CONF_BUTTONS): vol.All(cv.ensure_list, [BUTTON_SCHEMA])
})
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Required(CONF_KEYPADS): vol.All(cv.ensure_list, [BUTTONS_SCHEMA])
})


def setup_platform(hass, config, add_entities, discover_info=None):
    """Set up the Homeworks keypads."""
    controller = hass.data[HOMEWORKS_CONTROLLER]
    devs = []
    for keypad in config.get(CONF_KEYPADS):
        name = keypad.get(CONF_NAME)
        addr = keypad.get(CONF_ADDR)
        buttons = keypad.get(CONF_BUTTONS)
        for button in buttons:
            # FIX: This should be done differently
            for num, title in button.items():
                devname = name + '_' + title
                dev = HomeworksKeypad(controller, addr, num, devname)
                devs.append(dev)
    add_entities(devs, True)
    return True


class HomeworksKeypad(HomeworksDevice, BinarySensorDevice):
    """Homeworks Keypad."""

    def __init__(self, controller, addr, num, name):
        """Create keypad with addr, num, and name."""
        HomeworksDevice.__init__(self, controller, addr, name)
        self._num = num
        self._state = None

    @property
    def is_on(self):
        """Return state of the button."""
        return self._state

    def callback(self, msg_type, values):
        """Dispatch messages from the controller."""
        from pyhomeworks.pyhomeworks import (
            HW_BUTTON_PRESSED, HW_BUTTON_RELEASED)

        old_state = self._state
        if msg_type == HW_BUTTON_PRESSED and values[1] == self._num:
            self.hass.bus.fire(EVENT_BUTTON_PRESSED,
                               {'entity_id': self.entity_id})
            self._state = True
        elif msg_type == HW_BUTTON_RELEASED and values[1] == self._num:
            self._state = False
        return old_state != self._state