From 60dd2213cf50bb3a58c6af8e43546f5730e77122 Mon Sep 17 00:00:00 2001 From: springstan <46536646+springstan@users.noreply.github.com> Date: Sun, 5 Apr 2020 23:54:37 +0200 Subject: [PATCH] Remove global variable from apcupsd (#33717) * Remove global variable from apcupsd * Run isort * Address review comments --- homeassistant/components/apcupsd/__init__.py | 9 +++------ homeassistant/components/apcupsd/binary_sensor.py | 11 +++++++---- homeassistant/components/apcupsd/sensor.py | 8 +++++--- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/apcupsd/__init__.py b/homeassistant/components/apcupsd/__init__.py index 1f99d5410ec..967b5855a51 100644 --- a/homeassistant/components/apcupsd/__init__.py +++ b/homeassistant/components/apcupsd/__init__.py @@ -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 diff --git a/homeassistant/components/apcupsd/binary_sensor.py b/homeassistant/components/apcupsd/binary_sensor.py index de4e1f17200..e07f97c928c 100644 --- a/homeassistant/components/apcupsd/binary_sensor.py +++ b/homeassistant/components/apcupsd/binary_sensor.py @@ -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) diff --git a/homeassistant/components/apcupsd/sensor.py b/homeassistant/components/apcupsd/sensor.py index e39696cc37a..85bbe5c950e 100644 --- a/homeassistant/components/apcupsd/sensor.py +++ b/homeassistant/components/apcupsd/sensor.py @@ -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)