Remove global variable from apcupsd (#33717)

* Remove global variable from apcupsd

* Run isort

* Address review comments
This commit is contained in:
springstan 2020-04-05 23:54:37 +02:00 committed by GitHub
parent 00e67fb2c7
commit 60dd2213cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 13 deletions

View File

@ -11,9 +11,6 @@ from homeassistant.util import Throttle
_LOGGER = logging.getLogger(__name__)
CONF_TYPE = "type"
DATA = None
DEFAULT_HOST = "localhost"
DEFAULT_PORT = 3551
DOMAIN = "apcupsd"
@ -39,17 +36,17 @@ CONFIG_SCHEMA = vol.Schema(
def setup(hass, config):
"""Use config values to set up a function enabling status retrieval."""
global DATA # pylint: disable=global-statement
conf = config[DOMAIN]
host = conf.get(CONF_HOST)
port = conf.get(CONF_PORT)
DATA = APCUPSdData(host, port)
apcups_data = APCUPSdData(host, port)
hass.data[DOMAIN] = apcups_data
# It doesn't really matter why we're not able to get the status, just that
# we can't.
try:
DATA.update(no_throttle=True)
apcups_data.update(no_throttle=True)
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Failure while testing APCUPSd status retrieval.")
return False

View File

@ -1,11 +1,12 @@
"""Support for tracking the online status of a UPS."""
import voluptuous as vol
from homeassistant.components import apcupsd
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorDevice
from homeassistant.const import CONF_NAME
import homeassistant.helpers.config_validation as cv
from . import DOMAIN, KEY_STATUS, VALUE_ONLINE
DEFAULT_NAME = "UPS Online Status"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string}
@ -14,7 +15,9 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up an APCUPSd Online Status binary sensor."""
add_entities([OnlineStatus(config, apcupsd.DATA)], True)
apcups_data = hass.data[DOMAIN]
add_entities([OnlineStatus(config, apcups_data)], True)
class OnlineStatus(BinarySensorDevice):
@ -34,8 +37,8 @@ class OnlineStatus(BinarySensorDevice):
@property
def is_on(self):
"""Return true if the UPS is online, else false."""
return self._state & apcupsd.VALUE_ONLINE > 0
return self._state & VALUE_ONLINE > 0
def update(self):
"""Get the status report from APCUPSd and set this entity's state."""
self._state = int(self._data.status[apcupsd.KEY_STATUS], 16)
self._state = int(self._data.status[KEY_STATUS], 16)

View File

@ -4,7 +4,6 @@ import logging
from apcaccess.status import ALL_UNITS
import voluptuous as vol
from homeassistant.components import apcupsd
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_RESOURCES,
@ -17,6 +16,8 @@ from homeassistant.const import (
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from . import DOMAIN
_LOGGER = logging.getLogger(__name__)
SENSOR_PREFIX = "UPS "
@ -114,6 +115,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the APCUPSd sensors."""
apcups_data = hass.data[DOMAIN]
entities = []
for resource in config[CONF_RESOURCES]:
@ -126,13 +128,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"mdi:information-outline",
]
if sensor_type.upper() not in apcupsd.DATA.status:
if sensor_type.upper() not in apcups_data.status:
_LOGGER.warning(
"Sensor type: %s does not appear in the APCUPSd status output",
sensor_type,
)
entities.append(APCUPSdSensor(apcupsd.DATA, sensor_type))
entities.append(APCUPSdSensor(apcups_data, sensor_type))
add_entities(entities, True)