Update nut to use CoordinatorEntity (#39394)

This commit is contained in:
J. Nick Koston 2020-08-30 07:40:26 -05:00 committed by GitHub
parent 4e876cb473
commit 2525d319a3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -17,7 +17,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
COORDINATOR,
@ -117,7 +117,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
async_add_entities(entities, True)
class NUTSensor(Entity):
class NUTSensor(CoordinatorEntity):
"""Representation of a sensor entity for NUT status values."""
def __init__(
@ -132,7 +132,7 @@ class NUTSensor(Entity):
firmware,
):
"""Initialize the sensor."""
self._coordinator = coordinator
super().__init__(coordinator)
self._type = sensor_type
self._manufacturer = manufacturer
self._firmware = firmware
@ -200,34 +200,11 @@ class NUTSensor(Entity):
"""Return the unit of measurement of this entity, if any."""
return self._unit
@property
def should_poll(self):
"""No need to poll. Coordinator notifies entity of updates."""
return False
@property
def available(self):
"""Return if entity is available."""
return self._coordinator.last_update_success
@property
def device_state_attributes(self):
"""Return the sensor attributes."""
return {ATTR_STATE: _format_display_state(self._data.status)}
async def async_update(self):
"""Update the entity.
Only used by the generic entity update service.
"""
await self._coordinator.async_request_refresh()
async def async_added_to_hass(self):
"""When entity is added to hass."""
self.async_on_remove(
self._coordinator.async_add_listener(self.async_write_ha_state)
)
def _format_display_state(status):
"""Return UPS display state."""