Use entity class attributes for apcupsd (#52662)

This commit is contained in:
Robert Hillis 2021-07-08 05:55:26 -04:00 committed by GitHub
parent 7d0751df8a
commit 1c11b247e4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 42 deletions

View file

@ -25,20 +25,9 @@ class OnlineStatus(BinarySensorEntity):
def __init__(self, config, data):
"""Initialize the APCUPSd binary device."""
self._config = config
self._data = data
self._state = None
@property
def name(self):
"""Return the name of the UPS online status sensor."""
return self._config[CONF_NAME]
@property
def is_on(self):
"""Return true if the UPS is online, else false."""
return self._state & VALUE_ONLINE > 0
self._attr_name = config[CONF_NAME]
def update(self):
"""Get the status report from APCUPSd and set this entity's state."""
self._state = int(self._data.status[KEY_STATUS], 16)
self._attr_is_on = int(self._data.status[KEY_STATUS], 16) & VALUE_ONLINE > 0

View file

@ -162,39 +162,18 @@ class APCUPSdSensor(SensorEntity):
"""Initialize the sensor."""
self._data = data
self.type = sensor_type
self._name = SENSOR_PREFIX + SENSOR_TYPES[sensor_type][0]
self._unit = SENSOR_TYPES[sensor_type][1]
self._inferred_unit = None
self._state = None
@property
def name(self):
"""Return the name of the UPS sensor."""
return self._name
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return SENSOR_TYPES[self.type][2]
@property
def state(self):
"""Return true if the UPS is online, else False."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
if not self._unit:
return self._inferred_unit
return self._unit
self._attr_name = SENSOR_PREFIX + SENSOR_TYPES[sensor_type][0]
self._attr_icon = SENSOR_TYPES[self.type][2]
if SENSOR_TYPES[sensor_type][1]:
self._attr_unit_of_measurement = SENSOR_TYPES[sensor_type][1]
def update(self):
"""Get the latest status and use it to update our sensor state."""
if self.type.upper() not in self._data.status:
self._state = None
self._inferred_unit = None
self._attr_state = None
else:
self._state, self._inferred_unit = infer_unit(
self._attr_state, inferred_unit = infer_unit(
self._data.status[self.type.upper()]
)
if not self._attr_unit_of_measurement:
self._attr_unit_of_measurement = inferred_unit