refactor: Update price level and rating options to inline definitions for sensor initialization

This commit is contained in:
Julian Pawlowski 2025-11-08 09:24:28 +00:00
parent 9c6ebc45ec
commit 3f3edd8a28
2 changed files with 19 additions and 12 deletions

View file

@ -128,6 +128,9 @@ PRICE_RATING_NORMAL = "NORMAL"
PRICE_RATING_HIGH = "HIGH" PRICE_RATING_HIGH = "HIGH"
# Sensor options (lowercase versions for ENUM device class) # Sensor options (lowercase versions for ENUM device class)
# NOTE: These constants define the valid enum options, but they are not used directly
# in sensor.py due to import timing issues. Instead, the options are defined inline
# in the SensorEntityDescription objects. Keep these in sync with sensor.py!
PRICE_LEVEL_OPTIONS = [ PRICE_LEVEL_OPTIONS = [
PRICE_LEVEL_VERY_CHEAP.lower(), PRICE_LEVEL_VERY_CHEAP.lower(),
PRICE_LEVEL_CHEAP.lower(), PRICE_LEVEL_CHEAP.lower(),

View file

@ -34,9 +34,7 @@ from .const import (
DEFAULT_PRICE_RATING_THRESHOLD_LOW, DEFAULT_PRICE_RATING_THRESHOLD_LOW,
DOMAIN, DOMAIN,
PRICE_LEVEL_MAPPING, PRICE_LEVEL_MAPPING,
PRICE_LEVEL_OPTIONS,
PRICE_RATING_MAPPING, PRICE_RATING_MAPPING,
PRICE_RATING_OPTIONS,
async_get_entity_description, async_get_entity_description,
format_price_unit_minor, format_price_unit_minor,
get_entity_description, get_entity_description,
@ -110,13 +108,16 @@ PRICE_SENSORS = (
device_class=SensorDeviceClass.MONETARY, device_class=SensorDeviceClass.MONETARY,
suggested_display_precision=1, suggested_display_precision=1,
), ),
# NOTE: Enum options are defined inline (not imported from const.py) to avoid
# import timing issues with Home Assistant's entity platform initialization.
# Keep in sync with PRICE_LEVEL_OPTIONS in const.py!
SensorEntityDescription( SensorEntityDescription(
key="price_level", key="price_level",
translation_key="price_level", translation_key="price_level",
name="Current Price Level", name="Current Price Level",
icon="mdi:gauge", icon="mdi:gauge",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_LEVEL_OPTIONS, options=["very_cheap", "cheap", "normal", "expensive", "very_expensive"],
), ),
SensorEntityDescription( SensorEntityDescription(
key="next_interval_price_level", key="next_interval_price_level",
@ -124,7 +125,7 @@ PRICE_SENSORS = (
name="Next Price Level", name="Next Price Level",
icon="mdi:gauge-empty", icon="mdi:gauge-empty",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_LEVEL_OPTIONS, options=["very_cheap", "cheap", "normal", "expensive", "very_expensive"],
), ),
SensorEntityDescription( SensorEntityDescription(
key="previous_interval_price_level", key="previous_interval_price_level",
@ -133,7 +134,7 @@ PRICE_SENSORS = (
icon="mdi:gauge-empty", icon="mdi:gauge-empty",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_LEVEL_OPTIONS, options=["very_cheap", "cheap", "normal", "expensive", "very_expensive"],
), ),
SensorEntityDescription( SensorEntityDescription(
key="current_hour_price_level", key="current_hour_price_level",
@ -141,7 +142,7 @@ PRICE_SENSORS = (
name="Current Hour Price Level", name="Current Hour Price Level",
icon="mdi:gauge", icon="mdi:gauge",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_LEVEL_OPTIONS, options=["very_cheap", "cheap", "normal", "expensive", "very_expensive"],
), ),
SensorEntityDescription( SensorEntityDescription(
key="next_hour_price_level", key="next_hour_price_level",
@ -149,7 +150,7 @@ PRICE_SENSORS = (
name="Next Hour Price Level", name="Next Hour Price Level",
icon="mdi:gauge-empty", icon="mdi:gauge-empty",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_LEVEL_OPTIONS, options=["very_cheap", "cheap", "normal", "expensive", "very_expensive"],
), ),
) )
@ -257,6 +258,9 @@ STATISTICS_SENSORS = (
) )
# Rating sensors # Rating sensors
# NOTE: Enum options are defined inline (not imported from const.py) to avoid
# import timing issues with Home Assistant's entity platform initialization.
# Keep in sync with PRICE_RATING_OPTIONS in const.py!
RATING_SENSORS = ( RATING_SENSORS = (
SensorEntityDescription( SensorEntityDescription(
key="price_rating", key="price_rating",
@ -264,7 +268,7 @@ RATING_SENSORS = (
name="Current Price Rating", name="Current Price Rating",
icon="mdi:star-outline", icon="mdi:star-outline",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_RATING_OPTIONS, options=["low", "normal", "high"],
), ),
SensorEntityDescription( SensorEntityDescription(
key="next_interval_price_rating", key="next_interval_price_rating",
@ -272,7 +276,7 @@ RATING_SENSORS = (
name="Next Price Rating", name="Next Price Rating",
icon="mdi:star-half-full", icon="mdi:star-half-full",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_RATING_OPTIONS, options=["low", "normal", "high"],
), ),
SensorEntityDescription( SensorEntityDescription(
key="previous_interval_price_rating", key="previous_interval_price_rating",
@ -281,7 +285,7 @@ RATING_SENSORS = (
icon="mdi:star-half-full", icon="mdi:star-half-full",
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_RATING_OPTIONS, options=["low", "normal", "high"],
), ),
SensorEntityDescription( SensorEntityDescription(
key="current_hour_price_rating", key="current_hour_price_rating",
@ -289,7 +293,7 @@ RATING_SENSORS = (
name="Current Hour Price Rating", name="Current Hour Price Rating",
icon="mdi:star-outline", icon="mdi:star-outline",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_RATING_OPTIONS, options=["low", "normal", "high"],
), ),
SensorEntityDescription( SensorEntityDescription(
key="next_hour_price_rating", key="next_hour_price_rating",
@ -297,7 +301,7 @@ RATING_SENSORS = (
name="Next Hour Price Rating", name="Next Hour Price Rating",
icon="mdi:star-half-full", icon="mdi:star-half-full",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=PRICE_RATING_OPTIONS, options=["low", "normal", "high"],
), ),
) )