mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 07:17:12 +00:00
Resolved UI flicker, new config vars, brightness up to 255, fixed buttons, fixed race condition (#2072)
This commit is contained in:
parent
0adc853741
commit
6dae005b65
@ -27,10 +27,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
qsusb = qwikswitch.QSUSB[discovery_info['qsusb_id']]
|
qsusb = qwikswitch.QSUSB[discovery_info['qsusb_id']]
|
||||||
|
|
||||||
for item in qsusb.ha_devices:
|
for item in qsusb.ha_devices:
|
||||||
if item['id'] in qsusb.ha_objects or \
|
if item['type'] not in ['dim', 'rel']:
|
||||||
item['type'] not in ['dim', 'rel']:
|
|
||||||
continue
|
|
||||||
if item['type'] == 'rel' and item['name'].lower().endswith(' switch'):
|
|
||||||
continue
|
continue
|
||||||
dev = QSLight(item, qsusb)
|
dev = QSLight(item, qsusb)
|
||||||
add_devices([dev])
|
add_devices([dev])
|
||||||
|
@ -21,7 +21,7 @@ QSUSB = None
|
|||||||
|
|
||||||
|
|
||||||
class QSToggleEntity(object):
|
class QSToggleEntity(object):
|
||||||
"""Representation of a Qwikswitch Entiry.
|
"""Representation of a Qwikswitch Entity.
|
||||||
|
|
||||||
Implement base QS methods. Modeled around HA ToggleEntity[1] & should only
|
Implement base QS methods. Modeled around HA ToggleEntity[1] & should only
|
||||||
be used in a class that extends both QSToggleEntity *and* ToggleEntity.
|
be used in a class that extends both QSToggleEntity *and* ToggleEntity.
|
||||||
@ -36,7 +36,7 @@ class QSToggleEntity(object):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, qsitem, qsusb):
|
def __init__(self, qsitem, qsusb):
|
||||||
"""Initialize the light."""
|
"""Initialize the ToggleEntity."""
|
||||||
self._id = qsitem['id']
|
self._id = qsitem['id']
|
||||||
self._name = qsitem['name']
|
self._name = qsitem['name']
|
||||||
self._qsusb = qsusb
|
self._qsusb = qsusb
|
||||||
@ -66,34 +66,41 @@ class QSToggleEntity(object):
|
|||||||
|
|
||||||
def update_value(self, value):
|
def update_value(self, value):
|
||||||
"""Decode QSUSB value & update HA state."""
|
"""Decode QSUSB value & update HA state."""
|
||||||
self._value = value
|
if value != self._value:
|
||||||
# pylint: disable=no-member
|
self._value = value
|
||||||
super().update_ha_state() # Part of Entity/ToggleEntity
|
# pylint: disable=no-member
|
||||||
|
super().update_ha_state() # Part of Entity/ToggleEntity
|
||||||
return self._value
|
return self._value
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the device on."""
|
"""Turn the device on."""
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
self._value = kwargs[ATTR_BRIGHTNESS]
|
self.update_value(kwargs[ATTR_BRIGHTNESS])
|
||||||
else:
|
else:
|
||||||
self._value = 100
|
self.update_value(255)
|
||||||
return self._qsusb.set(self._id, self._value)
|
|
||||||
|
return self._qsusb.set(self._id, round(min(self._value, 255)/2.55))
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the device off."""
|
"""Turn the device off."""
|
||||||
|
self.update_value(0)
|
||||||
return self._qsusb.set(self._id, 0)
|
return self._qsusb.set(self._id, 0)
|
||||||
|
|
||||||
|
|
||||||
# pylint: disable=too-many-locals
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Setup the QSUSB component."""
|
"""Setup the QSUSB component."""
|
||||||
from pyqwikswitch import QSUsb
|
from pyqwikswitch import QSUsb, CMD_BUTTONS
|
||||||
|
|
||||||
|
# Override which cmd's in /&listen packets will fire events
|
||||||
|
# By default only buttons of type [TOGGLE,SCENE EXE,LEVEL]
|
||||||
|
cmd_buttons = config[DOMAIN].get('button_events', ','.join(CMD_BUTTONS))
|
||||||
|
cmd_buttons = cmd_buttons.split(',')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
url = config[DOMAIN].get('url', 'http://127.0.0.1:2020')
|
url = config[DOMAIN].get('url', 'http://127.0.0.1:2020')
|
||||||
qsusb = QSUsb(url, _LOGGER)
|
dimmer_adjust = float(config[DOMAIN].get('dimmer_adjust', '1'))
|
||||||
|
qsusb = QSUsb(url, _LOGGER, dimmer_adjust)
|
||||||
|
|
||||||
# Ensure qsusb terminates threads correctly
|
# Ensure qsusb terminates threads correctly
|
||||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
||||||
@ -105,25 +112,27 @@ def setup(hass, config):
|
|||||||
qsusb.ha_devices = qsusb.devices()
|
qsusb.ha_devices = qsusb.devices()
|
||||||
qsusb.ha_objects = {}
|
qsusb.ha_objects = {}
|
||||||
|
|
||||||
|
# Identify switches & remove ' Switch' postfix in name
|
||||||
|
for item in qsusb.ha_devices:
|
||||||
|
if item['type'] == 'rel' and item['name'].lower().endswith(' switch'):
|
||||||
|
item['type'] = 'switch'
|
||||||
|
item['name'] = item['name'][:-7]
|
||||||
|
|
||||||
global QSUSB
|
global QSUSB
|
||||||
if QSUSB is None:
|
if QSUSB is None:
|
||||||
QSUSB = {}
|
QSUSB = {}
|
||||||
QSUSB[id(qsusb)] = qsusb
|
QSUSB[id(qsusb)] = qsusb
|
||||||
|
|
||||||
# Register add_device callbacks onto the gloabl ADD_DEVICES
|
# Load sub-components for qwikswitch
|
||||||
# Switch called first since they are [type=rel] and end with ' switch'
|
|
||||||
for comp_name in ('switch', 'light'):
|
for comp_name in ('switch', 'light'):
|
||||||
load_platform(hass, comp_name, 'qwikswitch',
|
load_platform(hass, comp_name, 'qwikswitch',
|
||||||
{'qsusb_id': id(qsusb)}, config)
|
{'qsusb_id': id(qsusb)}, config)
|
||||||
|
|
||||||
def qs_callback(item):
|
def qs_callback(item):
|
||||||
"""Typically a btn press or update signal."""
|
"""Typically a btn press or update signal."""
|
||||||
from pyqwikswitch import CMD_BUTTONS
|
|
||||||
|
|
||||||
# If button pressed, fire a hass event
|
# If button pressed, fire a hass event
|
||||||
if item.get('type', '') in CMD_BUTTONS:
|
if item.get('cmd', '') in cmd_buttons:
|
||||||
_LOGGER.info('qwikswitch.button.%s', item['id'])
|
hass.bus.fire('qwikswitch.button.' + item.get('id', '@no_id'))
|
||||||
hass.bus.fire('qwikswitch.button.{}'.format(item['id']))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
# Update all ha_objects
|
# Update all ha_objects
|
||||||
@ -133,7 +142,8 @@ def setup(hass, config):
|
|||||||
for item in qsreply:
|
for item in qsreply:
|
||||||
item_id = item.get('id', '')
|
item_id = item.get('id', '')
|
||||||
if item_id in qsusb.ha_objects:
|
if item_id in qsusb.ha_objects:
|
||||||
qsusb.ha_objects[item_id].update_value(item['value'])
|
qsusb.ha_objects[item_id].update_value(
|
||||||
|
round(min(item['value'], 100) * 2.55))
|
||||||
|
|
||||||
qsusb.listen(callback=qs_callback, timeout=10)
|
qsusb.listen(callback=qs_callback, timeout=30)
|
||||||
return True
|
return True
|
||||||
|
@ -27,10 +27,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
|||||||
qsusb = qwikswitch.QSUSB[discovery_info['qsusb_id']]
|
qsusb = qwikswitch.QSUSB[discovery_info['qsusb_id']]
|
||||||
|
|
||||||
for item in qsusb.ha_devices:
|
for item in qsusb.ha_devices:
|
||||||
if item['type'] == 'rel' and \
|
if item['type'] == 'switch':
|
||||||
item['name'].lower().endswith(' switch'):
|
|
||||||
# Remove the ' Switch' name postfix for HA
|
|
||||||
item['name'] = item['name'][:-7]
|
|
||||||
dev = QSSwitch(item, qsusb)
|
dev = QSSwitch(item, qsusb)
|
||||||
add_devices([dev])
|
add_devices([dev])
|
||||||
qsusb.ha_objects[item['id']] = dev
|
qsusb.ha_objects[item['id']] = dev
|
||||||
|
Loading…
x
Reference in New Issue
Block a user