From f120947299aaef08a5ee7490704eb8dee0192786 Mon Sep 17 00:00:00 2001 From: tr4nt0r Date: Sat, 27 Apr 2024 13:47:55 +0000 Subject: [PATCH] Revert "Move Data coordinator to separate module" This reverts commit f5c8c3c886a868b2ed50ad2098fe3cb1ccc01c62. --- homeassistant/components/habitica/__init__.py | 8 +-- .../components/habitica/coordinator.py | 67 ------------------- homeassistant/components/habitica/sensor.py | 66 +++++++++++++++++- 3 files changed, 67 insertions(+), 74 deletions(-) delete mode 100644 homeassistant/components/habitica/coordinator.py diff --git a/homeassistant/components/habitica/__init__.py b/homeassistant/components/habitica/__init__.py index 5df8727700e..ce79d3fa1ca 100644 --- a/homeassistant/components/habitica/__init__.py +++ b/homeassistant/components/habitica/__init__.py @@ -23,7 +23,6 @@ from .const import ( EVENT_API_CALL_SUCCESS, SERVICE_API_CALL, ) -from .coordinator import HabitipyData _LOGGER = logging.getLogger(__name__) @@ -108,8 +107,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: api = None for entry in entries: if entry.data[CONF_NAME] == name: - coordinator = hass.data[DOMAIN].get(entry.entry_id) - api = coordinator.api + api = hass.data[DOMAIN].get(entry.entry_id) break if api is None: _LOGGER.error("API_CALL: User '%s' not configured", name) @@ -128,6 +126,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: EVENT_API_CALL_SUCCESS, {ATTR_NAME: name, ATTR_PATH: path, ATTR_DATA: data} ) + data = hass.data.setdefault(DOMAIN, {}) config = entry.data websession = async_get_clientsession(hass) url = config[CONF_URL] @@ -143,9 +142,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: entry, data={**entry.data, CONF_NAME: name}, ) + data[entry.entry_id] = api - coordinator = HabitipyData(api) - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) if not hass.services.has_service(DOMAIN, SERVICE_API_CALL): diff --git a/homeassistant/components/habitica/coordinator.py b/homeassistant/components/habitica/coordinator.py deleted file mode 100644 index 19105714c6b..00000000000 --- a/homeassistant/components/habitica/coordinator.py +++ /dev/null @@ -1,67 +0,0 @@ -"""Data Coordinator for Habitica.""" - -from datetime import timedelta -from http import HTTPStatus -import logging -from typing import Any - -from aiohttp import ClientResponseError - -from homeassistant.util import Throttle - -from .const import DOMAIN - -_LOGGER = logging.getLogger(__name__) - -MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1) - - -class HabitipyData: - """Habitica API user data cache.""" - - tasks: dict[str, Any] = {} - - def __init__(self, api) -> None: - """Habitica API user data cache.""" - self.api = api - self.data = None - - @Throttle(MIN_TIME_BETWEEN_UPDATES) - async def update(self): - """Get a new fix from Habitica servers.""" - try: - self.data = await self.api.user.get() - except ClientResponseError as error: - if error.status == HTTPStatus.TOO_MANY_REQUESTS: - _LOGGER.warning( - ( - "Sensor data update for %s has too many API requests;" - " Skipping the update" - ), - DOMAIN, - ) - else: - _LOGGER.error( - "Count not update sensor data for %s (%s)", - DOMAIN, - error, - ) - - for task_type in ("habits", "dailys", "todos", "rewards", "completedTodos"): - try: - self.tasks[task_type] = await self.api.tasks.user.get(type=task_type) - except ClientResponseError as error: - if error.status == HTTPStatus.TOO_MANY_REQUESTS: - _LOGGER.warning( - ( - "Sensor data update for %s has too many API requests;" - " Skipping the update" - ), - DOMAIN, - ) - else: - _LOGGER.error( - "Count not update sensor data for %s (%s)", - DOMAIN, - error, - ) diff --git a/homeassistant/components/habitica/sensor.py b/homeassistant/components/habitica/sensor.py index 00775c32d16..33ab2361a0e 100644 --- a/homeassistant/components/habitica/sensor.py +++ b/homeassistant/components/habitica/sensor.py @@ -4,8 +4,13 @@ from __future__ import annotations from collections import namedtuple from dataclasses import dataclass +from datetime import timedelta from enum import StrEnum -from typing import TYPE_CHECKING +from http import HTTPStatus +import logging +from typing import TYPE_CHECKING, Any + +from aiohttp import ClientResponseError from homeassistant.components.sensor import ( SensorDeviceClass, @@ -17,9 +22,14 @@ from homeassistant.const import CONF_NAME, CONF_URL from homeassistant.core import HomeAssistant from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.util import Throttle from .const import DOMAIN, MANUFACTURER, NAME +_LOGGER = logging.getLogger(__name__) + +MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=15) + SensorType = namedtuple("SensorType", ["name", "icon", "unit", "path"]) @@ -160,7 +170,7 @@ async def async_setup_entry( """Set up the habitica sensors.""" name = config_entry.data[CONF_NAME] - sensor_data = hass.data[DOMAIN][config_entry.entry_id] + sensor_data = HabitipyData(hass.data[DOMAIN][config_entry.entry_id]) await sensor_data.update() entities: list[SensorEntity] = [ @@ -174,6 +184,58 @@ async def async_setup_entry( async_add_entities(entities, True) +class HabitipyData: + """Habitica API user data cache.""" + + tasks: dict[str, Any] + + def __init__(self, api) -> None: + """Habitica API user data cache.""" + self.api = api + self.data = None + self.tasks = {} + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + async def update(self): + """Get a new fix from Habitica servers.""" + try: + self.data = await self.api.user.get() + except ClientResponseError as error: + if error.status == HTTPStatus.TOO_MANY_REQUESTS: + _LOGGER.warning( + ( + "Sensor data update for %s has too many API requests;" + " Skipping the update" + ), + DOMAIN, + ) + else: + _LOGGER.error( + "Count not update sensor data for %s (%s)", + DOMAIN, + error, + ) + + for task_type in TASKS_TYPES: + try: + self.tasks[task_type] = await self.api.tasks.user.get(type=task_type) + except ClientResponseError as error: + if error.status == HTTPStatus.TOO_MANY_REQUESTS: + _LOGGER.warning( + ( + "Sensor data update for %s has too many API requests;" + " Skipping the update" + ), + DOMAIN, + ) + else: + _LOGGER.error( + "Count not update sensor data for %s (%s)", + DOMAIN, + error, + ) + + class HabitipySensor(SensorEntity): """A generic Habitica sensor."""