2016-05-13 04:39:30 +00:00
|
|
|
"""
|
2016-05-21 14:59:52 +00:00
|
|
|
Support for Qwikswitch devices.
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2016-05-21 14:59:52 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/qwikswitch/
|
2016-05-13 04:39:30 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2017-01-17 22:40:34 +00:00
|
|
|
|
2016-07-24 00:03:29 +00:00
|
|
|
import voluptuous as vol
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2017-01-17 22:40:34 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, CONF_URL)
|
2016-07-24 00:03:29 +00:00
|
|
|
from homeassistant.helpers.discovery import load_platform
|
2017-01-17 22:40:34 +00:00
|
|
|
from homeassistant.components.light import (
|
|
|
|
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
|
2016-07-24 00:03:29 +00:00
|
|
|
from homeassistant.components.switch import SwitchDevice
|
|
|
|
|
2016-12-29 09:08:11 +00:00
|
|
|
REQUIREMENTS = ['pyqwikswitch==0.4']
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2017-01-17 22:40:34 +00:00
|
|
|
DOMAIN = 'qwikswitch'
|
|
|
|
|
|
|
|
CONF_DIMMER_ADJUST = 'dimmer_adjust'
|
|
|
|
CONF_BUTTON_EVENTS = 'button_events'
|
2016-07-24 00:03:29 +00:00
|
|
|
CV_DIM_VALUE = vol.All(vol.Coerce(float), vol.Range(min=1, max=3))
|
2017-01-17 22:40:34 +00:00
|
|
|
|
2016-07-24 00:03:29 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
2017-01-17 22:40:34 +00:00
|
|
|
vol.Required(CONF_URL, default='http://127.0.0.1:2020'):
|
|
|
|
vol.Coerce(str),
|
|
|
|
vol.Optional(CONF_DIMMER_ADJUST, default=1): CV_DIM_VALUE,
|
|
|
|
vol.Optional(CONF_BUTTON_EVENTS): vol.Coerce(str)
|
2016-07-24 00:03:29 +00:00
|
|
|
})}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
QSUSB = {}
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_QWIKSWITCH = SUPPORT_BRIGHTNESS
|
|
|
|
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
class QSToggleEntity(object):
|
2016-05-14 21:21:05 +00:00
|
|
|
"""Representation of a Qwikswitch Entity.
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
Implement base QS methods. Modeled around HA ToggleEntity[1] & should only
|
|
|
|
be used in a class that extends both QSToggleEntity *and* ToggleEntity.
|
|
|
|
|
|
|
|
Implemented:
|
|
|
|
- QSLight extends QSToggleEntity and Light[2] (ToggleEntity[1])
|
|
|
|
- QSSwitch extends QSToggleEntity and SwitchDevice[3] (ToggleEntity[1])
|
|
|
|
|
|
|
|
[1] /helpers/entity.py
|
|
|
|
[2] /components/light/__init__.py
|
|
|
|
[3] /components/switch/__init__.py
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, qsitem, qsusb):
|
2016-05-14 21:21:05 +00:00
|
|
|
"""Initialize the ToggleEntity."""
|
2016-05-21 17:12:42 +00:00
|
|
|
from pyqwikswitch import (QS_ID, QS_NAME, QSType, PQS_VALUE, PQS_TYPE)
|
|
|
|
self._id = qsitem[QS_ID]
|
|
|
|
self._name = qsitem[QS_NAME]
|
|
|
|
self._value = qsitem[PQS_VALUE]
|
2016-05-13 04:39:30 +00:00
|
|
|
self._qsusb = qsusb
|
2016-05-21 17:12:42 +00:00
|
|
|
self._dim = qsitem[PQS_TYPE] == QSType.dimmer
|
2016-07-24 00:03:29 +00:00
|
|
|
QSUSB[self._id] = self
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..100."""
|
|
|
|
return self._value if self._dim else None
|
|
|
|
|
|
|
|
# pylint: disable=no-self-use
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
2016-05-21 14:59:52 +00:00
|
|
|
"""No polling needed."""
|
2016-05-13 04:39:30 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the light."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-05-21 14:59:52 +00:00
|
|
|
"""Check if device is on (non-zero)."""
|
2016-05-13 04:39:30 +00:00
|
|
|
return self._value > 0
|
|
|
|
|
|
|
|
def update_value(self, value):
|
2016-05-21 14:59:52 +00:00
|
|
|
"""Decode the QSUSB value and update the Home assistant state."""
|
2016-05-14 21:21:05 +00:00
|
|
|
if value != self._value:
|
|
|
|
self._value = value
|
|
|
|
# pylint: disable=no-member
|
2017-03-04 23:10:36 +00:00
|
|
|
super().schedule_update_ha_state() # Part of Entity/ToggleEntity
|
2016-05-13 04:39:30 +00:00
|
|
|
return self._value
|
|
|
|
|
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the device on."""
|
2016-05-21 17:12:42 +00:00
|
|
|
newvalue = 255
|
2016-05-13 04:39:30 +00:00
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2016-05-21 17:12:42 +00:00
|
|
|
newvalue = kwargs[ATTR_BRIGHTNESS]
|
|
|
|
if self._qsusb.set(self._id, round(min(newvalue, 255)/2.55)) >= 0:
|
|
|
|
self.update_value(newvalue)
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the device off."""
|
2016-05-21 17:12:42 +00:00
|
|
|
if self._qsusb.set(self._id, 0) >= 0:
|
|
|
|
self.update_value(0)
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
|
2016-07-24 00:03:29 +00:00
|
|
|
class QSSwitch(QSToggleEntity, SwitchDevice):
|
|
|
|
"""Switch based on a Qwikswitch relay module."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class QSLight(QSToggleEntity, Light):
|
|
|
|
"""Light based on a Qwikswitch relay/dimmer module."""
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_QWIKSWITCH
|
2016-07-24 00:03:29 +00:00
|
|
|
|
|
|
|
|
2016-05-13 04:39:30 +00:00
|
|
|
def setup(hass, config):
|
|
|
|
"""Setup the QSUSB component."""
|
2017-01-17 22:40:34 +00:00
|
|
|
from pyqwikswitch import (
|
|
|
|
QSUsb, CMD_BUTTONS, QS_NAME, QS_ID, QS_CMD, PQS_VALUE, PQS_TYPE,
|
|
|
|
QSType)
|
2016-05-14 21:21:05 +00:00
|
|
|
|
|
|
|
# Override which cmd's in /&listen packets will fire events
|
|
|
|
# By default only buttons of type [TOGGLE,SCENE EXE,LEVEL]
|
2017-01-17 22:40:34 +00:00
|
|
|
cmd_buttons = config[DOMAIN].get(CONF_BUTTON_EVENTS, ','.join(CMD_BUTTONS))
|
2016-05-14 21:21:05 +00:00
|
|
|
cmd_buttons = cmd_buttons.split(',')
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2017-01-17 22:40:34 +00:00
|
|
|
url = config[DOMAIN][CONF_URL]
|
|
|
|
dimmer_adjust = config[DOMAIN][CONF_DIMMER_ADJUST]
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2016-07-24 00:03:29 +00:00
|
|
|
qsusb = QSUsb(url, _LOGGER, dimmer_adjust)
|
2016-05-14 21:21:05 +00:00
|
|
|
|
2016-07-24 00:03:29 +00:00
|
|
|
def _stop(event):
|
|
|
|
"""Stop the listener queue and clean up."""
|
|
|
|
nonlocal qsusb
|
|
|
|
qsusb.stop()
|
|
|
|
qsusb = None
|
|
|
|
global QSUSB
|
2016-05-13 04:39:30 +00:00
|
|
|
QSUSB = {}
|
2016-07-24 00:03:29 +00:00
|
|
|
_LOGGER.info("Waiting for long poll to QSUSB to time out")
|
|
|
|
|
|
|
|
hass.bus.listen(EVENT_HOMEASSISTANT_STOP, _stop)
|
|
|
|
|
|
|
|
# Discover all devices in QSUSB
|
|
|
|
devices = qsusb.devices()
|
|
|
|
QSUSB['switch'] = []
|
|
|
|
QSUSB['light'] = []
|
|
|
|
for item in devices:
|
|
|
|
if item[PQS_TYPE] == QSType.relay and (item[QS_NAME].lower()
|
|
|
|
.endswith(' switch')):
|
|
|
|
item[QS_NAME] = item[QS_NAME][:-7] # Remove ' switch' postfix
|
|
|
|
QSUSB['switch'].append(QSSwitch(item, qsusb))
|
|
|
|
elif item[PQS_TYPE] in [QSType.relay, QSType.dimmer]:
|
|
|
|
QSUSB['light'].append(QSLight(item, qsusb))
|
|
|
|
else:
|
|
|
|
_LOGGER.warning("Ignored unknown QSUSB device: %s", item)
|
|
|
|
|
|
|
|
# Load platforms
|
2016-05-13 04:39:30 +00:00
|
|
|
for comp_name in ('switch', 'light'):
|
2017-04-24 03:41:09 +00:00
|
|
|
if QSUSB[comp_name]:
|
2016-07-24 00:03:29 +00:00
|
|
|
load_platform(hass, comp_name, 'qwikswitch', {}, config)
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
def qs_callback(item):
|
2016-05-21 14:59:52 +00:00
|
|
|
"""Typically a button press or update signal."""
|
2016-07-24 00:03:29 +00:00
|
|
|
if qsusb is None: # Shutting down
|
2017-04-24 03:41:09 +00:00
|
|
|
_LOGGER.info("Botton press or updating signal done")
|
2016-07-24 00:03:29 +00:00
|
|
|
return
|
|
|
|
|
2016-05-13 04:39:30 +00:00
|
|
|
# If button pressed, fire a hass event
|
2016-05-21 17:12:42 +00:00
|
|
|
if item.get(QS_CMD, '') in cmd_buttons:
|
|
|
|
hass.bus.fire('qwikswitch.button.' + item.get(QS_ID, '@no_id'))
|
2016-05-13 04:39:30 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Update all ha_objects
|
|
|
|
qsreply = qsusb.devices()
|
|
|
|
if qsreply is False:
|
|
|
|
return
|
|
|
|
for item in qsreply:
|
2016-07-24 00:03:29 +00:00
|
|
|
if item[QS_ID] in QSUSB:
|
|
|
|
QSUSB[item[QS_ID]].update_value(
|
2016-05-21 17:12:42 +00:00
|
|
|
round(min(item[PQS_VALUE], 100) * 2.55))
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2016-07-24 00:03:29 +00:00
|
|
|
def _start(event):
|
|
|
|
"""Start listening."""
|
|
|
|
qsusb.listen(callback=qs_callback, timeout=30)
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start)
|
|
|
|
|
2016-05-13 04:39:30 +00:00
|
|
|
return True
|