2019-02-13 20:21:14 +00:00
|
|
|
"""Support for APCUPSd via its Network Information Server (NIS)."""
|
2016-02-11 07:33:53 +00:00
|
|
|
from datetime import timedelta
|
2019-10-18 00:10:28 +00:00
|
|
|
import logging
|
2016-02-11 07:33:53 +00:00
|
|
|
|
2019-10-18 00:10:28 +00:00
|
|
|
from apcaccess import status
|
2016-08-30 19:34:33 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_PORT
|
2016-08-30 19:34:33 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-02-11 07:33:53 +00:00
|
|
|
from homeassistant.util import Throttle
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2016-08-30 19:34:33 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_HOST = "localhost"
|
2016-08-30 19:34:33 +00:00
|
|
|
DEFAULT_PORT = 3551
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "apcupsd"
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2020-01-03 16:40:36 +00:00
|
|
|
KEY_STATUS = "STATFLAG"
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2016-02-11 07:33:53 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
|
|
|
|
2020-01-03 16:40:36 +00:00
|
|
|
VALUE_ONLINE = 8
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string,
|
|
|
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Use config values to set up a function enabling status retrieval."""
|
2016-08-30 19:34:33 +00:00
|
|
|
conf = config[DOMAIN]
|
2020-04-07 20:45:56 +00:00
|
|
|
host = conf[CONF_HOST]
|
|
|
|
port = conf[CONF_PORT]
|
2016-02-10 16:32:18 +00:00
|
|
|
|
2020-04-05 21:54:37 +00:00
|
|
|
apcups_data = APCUPSdData(host, port)
|
|
|
|
hass.data[DOMAIN] = apcups_data
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
# It doesn't really matter why we're not able to get the status, just that
|
|
|
|
# we can't.
|
|
|
|
try:
|
2020-04-05 21:54:37 +00:00
|
|
|
apcups_data.update(no_throttle=True)
|
2018-10-10 10:17:11 +00:00
|
|
|
except Exception: # pylint: disable=broad-except
|
2020-07-05 21:04:19 +00:00
|
|
|
_LOGGER.exception("Failure while testing APCUPSd status retrieval")
|
2016-02-10 16:32:18 +00:00
|
|
|
return False
|
|
|
|
return True
|
2016-02-11 07:33:53 +00:00
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class APCUPSdData:
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Stores the data retrieved from APCUPSd.
|
|
|
|
|
|
|
|
For each entity to use, acts as the single point responsible for fetching
|
|
|
|
updates from the server.
|
2016-02-11 07:33:53 +00:00
|
|
|
"""
|
2016-03-08 16:55:57 +00:00
|
|
|
|
2016-02-11 07:33:53 +00:00
|
|
|
def __init__(self, host, port):
|
2018-01-29 22:37:19 +00:00
|
|
|
"""Initialize the data object."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2016-02-11 07:33:53 +00:00
|
|
|
self._host = host
|
|
|
|
self._port = port
|
|
|
|
self._status = None
|
|
|
|
self._get = status.get
|
|
|
|
self._parse = status.parse
|
|
|
|
|
|
|
|
@property
|
|
|
|
def status(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Get latest update if throttle allows. Return status."""
|
2016-02-11 07:33:53 +00:00
|
|
|
self.update()
|
|
|
|
return self._status
|
|
|
|
|
|
|
|
def _get_status(self):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Get the status from APCUPSd and parse it into a dict."""
|
2016-02-11 07:33:53 +00:00
|
|
|
return self._parse(self._get(host=self._host, port=self._port))
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def update(self, **kwargs):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Fetch the latest status from APCUPSd."""
|
2016-02-11 07:33:53 +00:00
|
|
|
self._status = self._get_status()
|