mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Qwikswitch async & updates (#12641)
This commit is contained in:
parent
6d20a84f0e
commit
3e3f710b12
@ -6,19 +6,16 @@ https://home-assistant.io/components/light.qwikswitch/
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import homeassistant.components.qwikswitch as qwikswitch
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
DEPENDENCIES = ['qwikswitch']
|
DEPENDENCIES = ['qwikswitch']
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Set up the lights from the main Qwikswitch component."""
|
"""Add lights from the main Qwikswitch component."""
|
||||||
if discovery_info is None:
|
if discovery_info is None:
|
||||||
_LOGGER.error("Configure Qwikswitch component failed")
|
logging.getLogger(__name__).error(
|
||||||
|
"Configure Qwikswitch Light component failed")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
add_devices(qwikswitch.QSUSB['light'])
|
add_devices(hass.data['qwikswitch']['light'])
|
||||||
return True
|
return True
|
||||||
|
@ -4,18 +4,21 @@ Support for Qwikswitch devices.
|
|||||||
For more details about this component, please refer to the documentation at
|
For more details about this component, please refer to the documentation at
|
||||||
https://home-assistant.io/components/qwikswitch/
|
https://home-assistant.io/components/qwikswitch/
|
||||||
"""
|
"""
|
||||||
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, CONF_URL)
|
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, CONF_URL)
|
||||||
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.discovery import load_platform
|
from homeassistant.helpers.discovery import load_platform
|
||||||
from homeassistant.components.light import (
|
from homeassistant.components.light import (
|
||||||
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
|
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
|
||||||
from homeassistant.components.switch import SwitchDevice
|
from homeassistant.components.switch import SwitchDevice
|
||||||
|
|
||||||
REQUIREMENTS = ['pyqwikswitch==0.4']
|
REQUIREMENTS = ['pyqwikswitch==0.5']
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -33,10 +36,6 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
vol.Optional(CONF_BUTTON_EVENTS): vol.Coerce(str)
|
vol.Optional(CONF_BUTTON_EVENTS): vol.Coerce(str)
|
||||||
})}, extra=vol.ALLOW_EXTRA)
|
})}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
QSUSB = {}
|
|
||||||
|
|
||||||
SUPPORT_QWIKSWITCH = SUPPORT_BRIGHTNESS
|
|
||||||
|
|
||||||
|
|
||||||
class QSToggleEntity(object):
|
class QSToggleEntity(object):
|
||||||
"""Representation of a Qwikswitch Entity.
|
"""Representation of a Qwikswitch Entity.
|
||||||
@ -53,22 +52,15 @@ class QSToggleEntity(object):
|
|||||||
[3] /components/switch/__init__.py
|
[3] /components/switch/__init__.py
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, qsitem, qsusb):
|
def __init__(self, qsid, qsusb):
|
||||||
"""Initialize the ToggleEntity."""
|
"""Initialize the ToggleEntity."""
|
||||||
from pyqwikswitch import (QS_ID, QS_NAME, QSType, PQS_VALUE, PQS_TYPE)
|
from pyqwikswitch import (QS_NAME, QSDATA, QS_TYPE, QSType)
|
||||||
self._id = qsitem[QS_ID]
|
self._id = qsid
|
||||||
self._name = qsitem[QS_NAME]
|
self._qsusb = qsusb.devices
|
||||||
self._value = qsitem[PQS_VALUE]
|
dev = qsusb.devices[qsid]
|
||||||
self._qsusb = qsusb
|
self._dim = dev[QS_TYPE] == QSType.dimmer
|
||||||
self._dim = qsitem[PQS_TYPE] == QSType.dimmer
|
self._name = dev[QSDATA][QS_NAME]
|
||||||
QSUSB[self._id] = self
|
|
||||||
|
|
||||||
@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
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
"""No polling needed."""
|
"""No polling needed."""
|
||||||
@ -82,29 +74,27 @@ class QSToggleEntity(object):
|
|||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
"""Check if device is on (non-zero)."""
|
"""Check if device is on (non-zero)."""
|
||||||
return self._value > 0
|
return self._qsusb[self._id, 1] > 0
|
||||||
|
|
||||||
def update_value(self, value):
|
|
||||||
"""Decode the QSUSB value and update the Home assistant state."""
|
|
||||||
if value != self._value:
|
|
||||||
self._value = value
|
|
||||||
# pylint: disable=no-member
|
|
||||||
super().schedule_update_ha_state() # Part of Entity/ToggleEntity
|
|
||||||
return self._value
|
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
newvalue = 255
|
new = kwargs.get(ATTR_BRIGHTNESS, 255)
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
self._qsusb.set_value(self._id, new)
|
||||||
newvalue = kwargs[ATTR_BRIGHTNESS]
|
|
||||||
if self._qsusb.set(self._id, round(min(newvalue, 255)/2.55)) >= 0:
|
|
||||||
self.update_value(newvalue)
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
@asyncio.coroutine
|
||||||
def turn_off(self, **kwargs):
|
def async_turn_on(self, **kwargs):
|
||||||
|
"""Turn the device on."""
|
||||||
|
new = kwargs.get(ATTR_BRIGHTNESS, 255)
|
||||||
|
self._qsusb.set_value(self._id, new)
|
||||||
|
|
||||||
|
def turn_off(self, **kwargs): # pylint: disable=unused-argument
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
if self._qsusb.set(self._id, 0) >= 0:
|
self._qsusb.set_value(self._id, 0)
|
||||||
self.update_value(0)
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
|
||||||
|
"""Turn the device off."""
|
||||||
|
self._qsusb.set_value(self._id, 0)
|
||||||
|
|
||||||
|
|
||||||
class QSSwitch(QSToggleEntity, SwitchDevice):
|
class QSSwitch(QSToggleEntity, SwitchDevice):
|
||||||
@ -116,17 +106,25 @@ class QSSwitch(QSToggleEntity, SwitchDevice):
|
|||||||
class QSLight(QSToggleEntity, Light):
|
class QSLight(QSToggleEntity, Light):
|
||||||
"""Light based on a Qwikswitch relay/dimmer module."""
|
"""Light based on a Qwikswitch relay/dimmer module."""
|
||||||
|
|
||||||
|
@property
|
||||||
|
def brightness(self):
|
||||||
|
"""Return the brightness of this light (0-255)."""
|
||||||
|
return self._qsusb[self._id, 1] if self._dim else None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def supported_features(self):
|
def supported_features(self):
|
||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
return SUPPORT_QWIKSWITCH
|
return SUPPORT_BRIGHTNESS if self._dim else None
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
@asyncio.coroutine
|
||||||
"""Set up the QSUSB component."""
|
def async_setup(hass, config):
|
||||||
|
"""Setup qwiskswitch component."""
|
||||||
|
from pyqwikswitch.async import QSUsb
|
||||||
from pyqwikswitch import (
|
from pyqwikswitch import (
|
||||||
QSUsb, CMD_BUTTONS, QS_NAME, QS_ID, QS_CMD, PQS_VALUE, PQS_TYPE,
|
CMD_BUTTONS, QS_CMD, QSDATA, QS_ID, QS_NAME, QS_TYPE, QSType)
|
||||||
QSType)
|
|
||||||
|
hass.data[DOMAIN] = {}
|
||||||
|
|
||||||
# Override which cmd's in /&listen packets will fire events
|
# Override which cmd's in /&listen packets will fire events
|
||||||
# By default only buttons of type [TOGGLE,SCENE EXE,LEVEL]
|
# By default only buttons of type [TOGGLE,SCENE EXE,LEVEL]
|
||||||
@ -136,61 +134,69 @@ def setup(hass, config):
|
|||||||
url = config[DOMAIN][CONF_URL]
|
url = config[DOMAIN][CONF_URL]
|
||||||
dimmer_adjust = config[DOMAIN][CONF_DIMMER_ADJUST]
|
dimmer_adjust = config[DOMAIN][CONF_DIMMER_ADJUST]
|
||||||
|
|
||||||
qsusb = QSUsb(url, _LOGGER, dimmer_adjust)
|
def callback_value_changed(qsdevices, key, new): \
|
||||||
|
# pylint: disable=unused-argument
|
||||||
|
"""Update entiry values based on device change."""
|
||||||
|
entity = hass.data[DOMAIN].get(key)
|
||||||
|
if entity is not None:
|
||||||
|
entity.schedule_update_ha_state() # Part of Entity/ToggleEntity
|
||||||
|
|
||||||
def _stop(event):
|
session = async_get_clientsession(hass)
|
||||||
|
qsusb = QSUsb(url=url, dim_adj=dimmer_adjust, session=session,
|
||||||
|
callback_value_changed=callback_value_changed)
|
||||||
|
|
||||||
|
@callback
|
||||||
|
def async_stop(event): # pylint: disable=unused-argument
|
||||||
"""Stop the listener queue and clean up."""
|
"""Stop the listener queue and clean up."""
|
||||||
nonlocal qsusb
|
nonlocal qsusb
|
||||||
qsusb.stop()
|
qsusb.stop()
|
||||||
qsusb = None
|
qsusb = None
|
||||||
global QSUSB
|
hass.data[DOMAIN] = {}
|
||||||
QSUSB = {}
|
|
||||||
_LOGGER.info("Waiting for long poll to QSUSB to time out")
|
_LOGGER.info("Waiting for long poll to QSUSB to time out")
|
||||||
|
|
||||||
hass.bus.listen(EVENT_HOMEASSISTANT_STOP, _stop)
|
hass.bus.async_listen(EVENT_HOMEASSISTANT_STOP, async_stop)
|
||||||
|
|
||||||
# Discover all devices in QSUSB
|
# Discover all devices in QSUSB
|
||||||
devices = qsusb.devices()
|
yield from qsusb.update_from_devices()
|
||||||
QSUSB['switch'] = []
|
hass.data[DOMAIN]['switch'] = []
|
||||||
QSUSB['light'] = []
|
hass.data[DOMAIN]['light'] = []
|
||||||
for item in devices:
|
for _id, item in qsusb.devices:
|
||||||
if item[PQS_TYPE] == QSType.relay and (item[QS_NAME].lower()
|
if (item[QS_TYPE] == QSType.relay and
|
||||||
.endswith(' switch')):
|
item[QSDATA][QS_NAME].lower().endswith(' switch')):
|
||||||
item[QS_NAME] = item[QS_NAME][:-7] # Remove ' switch' postfix
|
item[QSDATA][QS_NAME] = item[QSDATA][QS_NAME][:-7] # Remove switch
|
||||||
QSUSB['switch'].append(QSSwitch(item, qsusb))
|
new_dev = QSSwitch(_id, qsusb)
|
||||||
elif item[PQS_TYPE] in [QSType.relay, QSType.dimmer]:
|
hass.data[DOMAIN]['switch'].append(new_dev)
|
||||||
QSUSB['light'].append(QSLight(item, qsusb))
|
elif item[QS_TYPE] in [QSType.relay, QSType.dimmer]:
|
||||||
|
new_dev = QSLight(_id, qsusb)
|
||||||
|
hass.data[DOMAIN]['light'].append(new_dev)
|
||||||
else:
|
else:
|
||||||
_LOGGER.warning("Ignored unknown QSUSB device: %s", item)
|
_LOGGER.warning("Ignored unknown QSUSB device: %s", item)
|
||||||
|
continue
|
||||||
|
hass.data[DOMAIN][_id] = new_dev
|
||||||
|
|
||||||
# Load platforms
|
# Load platforms
|
||||||
for comp_name in ('switch', 'light'):
|
for comp_name in ('switch', 'light'):
|
||||||
if QSUSB[comp_name]:
|
if hass.data[DOMAIN][comp_name]:
|
||||||
load_platform(hass, comp_name, 'qwikswitch', {}, config)
|
load_platform(hass, comp_name, 'qwikswitch', {}, config)
|
||||||
|
|
||||||
def qs_callback(item):
|
def callback_qs_listen(item):
|
||||||
"""Typically a button press or update signal."""
|
"""Typically a button press or update signal."""
|
||||||
if qsusb is None: # Shutting down
|
if qsusb is None: # Shutting down
|
||||||
_LOGGER.info("Button press or updating signal done")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# If button pressed, fire a hass event
|
# If button pressed, fire a hass event
|
||||||
if item.get(QS_CMD, '') in cmd_buttons:
|
if item.get(QS_CMD, '') in cmd_buttons and QS_ID in item:
|
||||||
hass.bus.fire('qwikswitch.button.' + item.get(QS_ID, '@no_id'))
|
hass.bus.async_fire('qwikswitch.button.{}'.format(item[QS_ID]))
|
||||||
return
|
return
|
||||||
|
|
||||||
# Update all ha_objects
|
# Update all ha_objects
|
||||||
qsreply = qsusb.devices()
|
hass.async_add_job(qsusb.update_from_devices)
|
||||||
if qsreply is False:
|
|
||||||
return
|
|
||||||
for itm in qsreply:
|
|
||||||
if itm[QS_ID] in QSUSB:
|
|
||||||
QSUSB[itm[QS_ID]].update_value(
|
|
||||||
round(min(itm[PQS_VALUE], 100) * 2.55))
|
|
||||||
|
|
||||||
def _start(event):
|
@callback
|
||||||
|
def async_start(event): # pylint: disable=unused-argument
|
||||||
"""Start listening."""
|
"""Start listening."""
|
||||||
qsusb.listen(callback=qs_callback, timeout=30)
|
hass.async_add_job(qsusb.listen, callback_qs_listen)
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, _start)
|
|
||||||
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, async_start)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -6,19 +6,16 @@ https://home-assistant.io/components/switch.qwikswitch/
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import homeassistant.components.qwikswitch as qwikswitch
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
DEPENDENCIES = ['qwikswitch']
|
DEPENDENCIES = ['qwikswitch']
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
"""Add switched from the main Qwikswitch component."""
|
"""Add switches from the main Qwikswitch component."""
|
||||||
if discovery_info is None:
|
if discovery_info is None:
|
||||||
_LOGGER.error("Configure Qwikswitch component")
|
logging.getLogger(__name__).error(
|
||||||
|
"Configure Qwikswitch Switch component failed")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
add_devices(qwikswitch.QSUSB['switch'])
|
add_devices(hass.data['qwikswitch']['switch'])
|
||||||
return True
|
return True
|
||||||
|
@ -860,7 +860,7 @@ pyowm==2.8.0
|
|||||||
pypollencom==1.1.1
|
pypollencom==1.1.1
|
||||||
|
|
||||||
# homeassistant.components.qwikswitch
|
# homeassistant.components.qwikswitch
|
||||||
pyqwikswitch==0.4
|
pyqwikswitch==0.5
|
||||||
|
|
||||||
# homeassistant.components.rainbird
|
# homeassistant.components.rainbird
|
||||||
pyrainbird==0.1.3
|
pyrainbird==0.1.3
|
||||||
|
Loading…
x
Reference in New Issue
Block a user