2019-04-03 15:40:03 +00:00
|
|
|
"""Support for X10 lights."""
|
2016-07-28 03:54:02 +00:00
|
|
|
import logging
|
|
|
|
from subprocess import check_output, CalledProcessError, STDOUT
|
2016-09-21 05:21:06 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.const import (CONF_NAME, CONF_ID, CONF_DEVICES)
|
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light, PLATFORM_SCHEMA)
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-07-28 03:54:02 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_X10 = SUPPORT_BRIGHTNESS
|
|
|
|
|
2016-09-21 05:21:06 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_DEVICES): vol.All(cv.ensure_list, [
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ID): cv.string,
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
}
|
|
|
|
]),
|
|
|
|
})
|
|
|
|
|
2016-07-28 03:54:02 +00:00
|
|
|
|
|
|
|
def x10_command(command):
|
|
|
|
"""Execute X10 command and check output."""
|
2016-09-21 05:21:06 +00:00
|
|
|
return check_output(['heyu'] + command.split(' '), stderr=STDOUT)
|
2016-07-28 03:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_unit_status(code):
|
|
|
|
"""Get on/off status for given unit."""
|
2017-01-17 07:11:02 +00:00
|
|
|
output = check_output('heyu onstate ' + code, shell=True)
|
|
|
|
return int(output.decode('utf-8')[0])
|
2016-07-28 03:54:02 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Set up the x10 Light platform."""
|
2018-12-06 15:25:59 +00:00
|
|
|
is_cm11a = True
|
2016-07-28 03:54:02 +00:00
|
|
|
try:
|
2016-09-21 05:21:06 +00:00
|
|
|
x10_command('info')
|
2016-07-28 03:54:02 +00:00
|
|
|
except CalledProcessError as err:
|
2018-12-06 15:25:59 +00:00
|
|
|
_LOGGER.info("Assuming that the device is CM17A: %s", err.output)
|
|
|
|
is_cm11a = False
|
2016-07-28 03:54:02 +00:00
|
|
|
|
2018-12-06 15:25:59 +00:00
|
|
|
add_entities(X10Light(light, is_cm11a) for light in config[CONF_DEVICES])
|
2016-07-28 03:54:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
class X10Light(Light):
|
2016-07-28 05:04:12 +00:00
|
|
|
"""Representation of an X10 Light."""
|
2016-07-28 03:54:02 +00:00
|
|
|
|
2018-12-06 15:25:59 +00:00
|
|
|
def __init__(self, light, is_cm11a):
|
2016-07-28 03:54:02 +00:00
|
|
|
"""Initialize an X10 Light."""
|
|
|
|
self._name = light['name']
|
|
|
|
self._id = light['id']
|
|
|
|
self._brightness = 0
|
2017-01-17 07:11:02 +00:00
|
|
|
self._state = False
|
2018-12-06 15:25:59 +00:00
|
|
|
self._is_cm11a = is_cm11a
|
2016-07-28 03:54:02 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this light."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the brightness of the light."""
|
2016-07-28 03:54:02 +00:00
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-07-28 05:04:12 +00:00
|
|
|
"""Return true if light is on."""
|
2017-01-17 07:11:02 +00:00
|
|
|
return self._state
|
2016-07-28 03:54:02 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_X10
|
|
|
|
|
2016-07-28 03:54:02 +00:00
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Instruct the light to turn on."""
|
2018-12-06 15:25:59 +00:00
|
|
|
if self._is_cm11a:
|
|
|
|
x10_command('on ' + self._id)
|
|
|
|
else:
|
|
|
|
x10_command('fon ' + self._id)
|
2016-07-28 03:54:02 +00:00
|
|
|
self._brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
|
2017-01-17 07:11:02 +00:00
|
|
|
self._state = True
|
2016-07-28 03:54:02 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Instruct the light to turn off."""
|
2018-12-06 15:25:59 +00:00
|
|
|
if self._is_cm11a:
|
|
|
|
x10_command('off ' + self._id)
|
|
|
|
else:
|
|
|
|
x10_command('foff ' + self._id)
|
2017-01-17 07:11:02 +00:00
|
|
|
self._state = False
|
2016-07-28 03:54:02 +00:00
|
|
|
|
|
|
|
def update(self):
|
2017-01-17 07:11:02 +00:00
|
|
|
"""Fetch update state."""
|
2018-12-06 15:25:59 +00:00
|
|
|
if self._is_cm11a:
|
|
|
|
self._state = bool(get_unit_status(self._id))
|
|
|
|
else:
|
|
|
|
# Not supported on CM17A
|
|
|
|
pass
|