core/homeassistant/components/zigbee/sensor.py

88 lines
2.4 KiB
Python
Raw Normal View History

"""Support for Zigbee sensors."""
from binascii import hexlify
import logging
2016-01-24 08:02:14 +00:00
2016-09-07 01:28:55 +00:00
import voluptuous as vol
2016-02-19 05:27:50 +00:00
from homeassistant.components import zigbee
2016-04-20 03:30:44 +00:00
from homeassistant.const import TEMP_CELSIUS
2016-01-24 08:02:14 +00:00
from homeassistant.helpers.entity import Entity
from . import PLATFORM_SCHEMA
2016-01-24 08:02:14 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
CONF_TYPE = "type"
CONF_MAX_VOLTS = "max_volts"
2016-09-07 01:28:55 +00:00
DEFAULT_VOLTS = 1.2
2019-07-31 19:25:30 +00:00
TYPES = ["analog", "temperature"]
2016-09-07 01:28:55 +00:00
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_TYPE): vol.In(TYPES),
vol.Optional(CONF_MAX_VOLTS, default=DEFAULT_VOLTS): vol.Coerce(float),
}
)
2016-01-24 08:02:14 +00:00
2016-09-07 01:28:55 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the ZigBee platform.
2016-03-08 15:46:34 +00:00
2016-01-24 08:02:14 +00:00
Uses the 'type' config value to work out which type of ZigBee sensor we're
dealing with and instantiates the relevant classes to handle it.
"""
2016-09-07 01:28:55 +00:00
typ = config.get(CONF_TYPE)
2016-01-24 08:02:14 +00:00
try:
sensor_class, config_class = TYPE_CLASSES[typ]
except KeyError:
_LOGGER.exception("Unknown ZigBee sensor type: %s", typ)
return
2016-09-07 01:28:55 +00:00
add_entities([sensor_class(hass, config_class(config))], True)
2016-01-24 08:02:14 +00:00
class ZigBeeTemperatureSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of XBee Pro temperature sensor."""
2016-01-24 08:02:14 +00:00
def __init__(self, hass, config):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2016-01-24 08:02:14 +00:00
self._config = config
self._temp = None
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the sensor."""
2016-01-24 08:02:14 +00:00
return self._config.name
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return the state of the sensor."""
2016-01-24 08:02:14 +00:00
return self._temp
@property
def unit_of_measurement(self):
"""Return the unit of measurement the value is expressed in."""
2016-04-20 03:30:44 +00:00
return TEMP_CELSIUS
2016-01-24 08:02:14 +00:00
def update(self):
2016-03-08 15:46:34 +00:00
"""Get the latest data."""
try:
self._temp = zigbee.DEVICE.get_temperature(self._config.address)
except zigbee.ZIGBEE_TX_FAILURE:
_LOGGER.warning(
"Transmission failure when attempting to get sample from "
2019-07-31 19:25:30 +00:00
"ZigBee device at address: %s",
hexlify(self._config.address),
)
except zigbee.ZIGBEE_EXCEPTION as exc:
2019-07-31 19:25:30 +00:00
_LOGGER.exception("Unable to get sample from ZigBee device: %s", exc)
2016-01-24 08:02:14 +00:00
# This must be below the classes to which it refers.
2016-01-24 08:02:14 +00:00
TYPE_CLASSES = {
"temperature": (ZigBeeTemperatureSensor, zigbee.ZigBeeConfig),
2019-07-31 19:25:30 +00:00
"analog": (zigbee.ZigBeeAnalogIn, zigbee.ZigBeeAnalogInConfig),
2016-01-24 08:02:14 +00:00
}