core/homeassistant/components/qwikswitch/sensor.py

70 lines
2.0 KiB
Python
Raw Normal View History

"""
Support for Qwikswitch Sensors.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.qwikswitch/
"""
import logging
from homeassistant.core import callback
from . import DOMAIN as QWIKSWITCH, QSEntity
DEPENDENCIES = [QWIKSWITCH]
_LOGGER = logging.getLogger(__name__)
async def async_setup_platform(hass, _, add_entities, discovery_info=None):
2018-04-08 19:59:19 +00:00
"""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)
2018-04-08 19:59:19 +00:00
devs = [QSSensor(sensor) for sensor in discovery_info[QWIKSWITCH]]
add_entities(devs)
2018-04-08 19:59:19 +00:00
class QSSensor(QSEntity):
"""Sensor based on a Qwikswitch relay/dimmer module."""
2018-04-08 19:59:19 +00:00
_val = None
2018-04-08 19:59:19 +00:00
def __init__(self, sensor):
"""Initialize the sensor."""
2018-04-08 19:59:19 +00:00
from pyqwikswitch import SENSORS
super().__init__(sensor['id'], sensor['name'])
self.channel = sensor['channel']
2018-04-21 06:34:42 +00:00
sensor_type = sensor['type']
2018-04-08 19:59:19 +00:00
2018-04-21 06:34:42 +00:00
self._decode, self.unit = SENSORS[sensor_type]
2018-04-08 19:59:19 +00:00
if isinstance(self.unit, type):
2018-04-21 06:34:42 +00:00
self.unit = "{}:{}".format(sensor_type, self.channel)
@callback
def update_packet(self, packet):
"""Receive update packet from QSUSB."""
2018-04-21 06:34:42 +00:00
val = self._decode(packet, channel=self.channel)
_LOGGER.debug("Update %s (%s:%s) decoded as %s: %s",
self.entity_id, self.qsid, self.channel, val, packet)
2018-04-08 19:59:19 +00:00
if val is not None:
self._val = val
self.async_schedule_update_ha_state()
@property
def state(self):
"""Return the value of the sensor."""
2018-04-08 19:59:19 +00:00
return str(self._val)
@property
def unique_id(self):
"""Return a unique identifier for this sensor."""
return "qs{}:{}".format(self.qsid, self.channel)
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
2018-04-08 19:59:19 +00:00
return self.unit