core/homeassistant/components/sensor/apcupsd.py

88 lines
2.5 KiB
Python
Raw Normal View History

2016-02-10 16:32:18 +00:00
"""
Provides a sensor to track various status aspects of a UPS.
2016-02-12 06:54:25 +00:00
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.apcupsd/
2016-02-10 16:32:18 +00:00
"""
import logging
2016-02-19 05:27:50 +00:00
from homeassistant.components import apcupsd
2016-04-20 03:30:44 +00:00
from homeassistant.const import TEMP_CELSIUS
2016-02-10 16:32:18 +00:00
from homeassistant.helpers.entity import Entity
DEPENDENCIES = [apcupsd.DOMAIN]
DEFAULT_NAME = "UPS Status"
SPECIFIC_UNITS = {
2016-04-20 03:30:44 +00:00
"ITEMP": TEMP_CELSIUS
2016-02-10 16:32:18 +00:00
}
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
2016-03-08 15:46:34 +00:00
"""Setup the APCUPSd sensor."""
2016-02-10 16:32:18 +00:00
typ = config.get(apcupsd.CONF_TYPE)
if typ is None:
_LOGGER.error(
"You must include a '%s' when configuring an APCUPSd sensor.",
apcupsd.CONF_TYPE)
return False
2016-02-10 16:32:18 +00:00
typ = typ.upper()
if typ not in apcupsd.DATA.status:
_LOGGER.error(
"Specified '%s' of '%s' does not appear in the APCUPSd status "
"output.", apcupsd.CONF_TYPE, typ)
return False
2016-02-10 16:32:18 +00:00
add_entities((
Sensor(config, apcupsd.DATA, unit=SPECIFIC_UNITS.get(typ)),
2016-02-10 16:32:18 +00:00
))
def infer_unit(value):
2016-03-08 15:46:34 +00:00
"""If the value ends with any of the units from ALL_UNITS.
Split the unit off the end of the value and return the value, unit tuple
pair. Else return the original value and None as the unit.
2016-02-10 16:32:18 +00:00
"""
from apcaccess.status import ALL_UNITS
for unit in ALL_UNITS:
if value.endswith(unit):
return value[:-len(unit)], unit
return value, None
class Sensor(Entity):
2016-03-08 15:46:34 +00:00
"""Representation of a sensor entity for APCUPSd status values."""
def __init__(self, config, data, unit=None):
2016-03-08 15:46:34 +00:00
"""Initialize the sensor."""
2016-02-10 16:32:18 +00:00
self._config = config
self._unit = unit
self._data = data
2016-02-10 16:32:18 +00:00
self._inferred_unit = None
self.update()
2016-02-10 16:32:18 +00:00
@property
def name(self):
2016-03-08 15:46:34 +00:00
"""Return the name of the UPS sensor."""
2016-02-10 16:32:18 +00:00
return self._config.get("name", DEFAULT_NAME)
@property
def state(self):
2016-03-08 15:46:34 +00:00
"""Return true if the UPS is online, else False."""
2016-02-10 16:32:18 +00:00
return self._state
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement of this entity, if any."""
2016-02-10 16:32:18 +00:00
if self._unit is None:
return self._inferred_unit
return self._unit
def update(self):
2016-02-23 05:21:49 +00:00
"""Get the latest status and use it to update our sensor state."""
2016-02-10 16:32:18 +00:00
key = self._config[apcupsd.CONF_TYPE].upper()
self._state, self._inferred_unit = infer_unit(self._data.status[key])