core/homeassistant/components/xbee/sensor.py

90 lines
2.5 KiB
Python
Raw Normal View History

2020-05-14 10:19:59 +00:00
"""Support for XBee 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
from xbee_helper.exceptions import ZigBeeException, ZigBeeTxFailure
2016-09-07 01:28:55 +00:00
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
2020-05-14 10:19:59 +00:00
from . import DOMAIN, PLATFORM_SCHEMA, XBeeAnalogIn, XBeeAnalogInConfig, XBeeConfig
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):
2020-05-14 10:19:59 +00:00
"""Set up the XBee Zigbee platform.
2016-03-08 15:46:34 +00:00
Uses the 'type' config value to work out which type of Zigbee sensor we're
2016-01-24 08:02:14 +00:00
dealing with and instantiates the relevant classes to handle it.
"""
zigbee_device = hass.data[DOMAIN]
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:
2020-05-14 10:19:59 +00:00
_LOGGER.exception("Unknown XBee Zigbee sensor type: %s", typ)
2016-01-24 08:02:14 +00:00
return
2016-09-07 01:28:55 +00:00
add_entities([sensor_class(config_class(config), zigbee_device)], True)
2016-01-24 08:02:14 +00:00
2020-05-14 10:19:59 +00:00
class XBeeTemperatureSensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of XBee Pro temperature sensor."""
def __init__(self, config, device):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2016-01-24 08:02:14 +00:00
self._config = config
self._device = device
2016-01-24 08:02:14 +00:00
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 = self._device.get_temperature(self._config.address)
except ZigBeeTxFailure:
_LOGGER.warning(
"Transmission failure when attempting to get sample from "
"Zigbee device at address: %s",
2019-07-31 19:25:30 +00:00
hexlify(self._config.address),
)
except ZigBeeException as exc:
_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 = {
2020-05-14 10:19:59 +00:00
"temperature": (XBeeTemperatureSensor, XBeeConfig),
"analog": (XBeeAnalogIn, XBeeAnalogInConfig),
2016-01-24 08:02:14 +00:00
}