mirror of
https://github.com/home-assistant/core.git
synced 2025-10-25 11:39:27 +00:00
* 2nd attempt to add energysensors to Adax component * Ruff format changes * I did not reuse the first call for information.. Now i do.. * Fixed some tests after the last change * Remove extra attributes * Dont use info logger * aggregate if not rooms * Raise error if no rooms are discovered * Move code out of try catch * Catch more specific errors * removed platforms from manifest.json * remove attribute translation key * Getting rid of the summation of energy used.. * Fixed errorness in test * set roomproperty in Init * concatenated the two functions * use raw Wh values and suggest a konversion for HomeAssistant * Use snapshot testing * Update homeassistant/components/adax/coordinator.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/adax/strings.json Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/adax/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/adax/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/adax/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/adax/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Removing un needed logg * Removing initial value * Changing tests to snapshot_platform * Removing available property from sensor.py and doing a ruff formating.. * Fix a broken indent * Add fix for coordinator updates in Adax energisensor and namesetting * Update homeassistant/components/adax/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/adax/coordinator.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/adax/coordinator.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Update homeassistant/components/adax/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * generated snapshots * Ruff changes * Even more ruff changes, that did not appear on ruff command locally * Trying to fix CI updates * Update homeassistant/components/adax/sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> * Improve AdaxEnergySensor by simplifying code and ensuring correct handling of energy values. Adjust how room and device information is retrieved to avoid duplication and improve readability. * Removed a test för device_id as per request.. * Make supersure that value is int and not "Any" * removing executable status * Update tests/components/adax/test_sensor.py Co-authored-by: Josef Zweck <josef@zweck.dev> --------- Co-authored-by: Josef Zweck <josef@zweck.dev>
95 lines
3.3 KiB
Python
95 lines
3.3 KiB
Python
"""DataUpdateCoordinator for the Adax component."""
|
|
|
|
import logging
|
|
from typing import Any, cast
|
|
|
|
from adax import Adax
|
|
from adax_local import Adax as AdaxLocal
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_IP_ADDRESS, CONF_PASSWORD, CONF_TOKEN
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
|
|
|
from .const import ACCOUNT_ID, SCAN_INTERVAL
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
type AdaxConfigEntry = ConfigEntry[AdaxCloudCoordinator | AdaxLocalCoordinator]
|
|
|
|
|
|
class AdaxCloudCoordinator(DataUpdateCoordinator[dict[str, dict[str, Any]]]):
|
|
"""Coordinator for updating data to and from Adax (cloud)."""
|
|
|
|
def __init__(self, hass: HomeAssistant, entry: AdaxConfigEntry) -> None:
|
|
"""Initialize the Adax coordinator used for Cloud mode."""
|
|
super().__init__(
|
|
hass,
|
|
config_entry=entry,
|
|
logger=_LOGGER,
|
|
name="AdaxCloud",
|
|
update_interval=SCAN_INTERVAL,
|
|
)
|
|
|
|
self.adax_data_handler = Adax(
|
|
entry.data[ACCOUNT_ID],
|
|
entry.data[CONF_PASSWORD],
|
|
websession=async_get_clientsession(hass),
|
|
)
|
|
|
|
async def _async_update_data(self) -> dict[str, dict[str, Any]]:
|
|
"""Fetch data from the Adax."""
|
|
try:
|
|
if hasattr(self.adax_data_handler, "fetch_rooms_info"):
|
|
rooms = await self.adax_data_handler.fetch_rooms_info() or []
|
|
_LOGGER.debug("fetch_rooms_info returned: %s", rooms)
|
|
else:
|
|
_LOGGER.debug("fetch_rooms_info method not available, using get_rooms")
|
|
rooms = []
|
|
|
|
if not rooms:
|
|
_LOGGER.debug(
|
|
"No rooms from fetch_rooms_info, trying get_rooms as fallback"
|
|
)
|
|
rooms = await self.adax_data_handler.get_rooms() or []
|
|
_LOGGER.debug("get_rooms fallback returned: %s", rooms)
|
|
|
|
if not rooms:
|
|
raise UpdateFailed("No rooms available from Adax API")
|
|
|
|
except OSError as e:
|
|
raise UpdateFailed(f"Error communicating with API: {e}") from e
|
|
|
|
for room in rooms:
|
|
room["energyWh"] = int(room.get("energyWh", 0))
|
|
|
|
return {r["id"]: r for r in rooms}
|
|
|
|
|
|
class AdaxLocalCoordinator(DataUpdateCoordinator[dict[str, Any] | None]):
|
|
"""Coordinator for updating data to and from Adax (local)."""
|
|
|
|
def __init__(self, hass: HomeAssistant, entry: AdaxConfigEntry) -> None:
|
|
"""Initialize the Adax coordinator used for Local mode."""
|
|
super().__init__(
|
|
hass,
|
|
config_entry=entry,
|
|
logger=_LOGGER,
|
|
name="AdaxLocal",
|
|
update_interval=SCAN_INTERVAL,
|
|
)
|
|
|
|
self.adax_data_handler = AdaxLocal(
|
|
entry.data[CONF_IP_ADDRESS],
|
|
entry.data[CONF_TOKEN],
|
|
websession=async_get_clientsession(hass, verify_ssl=False),
|
|
)
|
|
|
|
async def _async_update_data(self) -> dict[str, Any]:
|
|
"""Fetch data from the Adax."""
|
|
if result := await self.adax_data_handler.get_status():
|
|
return cast(dict[str, Any], result)
|
|
raise UpdateFailed("Got invalid status from device")
|