mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Update nut to use CoordinatorEntity (#39394)
This commit is contained in:
parent
4e876cb473
commit
2525d319a3
@ -17,7 +17,7 @@ from homeassistant.const import (
|
|||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
COORDINATOR,
|
COORDINATOR,
|
||||||
@ -117,7 +117,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
|
||||||
class NUTSensor(Entity):
|
class NUTSensor(CoordinatorEntity):
|
||||||
"""Representation of a sensor entity for NUT status values."""
|
"""Representation of a sensor entity for NUT status values."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -132,7 +132,7 @@ class NUTSensor(Entity):
|
|||||||
firmware,
|
firmware,
|
||||||
):
|
):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self._coordinator = coordinator
|
super().__init__(coordinator)
|
||||||
self._type = sensor_type
|
self._type = sensor_type
|
||||||
self._manufacturer = manufacturer
|
self._manufacturer = manufacturer
|
||||||
self._firmware = firmware
|
self._firmware = firmware
|
||||||
@ -200,34 +200,11 @@ class NUTSensor(Entity):
|
|||||||
"""Return the unit of measurement of this entity, if any."""
|
"""Return the unit of measurement of this entity, if any."""
|
||||||
return self._unit
|
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
|
@property
|
||||||
def device_state_attributes(self):
|
def device_state_attributes(self):
|
||||||
"""Return the sensor attributes."""
|
"""Return the sensor attributes."""
|
||||||
return {ATTR_STATE: _format_display_state(self._data.status)}
|
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):
|
def _format_display_state(status):
|
||||||
"""Return UPS display state."""
|
"""Return UPS display state."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user