hass-core/homeassistant/components/switch/mochad.py
Robert Svensson 0009be595c Device Registry (#15980)
* First draft

* Generate device id

* No obscure registry

* Dont store config_entry_id in device

* Storage

* Small mistake on rebase

* Do storage more like entity registry

* Improve device identification

* Add tests

* Remove deconz device support from PR

* Fix hound comments, voff!

* Fix comments and clean up

* Fix proper indentation

* Fix pydoc issues

* Fix mochad component to not use self.device

* Fix mochad light platform to not use self.device

* Fix TankUtilitySensor to not use self.device

* Fix Soundtouch to not use self.device

* Fix Plex to not use self.device

* Fix Emby to not use self.device

* Fix Heatmiser to not use self.device

* Fix Wemo lights to not use self.device

* Fix Lifx to not use self.device

* Fix Radiotherm to not use self.device

* Fix Juicenet to not use self.device

* Fix Qwikswitch to not use self.device

* Fix Xiaomi miio to not use self.device

* Fix Nest to not use self.device

* Fix Tellduslive to not use self.device

* Fix Knx to not use self.device

* Clean up a small mistake in soundtouch

* Fix comment from Ballob

* Fix bad indentation

* Fix indentatin

* Lint

* Remove unused variable

* Lint
2018-08-22 10:46:37 +02:00

108 lines
3.8 KiB
Python

"""
Contains functionality to use a X10 switch over Mochad.
For more details about this platform, please refer to the documentation at
https://home.assistant.io/components/switch.mochad
"""
import logging
import voluptuous as vol
from homeassistant.components import mochad
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (CONF_NAME, CONF_DEVICES,
CONF_PLATFORM, CONF_ADDRESS)
from homeassistant.helpers import config_validation as cv
DEPENDENCIES = ['mochad']
_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,
vol.Optional(mochad.CONF_COMM_TYPE): cv.string,
}]
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up X10 switches over a mochad controller."""
devs = config.get(CONF_DEVICES)
add_devices([MochadSwitch(
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)
self._comm_type = dev.get(mochad.CONF_COMM_TYPE, 'pl')
self.switch = device.Device(ctrl, self._address,
comm_type=self._comm_type)
# 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
@property
def name(self):
"""Get the name of the switch."""
return self._name
def turn_on(self, **kwargs):
"""Turn the switch on."""
from pymochad.exceptions import MochadException
_LOGGER.debug("Reconnect %s:%s", self._controller.server,
self._controller.port)
with mochad.REQ_LOCK:
try:
# Recycle socket on new command to recover mochad connection
self._controller.reconnect()
self.switch.send_cmd('on')
# 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)
def turn_off(self, **kwargs):
"""Turn the switch off."""
from pymochad.exceptions import MochadException
_LOGGER.debug("Reconnect %s:%s", self._controller.server,
self._controller.port)
with mochad.REQ_LOCK:
try:
# Recycle socket on new command to recover mochad connection
self._controller.reconnect()
self.switch.send_cmd('off')
# 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)
def _get_device_status(self):
"""Get the status of the switch from mochad."""
with mochad.REQ_LOCK:
status = self.switch.get_status().rstrip()
return status == 'on'
@property
def is_on(self):
"""Return true if switch is on."""
return self._state