mirror of
https://github.com/jpawlowski/hass.tibber_prices.git
synced 2026-03-29 21:03:40 +00:00
Complete terminology migration from confusing "major/minor" to clearer
"base/subunit" currency naming throughout entire codebase, translations,
documentation, tests, and services.
BREAKING CHANGES:
1. **Service API Parameters Renamed**:
- `get_chartdata`: `minor_currency` → `subunit_currency`
- `get_apexcharts_yaml`: Updated service_data references from
`minor_currency: true` to `subunit_currency: true`
- All automations/scripts using these parameters MUST be updated
2. **Configuration Option Key Changed**:
- Config entry option: Display mode setting now uses new terminology
- Internal key: `currency_display_mode` values remain "base"/"subunit"
- User-facing labels updated in all 5 languages (de, en, nb, nl, sv)
3. **Sensor Entity Key Renamed**:
- `current_interval_price_major` → `current_interval_price_base`
- Entity ID changes: `sensor.tibber_home_current_interval_price_major`
→ `sensor.tibber_home_current_interval_price_base`
- Energy Dashboard configurations MUST update entity references
4. **Function Signatures Changed**:
- `format_price_unit_major()` → `format_price_unit_base()`
- `format_price_unit_minor()` → `format_price_unit_subunit()`
- `get_price_value()`: Parameter `in_euro` deprecated in favor of
`config_entry` (backward compatible for now)
5. **Translation Keys Renamed**:
- All language files: Sensor translation key
`current_interval_price_major` → `current_interval_price_base`
- Service parameter descriptions updated in all languages
- Selector options updated: Display mode dropdown values
Changes by Category:
**Core Code (Python)**:
- const.py: Renamed all format_price_unit_*() functions, updated docstrings
- entity_utils/helpers.py: Updated get_price_value() with config-driven
conversion and backward-compatible in_euro parameter
- sensor/__init__.py: Added display mode filtering for base currency sensor
- sensor/core.py:
* Implemented suggested_display_precision property for dynamic decimal places
* Updated native_unit_of_measurement to use get_display_unit_string()
* Updated all price conversion calls to use config_entry parameter
- sensor/definitions.py: Renamed entity key and updated all
suggested_display_precision values (2 decimals for most sensors)
- sensor/calculators/*.py: Updated all price conversion calls (8 calculators)
- sensor/helpers.py: Updated aggregate_price_data() signature with config_entry
- sensor/attributes/future.py: Updated future price attributes conversion
**Services**:
- services/chartdata.py: Renamed parameter minor_currency → subunit_currency
throughout (53 occurrences), updated metadata calculation
- services/apexcharts.py: Updated service_data references in generated YAML
- services/formatters.py: Renamed parameter use_minor_currency →
use_subunit_currency in aggregate_hourly_exact() and get_period_data()
- sensor/chart_metadata.py: Updated default parameter name
**Translations (5 Languages)**:
- All /translations/*.json:
* Added new config step "display_settings" with comprehensive explanations
* Renamed current_interval_price_major → current_interval_price_base
* Updated service parameter descriptions (subunit_currency)
* Added selector.currency_display_mode.options with translated labels
- All /custom_translations/*.json:
* Renamed sensor description keys
* Updated chart_metadata usage_tips references
**Documentation**:
- docs/user/docs/actions.md: Updated parameter table and feature list
- docs/user/versioned_docs/version-v0.21.0/actions.md: Backported changes
**Tests**:
- Updated 7 test files with renamed parameters and conversion logic:
* test_connect_segments.py: Renamed minor/major to subunit/base
* test_period_data_format.py: Updated period price conversion tests
* test_avg_none_fallback.py: Fixed tuple unpacking for new return format
* test_best_price_e2e.py: Added config_entry parameter to all calls
* test_cache_validity.py: Fixed cache data structure (price_info key)
* test_coordinator_shutdown.py: Added repair_manager mock
* test_midnight_turnover.py: Added config_entry parameter
* test_peak_price_e2e.py: Added config_entry parameter, fixed price_avg → price_mean
* test_percentage_calculations.py: Added config_entry mock
**Coordinator/Period Calculation**:
- coordinator/periods.py: Added config_entry parameter to
calculate_periods_with_relaxation() calls (2 locations)
Migration Guide:
1. **Update Service Calls in Automations/Scripts**:
\`\`\`yaml
# Before:
service: tibber_prices.get_chartdata
data:
minor_currency: true
# After:
service: tibber_prices.get_chartdata
data:
subunit_currency: true
\`\`\`
2. **Update Energy Dashboard Configuration**:
- Settings → Dashboards → Energy
- Replace sensor entity:
`sensor.tibber_home_current_interval_price_major` →
`sensor.tibber_home_current_interval_price_base`
3. **Review Integration Configuration**:
- Settings → Devices & Services → Tibber Prices → Configure
- New "Currency Display Settings" step added
- Default mode depends on currency (EUR → subunit, Scandinavian → base)
Rationale:
The "major/minor" terminology was confusing and didn't clearly communicate:
- **Major** → Unclear if this means "primary" or "large value"
- **Minor** → Easily confused with "less important" rather than "smaller unit"
New terminology is precise and self-explanatory:
- **Base currency** → Standard ISO currency (€, kr, $, £)
- **Subunit currency** → Fractional unit (ct, øre, ¢, p)
This aligns with:
- International terminology (ISO 4217 standard)
- Banking/financial industry conventions
- User expectations from payment processing systems
Impact: Aligns currency terminology with international standards. Users must
update service calls, automations, and Energy Dashboard configuration after
upgrade.
Refs: User feedback session (December 2025) identified terminology confusion
151 lines
4.6 KiB
Python
151 lines
4.6 KiB
Python
"""
|
|
Common helper functions for entities across platforms.
|
|
|
|
This module provides utility functions used by both sensor and binary_sensor platforms:
|
|
- Price value conversion (major/subunit currency units)
|
|
- Translation helpers (price levels, ratings)
|
|
- Time-based calculations (rolling hour center index)
|
|
|
|
These functions operate on entity-level concepts (states, translations) but are
|
|
platform-agnostic and can be used by both sensor and binary_sensor platforms.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
from custom_components.tibber_prices.const import get_display_unit_factor, get_price_level_translation
|
|
|
|
if TYPE_CHECKING:
|
|
from datetime import datetime
|
|
|
|
from custom_components.tibber_prices.coordinator.time_service import TibberPricesTimeService
|
|
from custom_components.tibber_prices.data import TibberPricesConfigEntry
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
|
|
def get_price_value(
|
|
price: float,
|
|
*,
|
|
in_euro: bool | None = None,
|
|
config_entry: ConfigEntry | TibberPricesConfigEntry | None = None,
|
|
) -> float:
|
|
"""
|
|
Convert price based on unit.
|
|
|
|
NOTE: This function supports two modes for backward compatibility:
|
|
1. Legacy mode: in_euro=True/False (hardcoded conversion)
|
|
2. New mode: config_entry (config-driven conversion)
|
|
|
|
New code should use get_display_unit_factor(config_entry) directly.
|
|
|
|
Args:
|
|
price: Price value to convert.
|
|
in_euro: (Legacy) If True, return in base currency; if False, in subunit currency.
|
|
config_entry: (New) Config entry to get display unit configuration.
|
|
|
|
Returns:
|
|
Price in requested unit (major or subunit currency units).
|
|
|
|
"""
|
|
# Legacy mode: use in_euro parameter
|
|
if in_euro is not None:
|
|
return price if in_euro else round(price * 100, 2)
|
|
|
|
# New mode: use config_entry
|
|
if config_entry is not None:
|
|
factor = get_display_unit_factor(config_entry)
|
|
return round(price * factor, 2)
|
|
|
|
# Fallback: default to subunit currency (backward compatibility)
|
|
return round(price * 100, 2)
|
|
|
|
|
|
def translate_level(hass: HomeAssistant, level: str) -> str:
|
|
"""
|
|
Translate price level to the user's language.
|
|
|
|
Args:
|
|
hass: HomeAssistant instance for language configuration
|
|
level: Price level to translate (e.g., VERY_CHEAP, NORMAL, etc.)
|
|
|
|
Returns:
|
|
Translated level string, or original level if translation not found
|
|
|
|
"""
|
|
if not hass:
|
|
return level
|
|
|
|
language = hass.config.language or "en"
|
|
translated = get_price_level_translation(level, language)
|
|
if translated:
|
|
return translated
|
|
|
|
if language != "en":
|
|
fallback = get_price_level_translation(level, "en")
|
|
if fallback:
|
|
return fallback
|
|
|
|
return level
|
|
|
|
|
|
def translate_rating_level(rating: str) -> str:
|
|
"""
|
|
Translate price rating level to the user's language.
|
|
|
|
Args:
|
|
rating: Price rating to translate (e.g., LOW, NORMAL, HIGH)
|
|
|
|
Returns:
|
|
Translated rating string, or original rating if translation not found
|
|
|
|
Note:
|
|
Currently returns the rating as-is. Translation mapping for ratings
|
|
can be added here when needed, similar to translate_level().
|
|
|
|
"""
|
|
# For now, ratings are returned as-is
|
|
# Add translation mapping here when needed
|
|
return rating
|
|
|
|
|
|
def find_rolling_hour_center_index(
|
|
all_prices: list[dict],
|
|
current_time: datetime,
|
|
hour_offset: int,
|
|
*,
|
|
time: TibberPricesTimeService,
|
|
) -> int | None:
|
|
"""
|
|
Find the center index for the rolling hour window.
|
|
|
|
Args:
|
|
all_prices: List of all price interval dictionaries with 'startsAt' key
|
|
current_time: Current datetime to find the current interval
|
|
hour_offset: Number of hours to offset from current interval (can be negative)
|
|
time: TibberPricesTimeService instance (required)
|
|
|
|
Returns:
|
|
Index of the center interval for the rolling hour window, or None if not found
|
|
|
|
"""
|
|
# Round to nearest interval boundary to handle edge cases where HA schedules
|
|
# us slightly before the boundary (e.g., 14:59:59.999 → 15:00:00)
|
|
target_time = time.round_to_nearest_quarter(current_time)
|
|
current_idx = None
|
|
|
|
for idx, price_data in enumerate(all_prices):
|
|
starts_at = time.get_interval_time(price_data)
|
|
if starts_at is None:
|
|
continue
|
|
|
|
# Exact match after rounding
|
|
if starts_at == target_time:
|
|
current_idx = idx
|
|
break
|
|
|
|
if current_idx is None:
|
|
return None
|
|
|
|
return current_idx + (hour_offset * 4)
|