mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Update pi_hole to use CoordinatorEntity (#39433)
This commit is contained in:
parent
7bed868d60
commit
42818c150f
@ -18,8 +18,11 @@ from homeassistant.core import callback
|
|||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
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, UpdateFailed
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
UpdateFailed,
|
||||||
|
)
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_LOCATION,
|
CONF_LOCATION,
|
||||||
@ -150,22 +153,16 @@ def _async_platforms(entry):
|
|||||||
return platforms
|
return platforms
|
||||||
|
|
||||||
|
|
||||||
class PiHoleEntity(Entity):
|
class PiHoleEntity(CoordinatorEntity):
|
||||||
"""Representation of a Pi-hole entity."""
|
"""Representation of a Pi-hole entity."""
|
||||||
|
|
||||||
def __init__(self, api, coordinator, name, server_unique_id):
|
def __init__(self, api, coordinator, name, server_unique_id):
|
||||||
"""Initialize a Pi-hole entity."""
|
"""Initialize a Pi-hole entity."""
|
||||||
|
super().__init__(coordinator)
|
||||||
self.api = api
|
self.api = api
|
||||||
self.coordinator = coordinator
|
|
||||||
self._name = name
|
self._name = name
|
||||||
self._server_unique_id = server_unique_id
|
self._server_unique_id = server_unique_id
|
||||||
|
|
||||||
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 icon(self):
|
def icon(self):
|
||||||
"""Icon to use in the frontend, if any."""
|
"""Icon to use in the frontend, if any."""
|
||||||
@ -179,17 +176,3 @@ class PiHoleEntity(Entity):
|
|||||||
"name": self._name,
|
"name": self._name,
|
||||||
"manufacturer": "Pi-hole",
|
"manufacturer": "Pi-hole",
|
||||||
}
|
}
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self):
|
|
||||||
"""Could the device be accessed during the last update call."""
|
|
||||||
return self.coordinator.last_update_success
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""No need to poll. Coordinator notifies entity of updates."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
async def async_update(self):
|
|
||||||
"""Get the latest data from the Pi-hole API."""
|
|
||||||
await self.coordinator.async_request_refresh()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user