2019-02-13 20:21:14 +00:00
|
|
|
"""Support for tracking the online status of a UPS."""
|
2016-08-30 19:34:33 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice, PLATFORM_SCHEMA
|
2016-08-30 19:34:33 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-10 16:32:18 +00:00
|
|
|
from homeassistant.components import apcupsd
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "UPS Online Status"
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
|
|
|
|
)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-08-08 18:21:33 +00:00
|
|
|
"""Set up an APCUPSd Online Status binary sensor."""
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities([OnlineStatus(config, apcupsd.DATA)], True)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OnlineStatus(BinarySensorDevice):
|
2016-08-30 19:34:33 +00:00
|
|
|
"""Representation of an UPS online status."""
|
2016-03-07 19:21:08 +00:00
|
|
|
|
2016-02-11 07:33:53 +00:00
|
|
|
def __init__(self, config, data):
|
2016-08-30 19:34:33 +00:00
|
|
|
"""Initialize the APCUPSd binary device."""
|
2016-02-10 16:32:18 +00:00
|
|
|
self._config = config
|
2016-02-11 07:33:53 +00:00
|
|
|
self._data = data
|
2016-02-10 16:32:18 +00:00
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Return the name of the UPS online status sensor."""
|
2016-08-30 19:34:33 +00:00
|
|
|
return self._config.get(CONF_NAME)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Return true if the UPS is online, else false."""
|
2016-02-10 16:32:18 +00:00
|
|
|
return self._state == apcupsd.VALUE_ONLINE
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Get the status report from APCUPSd and set this entity's state."""
|
2016-02-11 07:33:53 +00:00
|
|
|
self._state = self._data.status[apcupsd.KEY_STATUS]
|