Qwikswitch sensors (#13622)

This commit is contained in:
Johann Kellerman 2018-04-08 21:59:19 +02:00 committed by GitHub
parent ef16c53e46
commit b01dceaff2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 182 additions and 92 deletions

View file

@ -6,8 +6,7 @@ https://home-assistant.io/components/sensor.qwikswitch/
"""
import logging
from homeassistant.components.qwikswitch import DOMAIN as QWIKSWITCH
from homeassistant.helpers.entity import Entity
from homeassistant.components.qwikswitch import DOMAIN as QWIKSWITCH, QSEntity
DEPENDENCIES = [QWIKSWITCH]
@ -15,55 +14,48 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_platform(hass, _, add_devices, discovery_info=None):
"""Add lights from the main Qwikswitch component."""
"""Add sensor from the main Qwikswitch component."""
if discovery_info is None:
return
qsusb = hass.data[QWIKSWITCH]
_LOGGER.debug("Setup qwikswitch.sensor %s, %s", qsusb, discovery_info)
devs = [QSSensor(name, qsid)
for name, qsid in discovery_info[QWIKSWITCH].items()]
devs = [QSSensor(sensor) for sensor in discovery_info[QWIKSWITCH]]
add_devices(devs)
class QSSensor(Entity):
class QSSensor(QSEntity):
"""Sensor based on a Qwikswitch relay/dimmer module."""
_val = {}
_val = None
def __init__(self, sensor_name, sensor_id):
def __init__(self, sensor):
"""Initialize the sensor."""
self._name = sensor_name
self.qsid = sensor_id
from pyqwikswitch import SENSORS
super().__init__(sensor['id'], sensor['name'])
self.channel = sensor['channel']
self.sensor_type = sensor['type']
self._decode, self.unit = SENSORS[self.sensor_type]
if isinstance(self.unit, type):
self.unit = "{}:{}".format(self.sensor_type, self.channel)
def update_packet(self, packet):
"""Receive update packet from QSUSB."""
_LOGGER.debug("Update %s (%s): %s", self.entity_id, self.qsid, packet)
self._val = packet
self.async_schedule_update_ha_state()
val = self._decode(packet.get('data'), channel=self.channel)
_LOGGER.debug("Update %s (%s) decoded as %s: %s: %s",
self.entity_id, self.qsid, val, self.channel, packet)
if val is not None:
self._val = val
self.async_schedule_update_ha_state()
@property
def state(self):
"""Return the value of the sensor."""
return self._val.get('data', 0)
@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
return self._val
return str(self._val)
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return None
@property
def poll(self):
"""QS sensors gets packets in update_packet."""
return False
async def async_added_to_hass(self):
"""Listen for updates from QSUSb via dispatcher."""
# Part of Entity/ToggleEntity
self.hass.helpers.dispatcher.async_dispatcher_connect(
self.qsid, self.update_packet)
return self.unit