Added QwikSwitch component & platforms (#1970)
* Added QwikSwitch platform farcy - worst than my english teacher * Clean up comments * Import only inside functions * Moved imports, no global var, load_platform * add_device reworked * Only serializable content on bus * Fixed imports & removed some loggingpull/2057/head
parent
f1eda430cd
commit
65ac1ae84a
|
@ -38,6 +38,9 @@ omit =
|
|||
homeassistant/components/octoprint.py
|
||||
homeassistant/components/*/octoprint.py
|
||||
|
||||
homeassistant/components/qwikswitch.py
|
||||
homeassistant/components/*/qwikswitch.py
|
||||
|
||||
homeassistant/components/rpi_gpio.py
|
||||
homeassistant/components/*/rpi_gpio.py
|
||||
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
"""
|
||||
Support for Qwikswitch Relays and Dimmers as HA Lights.
|
||||
|
||||
See the main component for more info
|
||||
"""
|
||||
import logging
|
||||
import homeassistant.components.qwikswitch as qwikswitch
|
||||
from homeassistant.components.light import Light
|
||||
|
||||
DEPENDENCIES = ['qwikswitch']
|
||||
|
||||
|
||||
class QSLight(qwikswitch.QSToggleEntity, Light):
|
||||
"""Light based on a Qwikswitch relay/dimmer module."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Store add_devices for the 'light' components."""
|
||||
if discovery_info is None or 'qsusb_id' not in discovery_info:
|
||||
logging.getLogger(__name__).error(
|
||||
'Configure main Qwikswitch component')
|
||||
return False
|
||||
|
||||
qsusb = qwikswitch.QSUSB[discovery_info['qsusb_id']]
|
||||
|
||||
for item in qsusb.ha_devices:
|
||||
if item['id'] in qsusb.ha_objects or \
|
||||
item['type'] not in ['dim', 'rel']:
|
||||
continue
|
||||
if item['type'] == 'rel' and item['name'].lower().endswith(' switch'):
|
||||
continue
|
||||
dev = QSLight(item, qsusb)
|
||||
add_devices([dev])
|
||||
qsusb.ha_objects[item['id']] = dev
|
|
@ -0,0 +1,139 @@
|
|||
"""
|
||||
Support for Qwikswitch lights and switches.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/qwikswitch
|
||||
"""
|
||||
|
||||
import logging
|
||||
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
|
||||
from homeassistant.components.light import ATTR_BRIGHTNESS
|
||||
from homeassistant.components.discovery import load_platform
|
||||
|
||||
REQUIREMENTS = ['https://github.com/kellerza/pyqwikswitch/archive/v0.1.zip'
|
||||
'#pyqwikswitch==0.1']
|
||||
DEPENDENCIES = []
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DOMAIN = 'qwikswitch'
|
||||
QSUSB = None
|
||||
|
||||
|
||||
class QSToggleEntity(object):
|
||||
"""Representation of a Qwikswitch Entiry.
|
||||
|
||||
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):
|
||||
"""Initialize the light."""
|
||||
self._id = qsitem['id']
|
||||
self._name = qsitem['name']
|
||||
self._qsusb = qsusb
|
||||
self._value = qsitem.get('value', 0)
|
||||
self._dim = qsitem['type'] == 'dim'
|
||||
|
||||
@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):
|
||||
"""State Polling needed."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the light."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Check if On (non-zero)."""
|
||||
return self._value > 0
|
||||
|
||||
def update_value(self, value):
|
||||
"""Decode QSUSB value & update HA state."""
|
||||
self._value = value
|
||||
# pylint: disable=no-member
|
||||
super().update_ha_state() # Part of Entity/ToggleEntity
|
||||
return self._value
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def turn_on(self, **kwargs):
|
||||
"""Turn the device on."""
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
self._value = kwargs[ATTR_BRIGHTNESS]
|
||||
else:
|
||||
self._value = 100
|
||||
return self._qsusb.set(self._id, self._value)
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def turn_off(self, **kwargs):
|
||||
"""Turn the device off."""
|
||||
return self._qsusb.set(self._id, 0)
|
||||
|
||||
|
||||
# pylint: disable=too-many-locals
|
||||
def setup(hass, config):
|
||||
"""Setup the QSUSB component."""
|
||||
from pyqwikswitch import QSUsb
|
||||
|
||||
try:
|
||||
url = config[DOMAIN].get('url', 'http://127.0.0.1:2020')
|
||||
qsusb = QSUsb(url, _LOGGER)
|
||||
|
||||
# Ensure qsusb terminates threads correctly
|
||||
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP,
|
||||
lambda event: qsusb.stop())
|
||||
except ValueError as val_err:
|
||||
_LOGGER.error(str(val_err))
|
||||
return False
|
||||
|
||||
qsusb.ha_devices = qsusb.devices()
|
||||
qsusb.ha_objects = {}
|
||||
|
||||
global QSUSB
|
||||
if QSUSB is None:
|
||||
QSUSB = {}
|
||||
QSUSB[id(qsusb)] = qsusb
|
||||
|
||||
# Register add_device callbacks onto the gloabl ADD_DEVICES
|
||||
# Switch called first since they are [type=rel] and end with ' switch'
|
||||
for comp_name in ('switch', 'light'):
|
||||
load_platform(hass, comp_name, 'qwikswitch',
|
||||
{'qsusb_id': id(qsusb)}, config)
|
||||
|
||||
def qs_callback(item):
|
||||
"""Typically a btn press or update signal."""
|
||||
from pyqwikswitch import CMD_BUTTONS
|
||||
|
||||
# If button pressed, fire a hass event
|
||||
if item.get('type', '') in CMD_BUTTONS:
|
||||
_LOGGER.info('qwikswitch.button.%s', item['id'])
|
||||
hass.bus.fire('qwikswitch.button.{}'.format(item['id']))
|
||||
return
|
||||
|
||||
# Update all ha_objects
|
||||
qsreply = qsusb.devices()
|
||||
if qsreply is False:
|
||||
return
|
||||
for item in qsreply:
|
||||
item_id = item.get('id', '')
|
||||
if item_id in qsusb.ha_objects:
|
||||
qsusb.ha_objects[item_id].update_value(item['value'])
|
||||
|
||||
qsusb.listen(callback=qs_callback, timeout=10)
|
||||
return True
|
|
@ -0,0 +1,36 @@
|
|||
"""
|
||||
Support for Qwikswitch Relays as HA Switches.
|
||||
|
||||
See the main component for more info
|
||||
"""
|
||||
import logging
|
||||
import homeassistant.components.qwikswitch as qwikswitch
|
||||
from homeassistant.components.switch import SwitchDevice
|
||||
|
||||
DEPENDENCIES = ['qwikswitch']
|
||||
|
||||
|
||||
class QSSwitch(qwikswitch.QSToggleEntity, SwitchDevice):
|
||||
"""Switch based on a Qwikswitch relay module."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
# pylint: disable=unused-argument
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Store add_devices for the 'switch' components."""
|
||||
if discovery_info is None or 'qsusb_id' not in discovery_info:
|
||||
logging.getLogger(__name__).error(
|
||||
'Configure main Qwikswitch component')
|
||||
return False
|
||||
|
||||
qsusb = qwikswitch.QSUSB[discovery_info['qsusb_id']]
|
||||
|
||||
for item in qsusb.ha_devices:
|
||||
if item['type'] == 'rel' and \
|
||||
item['name'].lower().endswith(' switch'):
|
||||
# Remove the ' Switch' name postfix for HA
|
||||
item['name'] = item['name'][:-7]
|
||||
dev = QSSwitch(item, qsusb)
|
||||
add_devices([dev])
|
||||
qsusb.ha_objects[item['id']] = dev
|
|
@ -109,6 +109,9 @@ https://github.com/danieljkemp/onkyo-eiscp/archive/python3.zip#onkyo-eiscp==0.9.
|
|||
# homeassistant.components.sensor.sabnzbd
|
||||
https://github.com/jamespcole/home-assistant-nzb-clients/archive/616cad59154092599278661af17e2a9f2cf5e2a9.zip#python-sabnzbd==0.1
|
||||
|
||||
# homeassistant.components.qwikswitch
|
||||
https://github.com/kellerza/pyqwikswitch/archive/v0.1.zip#pyqwikswitch==0.1
|
||||
|
||||
# homeassistant.components.ecobee
|
||||
https://github.com/nkgilley/python-ecobee-api/archive/4a884bc146a93991b4210f868f3d6aecf0a181e6.zip#python-ecobee==0.0.5
|
||||
|
||||
|
|
Loading…
Reference in New Issue