mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Use CoordinatorEntity for supla (#39621)
This commit is contained in:
parent
a0663d84d2
commit
17b4c1a2ca
@ -11,8 +11,10 @@ from homeassistant.const import CONF_ACCESS_TOKEN
|
|||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.discovery import async_load_platform
|
from homeassistant.helpers.discovery import async_load_platform
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.update_coordinator import (
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
CoordinatorEntity,
|
||||||
|
DataUpdateCoordinator,
|
||||||
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -154,26 +156,21 @@ async def discover_devices(hass, hass_config):
|
|||||||
await async_load_platform(hass, component_name, DOMAIN, config, hass_config)
|
await async_load_platform(hass, component_name, DOMAIN, config, hass_config)
|
||||||
|
|
||||||
|
|
||||||
class SuplaChannel(Entity):
|
class SuplaChannel(CoordinatorEntity):
|
||||||
"""Base class of a Supla Channel (an equivalent of HA's Entity)."""
|
"""Base class of a Supla Channel (an equivalent of HA's Entity)."""
|
||||||
|
|
||||||
def __init__(self, config, server, coordinator):
|
def __init__(self, config, server, coordinator):
|
||||||
"""Init from config, hookup[ server and coordinator."""
|
"""Init from config, hookup[ server and coordinator."""
|
||||||
|
super().__init__(coordinator)
|
||||||
self.server_name = config["server_name"]
|
self.server_name = config["server_name"]
|
||||||
self.channel_id = config["channel_id"]
|
self.channel_id = config["channel_id"]
|
||||||
self.server = server
|
self.server = server
|
||||||
self.coordinator = coordinator
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def channel_data(self):
|
def channel_data(self):
|
||||||
"""Return channel data taken from coordinator."""
|
"""Return channel data taken from coordinator."""
|
||||||
return self.coordinator.data.get(self.channel_id)
|
return self.coordinator.data.get(self.channel_id)
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""Supla uses DataUpdateCoordinator, so no additional polling needed."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self) -> str:
|
def unique_id(self) -> str:
|
||||||
"""Return a unique ID."""
|
"""Return a unique ID."""
|
||||||
@ -197,12 +194,6 @@ class SuplaChannel(Entity):
|
|||||||
return False
|
return False
|
||||||
return state.get("connected")
|
return state.get("connected")
|
||||||
|
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_action(self, action, **add_pars):
|
async def async_action(self, action, **add_pars):
|
||||||
"""
|
"""
|
||||||
Run server action.
|
Run server action.
|
||||||
@ -220,10 +211,3 @@ class SuplaChannel(Entity):
|
|||||||
|
|
||||||
# Update state
|
# Update state
|
||||||
await self.coordinator.async_request_refresh()
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
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