hass-core/homeassistant/components/pvpc_hourly_pricing/helpers.py
Eugenio Panadero b96d2cadac
Add new price sensors with API token access to pvpc hourly pricing (#85769)
*  Implement optional API token in config-flow + options

to make the data download from an authenticated path in ESIOS server

As this is an *alternative* access, and current public path works for the PVPC,
no user (current or new) is compelled to obtain a token,
and it can be enabled anytime in options, or doing the setup again

When enabling the token, it is verified (or "invalid_auth" error),
and a 'reauth' flow is implemented, which can change or disable the token if
it starts failing.

The 1st step of config/options flow adds a bool to enable this private access,
- if unchecked (default), entry is set for public access (like before)
- if checked, a 2nd step opens to input the token, with instructions
  of how to get one (with a direct link to create a 'request email').
  If the token is valid, the entry is set for authenticated access

The 'reauth' flow shows the boolean flag so the user could disable a bad token
by unchecking the boolean flag 'use_api_token'

* ♻️ Remove storage of flag 'use_api_token' in config entry

leaving it only to enable/disable the optional token in the config-flow

* ♻️ Adjust async_update_options

*  Add new price sensors with API token access

New price sensors added:
- Injection price: price of excess energy from self-consumption
- OMIE price: electricity price in the 'open' market
- MAG price: Temporal tax cost for gas compensation

*  Adapt tests to work with multiple sensors

* 🐛 Fix all integration sensors going unavailable

when any sensor lacks data for the current day (usually the 'OMIE price')

* Fix rebase

* Customize icons and display precision for new sensors

* Disable MAG Tax and OMIE price sensors by default

* Move logic to assign sensor unique ids to integration

* Move helper functions to helpers.py

* Fix sensor activation for API download
2023-12-18 21:06:02 +01:00

49 lines
1.4 KiB
Python

"""Helper functions to relate sensors keys and unique ids."""
from aiopvpc.const import (
ALL_SENSORS,
KEY_INJECTION,
KEY_MAG,
KEY_OMIE,
KEY_PVPC,
TARIFFS,
)
from homeassistant.helpers.entity_registry import RegistryEntry
_ha_uniqueid_to_sensor_key = {
TARIFFS[0]: KEY_PVPC,
TARIFFS[1]: KEY_PVPC,
f"{TARIFFS[0]}_{KEY_INJECTION}": KEY_INJECTION,
f"{TARIFFS[1]}_{KEY_INJECTION}": KEY_INJECTION,
f"{TARIFFS[0]}_{KEY_MAG}": KEY_MAG,
f"{TARIFFS[1]}_{KEY_MAG}": KEY_MAG,
f"{TARIFFS[0]}_{KEY_OMIE}": KEY_OMIE,
f"{TARIFFS[1]}_{KEY_OMIE}": KEY_OMIE,
}
def get_enabled_sensor_keys(
using_private_api: bool, entries: list[RegistryEntry]
) -> set[str]:
"""Get enabled API indicators."""
if not using_private_api:
return {KEY_PVPC}
if len(entries) > 1:
# activate only enabled sensors
return {
_ha_uniqueid_to_sensor_key[sensor.unique_id]
for sensor in entries
if not sensor.disabled
}
# default sensors when enabling token access
return {KEY_PVPC, KEY_INJECTION}
def make_sensor_unique_id(config_entry_id: str | None, sensor_key: str) -> str:
"""Generate unique_id for each sensor kind and config entry."""
assert sensor_key in ALL_SENSORS
assert config_entry_id is not None
if sensor_key == KEY_PVPC:
# for old compatibility
return config_entry_id
return f"{config_entry_id}_{sensor_key}"