2016-02-10 16:32:18 +00:00
|
|
|
"""
|
2016-03-07 17:49:31 +00:00
|
|
|
Support for status output of APCUPSd via its Network Information Server (NIS).
|
2016-02-12 06:54:25 +00:00
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/apcupsd/
|
2016-02-10 16:32:18 +00:00
|
|
|
"""
|
|
|
|
import logging
|
2016-02-11 07:33:53 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
|
|
|
|
from homeassistant.util import Throttle
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
DOMAIN = "apcupsd"
|
|
|
|
REQUIREMENTS = ("apcaccess==0.0.4",)
|
|
|
|
|
|
|
|
CONF_HOST = "host"
|
|
|
|
CONF_PORT = "port"
|
|
|
|
CONF_TYPE = "type"
|
|
|
|
|
|
|
|
DEFAULT_HOST = "localhost"
|
|
|
|
DEFAULT_PORT = 3551
|
|
|
|
|
|
|
|
KEY_STATUS = "STATUS"
|
|
|
|
|
|
|
|
VALUE_ONLINE = "ONLINE"
|
|
|
|
|
2016-02-11 07:33:53 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
|
|
|
|
|
|
|
|
DATA = None
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Use config values to set up a function enabling status retrieval."""
|
2016-02-11 07:33:53 +00:00
|
|
|
global DATA
|
2016-02-10 16:32:18 +00:00
|
|
|
|
|
|
|
host = config[DOMAIN].get(CONF_HOST, DEFAULT_HOST)
|
|
|
|
port = config[DOMAIN].get(CONF_PORT, DEFAULT_PORT)
|
|
|
|
|
2016-02-11 07:33:53 +00:00
|
|
|
DATA = APCUPSdData(host, port)
|
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.
|
|
|
|
# pylint: disable=broad-except
|
|
|
|
try:
|
2016-02-11 07:33:53 +00:00
|
|
|
DATA.update(no_throttle=True)
|
2016-02-10 16:32:18 +00:00
|
|
|
except Exception:
|
|
|
|
_LOGGER.exception("Failure while testing APCUPSd status retrieval.")
|
|
|
|
return False
|
|
|
|
return True
|
2016-02-11 07:33:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class APCUPSdData(object):
|
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):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the data oject."""
|
2016-02-11 07:33:53 +00:00
|
|
|
from apcaccess import status
|
|
|
|
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()
|