2019-02-14 04:35:12 +00:00
|
|
|
"""Support for X10 switch over Mochad."""
|
2016-10-25 04:49:49 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components import mochad
|
|
|
|
from homeassistant.components.switch import SwitchDevice
|
2017-08-03 08:51:01 +00:00
|
|
|
from homeassistant.const import (CONF_NAME, CONF_DEVICES,
|
|
|
|
CONF_PLATFORM, CONF_ADDRESS)
|
2016-10-25 04:49:49 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = vol.Schema({
|
|
|
|
vol.Required(CONF_PLATFORM): mochad.DOMAIN,
|
|
|
|
CONF_DEVICES: [{
|
|
|
|
vol.Optional(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_ADDRESS): cv.x10_address,
|
2017-08-03 08:51:01 +00:00
|
|
|
vol.Optional(mochad.CONF_COMM_TYPE): cv.string,
|
2016-10-25 04:49:49 +00:00
|
|
|
}]
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up X10 switches over a mochad controller."""
|
2016-10-25 04:49:49 +00:00
|
|
|
devs = config.get(CONF_DEVICES)
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([MochadSwitch(
|
2016-10-25 04:49:49 +00:00
|
|
|
hass, mochad.CONTROLLER.ctrl, dev) for dev in devs])
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class MochadSwitch(SwitchDevice):
|
|
|
|
"""Representation of a X10 switch over Mochad."""
|
|
|
|
|
|
|
|
def __init__(self, hass, ctrl, dev):
|
|
|
|
"""Initialize a Mochad Switch Device."""
|
|
|
|
from pymochad import device
|
|
|
|
|
|
|
|
self._controller = ctrl
|
|
|
|
self._address = dev[CONF_ADDRESS]
|
|
|
|
self._name = dev.get(CONF_NAME, 'x10_switch_dev_%s' % self._address)
|
2017-08-03 08:51:01 +00:00
|
|
|
self._comm_type = dev.get(mochad.CONF_COMM_TYPE, 'pl')
|
2019-02-14 04:35:12 +00:00
|
|
|
self.switch = device.Device(
|
|
|
|
ctrl, self._address, comm_type=self._comm_type)
|
2018-01-08 07:32:24 +00:00
|
|
|
# Init with false to avoid locking HA for long on CM19A (goes from rf
|
|
|
|
# to pl via TM751, but not other way around)
|
|
|
|
if self._comm_type == 'pl':
|
|
|
|
self._state = self._get_device_status()
|
|
|
|
else:
|
|
|
|
self._state = False
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Get the name of the switch."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the switch on."""
|
2018-01-08 07:32:24 +00:00
|
|
|
from pymochad.exceptions import MochadException
|
|
|
|
_LOGGER.debug("Reconnect %s:%s", self._controller.server,
|
|
|
|
self._controller.port)
|
2017-12-08 17:18:52 +00:00
|
|
|
with mochad.REQ_LOCK:
|
2018-01-08 07:32:24 +00:00
|
|
|
try:
|
|
|
|
# Recycle socket on new command to recover mochad connection
|
|
|
|
self._controller.reconnect()
|
2018-08-22 08:46:37 +00:00
|
|
|
self.switch.send_cmd('on')
|
2018-01-08 07:32:24 +00:00
|
|
|
# No read data on CM19A which is rf only
|
|
|
|
if self._comm_type == 'pl':
|
|
|
|
self._controller.read_data()
|
|
|
|
self._state = True
|
|
|
|
except (MochadException, OSError) as exc:
|
|
|
|
_LOGGER.error("Error with mochad communication: %s", exc)
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the switch off."""
|
2018-01-08 07:32:24 +00:00
|
|
|
from pymochad.exceptions import MochadException
|
|
|
|
_LOGGER.debug("Reconnect %s:%s", self._controller.server,
|
|
|
|
self._controller.port)
|
2017-12-08 17:18:52 +00:00
|
|
|
with mochad.REQ_LOCK:
|
2018-01-08 07:32:24 +00:00
|
|
|
try:
|
|
|
|
# Recycle socket on new command to recover mochad connection
|
|
|
|
self._controller.reconnect()
|
2018-08-22 08:46:37 +00:00
|
|
|
self.switch.send_cmd('off')
|
2018-01-08 07:32:24 +00:00
|
|
|
# No read data on CM19A which is rf only
|
|
|
|
if self._comm_type == 'pl':
|
|
|
|
self._controller.read_data()
|
|
|
|
self._state = False
|
|
|
|
except (MochadException, OSError) as exc:
|
|
|
|
_LOGGER.error("Error with mochad communication: %s", exc)
|
2016-10-25 04:49:49 +00:00
|
|
|
|
|
|
|
def _get_device_status(self):
|
|
|
|
"""Get the status of the switch from mochad."""
|
2017-12-08 17:18:52 +00:00
|
|
|
with mochad.REQ_LOCK:
|
2018-08-22 08:46:37 +00:00
|
|
|
status = self.switch.get_status().rstrip()
|
2016-10-25 04:49:49 +00:00
|
|
|
return status == 'on'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return true if switch is on."""
|
|
|
|
return self._state
|