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 (
|
2018-03-29 21:29:46 +00:00
|
|
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, CONF_URL,
|
|
|
|
CONF_SENSORS, CONF_SWITCHES)
|
2018-03-25 21:32:13 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
2016-07-24 00:03:29 +00:00
|
|
|
from homeassistant.helpers.discovery import load_platform
|
2018-03-29 21:29:46 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
from homeassistant.components.light import ATTR_BRIGHTNESS
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-07-24 00:03:29 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
REQUIREMENTS = ['pyqwikswitch==0.6']
|
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,
|
2018-03-29 21:29:46 +00:00
|
|
|
vol.Optional(CONF_BUTTON_EVENTS, default=[]): cv.ensure_list_csv,
|
|
|
|
vol.Optional(CONF_SENSORS, default={}): vol.Schema({cv.slug: str}),
|
|
|
|
vol.Optional(CONF_SWITCHES, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [str])
|
2016-07-24 00:03:29 +00:00
|
|
|
})}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
class QSToggleEntity(Entity):
|
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
|
|
|
|
"""
|
|
|
|
|
2018-03-25 21:32:13 +00:00
|
|
|
def __init__(self, qsid, qsusb):
|
2016-05-14 21:21:05 +00:00
|
|
|
"""Initialize the ToggleEntity."""
|
2018-03-25 21:32:13 +00:00
|
|
|
from pyqwikswitch import (QS_NAME, QSDATA, QS_TYPE, QSType)
|
2018-03-29 21:29:46 +00:00
|
|
|
self.qsid = qsid
|
2018-03-25 21:32:13 +00:00
|
|
|
self._qsusb = qsusb.devices
|
|
|
|
dev = qsusb.devices[qsid]
|
|
|
|
self._dim = dev[QS_TYPE] == QSType.dimmer
|
|
|
|
self._name = dev[QSDATA][QS_NAME]
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
@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)."""
|
2018-03-29 21:29:46 +00:00
|
|
|
return self._qsusb[self.qsid, 1] > 0
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
async def async_turn_on(self, **kwargs):
|
2016-05-13 04:39:30 +00:00
|
|
|
"""Turn the device on."""
|
2018-03-25 21:32:13 +00:00
|
|
|
new = kwargs.get(ATTR_BRIGHTNESS, 255)
|
2018-03-29 21:29:46 +00:00
|
|
|
self._qsusb.set_value(self.qsid, new)
|
2018-03-25 21:32:13 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
async def async_turn_off(self, **_):
|
2016-05-13 04:39:30 +00:00
|
|
|
"""Turn the device off."""
|
2018-03-29 21:29:46 +00:00
|
|
|
self._qsusb.set_value(self.qsid, 0)
|
2016-07-24 00:03:29 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
def _update(self, _packet=None):
|
|
|
|
"""Schedule an update - match dispather_send signature."""
|
|
|
|
self.async_schedule_update_ha_state()
|
2016-07-24 00:03:29 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Listen for updates from QSUSb via dispatcher."""
|
|
|
|
self.hass.helpers.dispatcher.async_dispatcher_connect(
|
|
|
|
self.qsid, self._update)
|
2016-07-24 00:03:29 +00:00
|
|
|
|
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Qwiskswitch component setup."""
|
|
|
|
from pyqwikswitch.async_ import QSUsb
|
2017-01-17 22:40:34 +00:00
|
|
|
from pyqwikswitch import (
|
2018-03-29 21:29:46 +00:00
|
|
|
CMD_BUTTONS, QS_CMD, QS_ID, QS_TYPE, QSType)
|
2016-05-14 21:21:05 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
# Add cmd's to in /&listen packets will fire events
|
2016-05-14 21:21:05 +00:00
|
|
|
# By default only buttons of type [TOGGLE,SCENE EXE,LEVEL]
|
2018-03-29 21:29:46 +00:00
|
|
|
cmd_buttons = set(CMD_BUTTONS)
|
|
|
|
for btn in config[DOMAIN][CONF_BUTTON_EVENTS]:
|
|
|
|
cmd_buttons.add(btn)
|
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]
|
2018-03-29 21:29:46 +00:00
|
|
|
sensors = config[DOMAIN]['sensors']
|
|
|
|
switches = config[DOMAIN]['switches']
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
def callback_value_changed(_qsd, qsid, _val):
|
|
|
|
"""Update entity values based on device change."""
|
|
|
|
_LOGGER.debug("Dispatch %s (update from devices)", qsid)
|
|
|
|
hass.helpers.dispatcher.async_dispatcher_send(qsid, None)
|
2018-03-25 21:32:13 +00:00
|
|
|
|
|
|
|
session = async_get_clientsession(hass)
|
|
|
|
qsusb = QSUsb(url=url, dim_adj=dimmer_adjust, session=session,
|
|
|
|
callback_value_changed=callback_value_changed)
|
2016-05-14 21:21:05 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
# Discover all devices in QSUSB
|
|
|
|
if not await qsusb.update_from_devices():
|
|
|
|
return False
|
2016-07-24 00:03:29 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
hass.data[DOMAIN] = qsusb
|
2016-07-24 00:03:29 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
_new = {'switch': [], 'light': [], 'sensor': sensors}
|
2018-03-25 21:32:13 +00:00
|
|
|
for _id, item in qsusb.devices:
|
2018-03-29 21:29:46 +00:00
|
|
|
if _id in switches:
|
|
|
|
if item[QS_TYPE] != QSType.relay:
|
|
|
|
_LOGGER.warning(
|
|
|
|
"You specified a switch that is not a relay %s", _id)
|
|
|
|
continue
|
|
|
|
_new['switch'].append(_id)
|
2018-03-25 21:32:13 +00:00
|
|
|
elif item[QS_TYPE] in [QSType.relay, QSType.dimmer]:
|
2018-03-29 21:29:46 +00:00
|
|
|
_new['light'].append(_id)
|
2016-07-24 00:03:29 +00:00
|
|
|
else:
|
|
|
|
_LOGGER.warning("Ignored unknown QSUSB device: %s", item)
|
2018-03-25 21:32:13 +00:00
|
|
|
continue
|
2016-07-24 00:03:29 +00:00
|
|
|
|
|
|
|
# Load platforms
|
2018-03-29 21:29:46 +00:00
|
|
|
for comp_name, comp_conf in _new.items():
|
|
|
|
if comp_conf:
|
|
|
|
load_platform(hass, comp_name, DOMAIN, {DOMAIN: comp_conf}, config)
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2018-03-25 21:32:13 +00:00
|
|
|
def callback_qs_listen(item):
|
2016-05-21 14:59:52 +00:00
|
|
|
"""Typically a button press or update signal."""
|
2016-05-13 04:39:30 +00:00
|
|
|
# If button pressed, fire a hass event
|
2018-03-29 21:29:46 +00:00
|
|
|
if QS_ID in item:
|
|
|
|
if item.get(QS_CMD, '') in cmd_buttons:
|
|
|
|
hass.bus.async_fire(
|
|
|
|
'qwikswitch.button.{}'.format(item[QS_ID]), item)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Private method due to bad __iter__ design in qsusb
|
|
|
|
# qsusb.devices returns a list of tuples
|
|
|
|
if item[QS_ID] not in \
|
|
|
|
qsusb.devices._data: # pylint: disable=protected-access
|
|
|
|
# Not a standard device in, component can handle packet
|
|
|
|
# i.e. sensors
|
|
|
|
_LOGGER.debug("Dispatch %s ((%s))", item[QS_ID], item)
|
|
|
|
hass.helpers.dispatcher.async_dispatcher_send(
|
|
|
|
item[QS_ID], item)
|
2016-05-13 04:39:30 +00:00
|
|
|
|
|
|
|
# Update all ha_objects
|
2018-03-25 21:32:13 +00:00
|
|
|
hass.async_add_job(qsusb.update_from_devices)
|
2016-05-13 04:39:30 +00:00
|
|
|
|
2018-03-25 21:32:13 +00:00
|
|
|
@callback
|
2018-03-29 21:29:46 +00:00
|
|
|
def async_start(_):
|
2016-07-24 00:03:29 +00:00
|
|
|
"""Start listening."""
|
2018-03-25 21:32:13 +00:00
|
|
|
hass.async_add_job(qsusb.listen, callback_qs_listen)
|
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, async_start)
|
2016-07-24 00:03:29 +00:00
|
|
|
|
2018-03-29 21:29:46 +00:00
|
|
|
@callback
|
|
|
|
def async_stop(_):
|
|
|
|
"""Stop the listener queue and clean up."""
|
|
|
|
hass.data[DOMAIN].stop()
|
|
|
|
_LOGGER.info("Waiting for long poll to QSUSB to time out (max 30sec)")
|
|
|
|
|
|
|
|
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, async_stop)
|
|
|
|
|
2016-05-13 04:39:30 +00:00
|
|
|
return True
|