mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Update eafm to use CoordinatorEntity (#39420)
This commit is contained in:
parent
16a0bd7ff3
commit
186c13f1ab
@ -7,8 +7,10 @@ import async_timeout
|
|||||||
|
|
||||||
from homeassistant.const import ATTR_ATTRIBUTION, LENGTH_METERS
|
from homeassistant.const import ATTR_ATTRIBUTION, LENGTH_METERS
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.update_coordinator import (
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
)
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
|
|
||||||
@ -75,14 +77,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
await coordinator.async_refresh()
|
await coordinator.async_refresh()
|
||||||
|
|
||||||
|
|
||||||
class Measurement(Entity):
|
class Measurement(CoordinatorEntity):
|
||||||
"""A gauge at a flood monitoring station."""
|
"""A gauge at a flood monitoring station."""
|
||||||
|
|
||||||
attribution = "This uses Environment Agency flood and river level data from the real-time data API"
|
attribution = "This uses Environment Agency flood and river level data from the real-time data API"
|
||||||
|
|
||||||
def __init__(self, coordinator, key):
|
def __init__(self, coordinator, key):
|
||||||
"""Initialise the gauge with a data instance and station."""
|
"""Initialise the gauge with a data instance and station."""
|
||||||
self.coordinator = coordinator
|
super().__init__(coordinator)
|
||||||
self.key = key
|
self.key = key
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -110,11 +112,6 @@ class Measurement(Entity):
|
|||||||
"""Return the name of the gauge."""
|
"""Return the name of the gauge."""
|
||||||
return f"{self.station_name} {self.parameter_name} {self.qualifier}"
|
return f"{self.station_name} {self.parameter_name} {self.qualifier}"
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self) -> bool:
|
|
||||||
"""Stations are polled as a group - the entity shouldn't poll by itself."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self):
|
||||||
"""Return the unique id of the gauge."""
|
"""Return the unique id of the gauge."""
|
||||||
@ -150,12 +147,6 @@ class Measurement(Entity):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return units for the sensor."""
|
"""Return units for the sensor."""
|
||||||
@ -173,11 +164,3 @@ class Measurement(Entity):
|
|||||||
def state(self):
|
def state(self):
|
||||||
"""Return the current sensor value."""
|
"""Return the current sensor value."""
|
||||||
return self.coordinator.data["measures"][self.key]["latestReading"]["value"]
|
return self.coordinator.data["measures"][self.key]["latestReading"]["value"]
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""
|
|
||||||
Update the entity.
|
|
||||||
|
|
||||||
Only used by the generic entity update service.
|
|
||||||
"""
|
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user