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

View File

@ -1,11 +1,12 @@
"""Support for tracking the online status of a UPS.""" """Support for tracking the online status of a UPS."""
import voluptuous as vol import voluptuous as vol
from homeassistant.components import apcupsd
from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorDevice from homeassistant.components.binary_sensor import PLATFORM_SCHEMA, BinarySensorDevice
from homeassistant.const import CONF_NAME from homeassistant.const import CONF_NAME
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from . import DOMAIN, KEY_STATUS, VALUE_ONLINE
DEFAULT_NAME = "UPS Online Status" DEFAULT_NAME = "UPS Online Status"
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend( PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string} {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): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up an APCUPSd Online Status binary sensor.""" """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): class OnlineStatus(BinarySensorDevice):
@ -34,8 +37,8 @@ class OnlineStatus(BinarySensorDevice):
@property @property
def is_on(self): def is_on(self):
"""Return true if the UPS is online, else false.""" """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): def update(self):
"""Get the status report from APCUPSd and set this entity's state.""" """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 from apcaccess.status import ALL_UNITS
import voluptuous as vol import voluptuous as vol
from homeassistant.components import apcupsd
from homeassistant.components.sensor import PLATFORM_SCHEMA from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ( from homeassistant.const import (
CONF_RESOURCES, CONF_RESOURCES,
@ -17,6 +16,8 @@ from homeassistant.const import (
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from . import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SENSOR_PREFIX = "UPS " SENSOR_PREFIX = "UPS "
@ -114,6 +115,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the APCUPSd sensors.""" """Set up the APCUPSd sensors."""
apcups_data = hass.data[DOMAIN]
entities = [] entities = []
for resource in config[CONF_RESOURCES]: for resource in config[CONF_RESOURCES]:
@ -126,13 +128,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"mdi:information-outline", "mdi:information-outline",
] ]
if sensor_type.upper() not in apcupsd.DATA.status: if sensor_type.upper() not in apcups_data.status:
_LOGGER.warning( _LOGGER.warning(
"Sensor type: %s does not appear in the APCUPSd status output", "Sensor type: %s does not appear in the APCUPSd status output",
sensor_type, sensor_type,
) )
entities.append(APCUPSdSensor(apcupsd.DATA, sensor_type)) entities.append(APCUPSdSensor(apcups_data, sensor_type))
add_entities(entities, True) add_entities(entities, True)