-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
86 lines (65 loc) · 2.01 KB
/
__init__.py
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
81
82
83
84
85
86
"""Support for CM15A/CM19A X10 Controller using mochad daemon."""
import logging
import threading
from pymochad import controller, exceptions
import voluptuous as vol
from homeassistant.const import (
CONF_HOST,
CONF_PORT,
EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP,
)
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
CONF_COMM_TYPE = "comm_type"
DOMAIN = "mochad2"
REQ_LOCK = threading.Lock()
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Optional(CONF_HOST, default="localhost"): cv.string,
vol.Optional(CONF_PORT, default=1099): cv.port,
}
)
},
extra=vol.ALLOW_EXTRA,
)
def setup(hass, config):
"""Set up the mochad component."""
conf = config[DOMAIN]
host = conf.get(CONF_HOST)
port = conf.get(CONF_PORT)
try:
mochad_controller = MochadCtrl(host, port)
except exceptions.ConfigurationError:
_LOGGER.exception()
return False
def stop_mochad(event):
"""Stop the Mochad service."""
mochad_controller.disconnect()
def start_mochad(event):
"""Start the Mochad service."""
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_mochad)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_mochad)
hass.data[DOMAIN] = mochad_controller
return True
class MochadCtrl:
"""Mochad controller."""
def __init__(self, host, port):
"""Initialize a PyMochad controller."""
super().__init__()
self._host = host
self._port = port
self.ctrl = controller.PyMochad(server=self._host, port=self._port)
@property
def host(self):
"""Return the server where mochad is running."""
return self._host
@property
def port(self):
"""Return the port mochad is running on."""
return self._port
def disconnect(self):
"""Close the connection to the mochad socket."""
self.ctrl.socket.close()