Add async_update_data to emoncms coordinator (#122416)

* Add async_update_data to coordinator

* Add const module
This commit is contained in:
Alexandre CUER 2024-07-22 21:47:01 +02:00 committed by GitHub
parent a1cdd91d23
commit 489457c47b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 12 deletions

View File

@ -0,0 +1,11 @@
"""Constants for the emoncms integration."""
import logging
CONF_EXCLUDE_FEEDID = "exclude_feed_id"
CONF_ONLY_INCLUDE_FEEDID = "include_only_feed_id"
CONF_MESSAGE = "message"
CONF_SUCCESS = "success"
LOGGER = logging.getLogger(__package__)

View File

@ -1,15 +1,14 @@
"""DataUpdateCoordinator for the emoncms integration."""
from datetime import timedelta
import logging
from typing import Any
from pyemoncms import EmoncmsClient
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
_LOGGER = logging.getLogger(__name__)
from .const import CONF_MESSAGE, CONF_SUCCESS, LOGGER
class EmoncmsCoordinator(DataUpdateCoordinator[list[dict[str, Any]] | None]):
@ -24,8 +23,15 @@ class EmoncmsCoordinator(DataUpdateCoordinator[list[dict[str, Any]] | None]):
"""Initialize the emoncms data coordinator."""
super().__init__(
hass,
_LOGGER,
LOGGER,
name="emoncms_coordinator",
update_method=emoncms_client.async_list_feeds,
update_interval=scan_interval,
)
self.emoncms_client = emoncms_client
async def _async_update_data(self) -> list[dict[str, Any]]:
"""Fetch data from API endpoint."""
data = await self.emoncms_client.async_request("/feed/list.json")
if not data[CONF_SUCCESS]:
raise UpdateFailed
return data[CONF_MESSAGE]

View File

@ -3,7 +3,6 @@
from __future__ import annotations
from datetime import timedelta
import logging
from typing import Any
from pyemoncms import EmoncmsClient
@ -33,10 +32,9 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import CONF_EXCLUDE_FEEDID, CONF_ONLY_INCLUDE_FEEDID
from .coordinator import EmoncmsCoordinator
_LOGGER = logging.getLogger(__name__)
ATTR_FEEDID = "FeedId"
ATTR_FEEDNAME = "FeedName"
ATTR_LASTUPDATETIME = "LastUpdated"
@ -45,8 +43,6 @@ ATTR_SIZE = "Size"
ATTR_TAG = "Tag"
ATTR_USERID = "UserId"
CONF_EXCLUDE_FEEDID = "exclude_feed_id"
CONF_ONLY_INCLUDE_FEEDID = "include_only_feed_id"
CONF_SENSOR_NAMES = "sensor_names"
DECIMALS = 2
@ -98,7 +94,7 @@ async def async_setup_platform(
coordinator = EmoncmsCoordinator(hass, emoncms_client, scan_interval)
await coordinator.async_refresh()
elems = coordinator.data
if elems is None:
if not elems:
return
sensors: list[EmonCmsSensor] = []
@ -208,7 +204,7 @@ class EmonCmsSensor(CoordinatorEntity[EmoncmsCoordinator], SensorEntity):
self._attr_native_value = None
if self._value_template is not None:
self._attr_native_value = (
self._value_template.render_with_possible_json_value(
self._value_template.async_render_with_possible_json_value(
elem["value"], STATE_UNKNOWN
)
)