Use a cache object to reduce the frequency of calls to APCUPSd
This commit is contained in:
parent
bb8981b611
commit
91fb2764cc
3 changed files with 59 additions and 51 deletions
|
@ -3,7 +3,6 @@ homeassistant.components.binary_sensor.apcupsd
|
|||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
Provides a binary sensor to track online status of a UPS.
|
||||
"""
|
||||
from homeassistant.core import JobPriority
|
||||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||
from homeassistant.components import apcupsd
|
||||
|
||||
|
@ -15,17 +14,16 @@ DEFAULT_NAME = "UPS Online Status"
|
|||
|
||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||
""" Instantiate an OnlineStatus binary sensor entity and add it to HA. """
|
||||
add_entities((OnlineStatus(hass, config),))
|
||||
add_entities((OnlineStatus(config, apcupsd.DATA),))
|
||||
|
||||
|
||||
class OnlineStatus(BinarySensorDevice):
|
||||
""" Binary sensor to represent UPS online status. """
|
||||
def __init__(self, hass, config):
|
||||
def __init__(self, config, data):
|
||||
self._config = config
|
||||
self._data = data
|
||||
self._state = None
|
||||
# Get initial state
|
||||
hass.pool.add_job(
|
||||
JobPriority.EVENT_STATE, (self.update_ha_state, True))
|
||||
self.update()
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
|
@ -39,8 +37,7 @@ class OnlineStatus(BinarySensorDevice):
|
|||
|
||||
def update(self):
|
||||
"""
|
||||
Get the latest status report from APCUPSd and establish whether the
|
||||
UPS is online.
|
||||
Get the status report from APCUPSd (or cache) and set this entity's
|
||||
state.
|
||||
"""
|
||||
status = apcupsd.GET_STATUS()
|
||||
self._state = status[apcupsd.KEY_STATUS]
|
||||
self._state = self._data.status[apcupsd.KEY_STATUS]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue