hass.tibber_prices/custom_components/tibber_prices/services/refresh_user_data.py
Julian Pawlowski ac24f6a8cb refactor(services): split monolithic services.py into package
Split services.py (1,097 lines) into modular package (6 files, ~200-600 lines each):

Structure:
- services/__init__.py: Service registration (70 lines)
- services/helpers.py: Entry validation (55 lines)
- services/formatters.py: Data transformation (380 lines)
- services/chartdata.py: Chart data export handler (600 lines)
- services/apexcharts.py: ApexCharts YAML generator (240 lines)
- services/refresh_user_data.py: User data refresh (110 lines)

Benefits:
- Clear separation of concerns (helpers, formatters, handlers)
- Each service isolated and independently testable
- Consistent handler naming (handle_* pattern)
- Better code reuse through formatters module

All services working identically (get_chartdata, get_apexcharts_yaml,
refresh_user_data). Updated __init__.py to import from services package.

Impact: Improved maintainability, reduced max file size from 1,097
to 600 lines. Architecture quality improved from 7.5/10 to ~8.5/10.
2025-11-18 20:07:05 +00:00

113 lines
3 KiB
Python

"""
User data refresh service handler.
This module implements the `refresh_user_data` service, which forces a refresh
of user profile and home information from the Tibber API.
Features:
- Force refresh of cached user data
- Bypass 24h cache TTL
- Return updated user profile and homes
- Error handling for API failures
Service: tibber_prices.refresh_user_data
Response: JSON with refresh status and updated data
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, Final
import voluptuous as vol
from custom_components.tibber_prices.api import (
TibberPricesApiClientAuthenticationError,
TibberPricesApiClientCommunicationError,
TibberPricesApiClientError,
)
from homeassistant.exceptions import ServiceValidationError
from .helpers import get_entry_and_data
if TYPE_CHECKING:
from homeassistant.core import ServiceCall
# Service constants
REFRESH_USER_DATA_SERVICE_NAME: Final = "refresh_user_data"
ATTR_ENTRY_ID: Final = "entry_id"
# Service schema
REFRESH_USER_DATA_SERVICE_SCHEMA: Final = vol.Schema(
{
vol.Required(ATTR_ENTRY_ID): str,
}
)
async def handle_refresh_user_data(call: ServiceCall) -> dict[str, Any]:
"""
Refresh user data for a specific config entry.
Forces a refresh of user profile and home information from Tibber API,
bypassing the 24-hour cache TTL. Returns updated information or error details.
See services.yaml for detailed parameter documentation.
Args:
call: Service call with parameters
Returns:
Dictionary with refresh status and updated data
Raises:
ServiceValidationError: If entry_id is missing or invalid
"""
entry_id = call.data.get(ATTR_ENTRY_ID)
hass = call.hass
if not entry_id:
return {
"success": False,
"message": "Entry ID is required",
}
# Get the entry and coordinator
try:
_, coordinator, _ = get_entry_and_data(hass, entry_id)
except ServiceValidationError as ex:
return {
"success": False,
"message": f"Invalid entry ID: {ex}",
}
# Force refresh user data using the public method
try:
updated = await coordinator.refresh_user_data()
except (
TibberPricesApiClientAuthenticationError,
TibberPricesApiClientCommunicationError,
TibberPricesApiClientError,
) as ex:
return {
"success": False,
"message": f"API error refreshing user data: {ex!s}",
}
else:
if updated:
user_profile = coordinator.get_user_profile()
homes = coordinator.get_user_homes()
return {
"success": True,
"message": "User data refreshed successfully",
"user_profile": user_profile,
"homes_count": len(homes),
"homes": homes,
"last_updated": user_profile.get("last_updated"),
}
return {
"success": False,
"message": "User data was already up to date",
}