1313 SensorStateClass ,
1414)
1515from homeassistant .config_entries import ConfigEntry
16- from homeassistant .const import PERCENTAGE , UnitOfPower
16+ from homeassistant .const import PERCENTAGE
1717from homeassistant .core import HomeAssistant
1818from homeassistant .helpers .entity_platform import AddEntitiesCallback
1919from homeassistant .helpers .update_coordinator import CoordinatorEntity , DataUpdateCoordinator
@@ -42,14 +42,42 @@ def _get_ef(data: dict[str, Any], key: str) -> float | None:
4242 return None
4343
4444
45+ def _get_ef_any (data : dict [str , Any ], * keys : str ) -> float | None :
46+ """Extract the first available numeric value from energy_flow using fallback keys."""
47+ for key in keys :
48+ value = _get_ef (data , key )
49+ if value is not None :
50+ return value
51+ return None
52+
53+
54+ def _get_ef_kw (data : dict [str , Any ], key : str ) -> float | None :
55+ """Extract a power value already reported in kW by the API."""
56+ return _get_ef (data , key )
57+
58+
59+ def _get_station_metric (data : dict [str , Any ], key : str ) -> float | None :
60+ """Extract a numeric value from station_info."""
61+ station = data .get ("station_info" , {})
62+ if not isinstance (station , dict ):
63+ return None
64+ value = station .get (key )
65+ if value is None :
66+ return None
67+ try :
68+ return float (value )
69+ except (TypeError , ValueError ):
70+ return None
71+
72+
4573SENSOR_DESCRIPTIONS : tuple [SigenEnergySensorDescription , ...] = (
4674 SigenEnergySensorDescription (
4775 key = "pv_power" ,
4876 translation_key = "pv_power" ,
49- native_unit_of_measurement = UnitOfPower . WATT ,
77+ native_unit_of_measurement = "kW" ,
5078 device_class = SensorDeviceClass .POWER ,
5179 state_class = SensorStateClass .MEASUREMENT ,
52- value_fn = lambda d : _get_ef (d , "pvPower" ),
80+ value_fn = lambda d : _get_ef_kw (d , "pvPower" ),
5381 ),
5482 SigenEnergySensorDescription (
5583 key = "battery_soc" ,
@@ -69,32 +97,46 @@ def _get_ef(data: dict[str, Any], key: str) -> float | None:
6997 SigenEnergySensorDescription (
7098 key = "battery_power" ,
7199 translation_key = "battery_power" ,
72- native_unit_of_measurement = UnitOfPower . WATT ,
100+ native_unit_of_measurement = "kW" ,
73101 device_class = SensorDeviceClass .POWER ,
74102 state_class = SensorStateClass .MEASUREMENT ,
75- value_fn = lambda d : _get_ef (d , "batteryPower" ),
103+ value_fn = lambda d : _get_ef_kw (d , "batteryPower" ),
76104 ),
77105 SigenEnergySensorDescription (
78106 key = "grid_power" ,
79107 translation_key = "grid_power" ,
80- native_unit_of_measurement = UnitOfPower . WATT ,
108+ native_unit_of_measurement = "kW" ,
81109 device_class = SensorDeviceClass .POWER ,
82110 state_class = SensorStateClass .MEASUREMENT ,
83- value_fn = lambda d : _get_ef ( d , "gridPower" ),
111+ value_fn = lambda d : _get_ef_any ( d , "buySellPower" , "gridPower" ),
84112 ),
85113 SigenEnergySensorDescription (
86114 key = "load_power" ,
87115 translation_key = "load_power" ,
88- native_unit_of_measurement = UnitOfPower . WATT ,
116+ native_unit_of_measurement = "kW" ,
89117 device_class = SensorDeviceClass .POWER ,
90118 state_class = SensorStateClass .MEASUREMENT ,
91- value_fn = lambda d : _get_ef (d , "loadPower" ),
119+ value_fn = lambda d : _get_ef_kw (d , "loadPower" ),
92120 ),
93121 SigenEnergySensorDescription (
94122 key = "system_online" ,
95123 translation_key = "system_online" ,
96124 value_fn = lambda d : d .get ("energy_flow" , {}).get ("online" ),
97125 ),
126+ SigenEnergySensorDescription (
127+ key = "pv_capacity" ,
128+ translation_key = "pv_capacity" ,
129+ native_unit_of_measurement = "kW" ,
130+ state_class = SensorStateClass .MEASUREMENT ,
131+ value_fn = lambda d : _get_station_metric (d , "pvCapacity" ),
132+ ),
133+ SigenEnergySensorDescription (
134+ key = "battery_capacity" ,
135+ translation_key = "battery_capacity" ,
136+ native_unit_of_measurement = "kWh" ,
137+ state_class = SensorStateClass .MEASUREMENT ,
138+ value_fn = lambda d : _get_station_metric (d , "batteryCapacity" ),
139+ ),
98140)
99141
100142
@@ -107,9 +149,10 @@ async def async_setup_entry(
107149 data : SigenEnergyData = hass .data [DOMAIN ][entry .entry_id ]
108150 coordinator = data .coordinator
109151 station_id = data .station_id
152+ station_info = data .station_info
110153
111154 entities = [
112- SigenEnergySensor (coordinator , description , station_id )
155+ SigenEnergySensor (coordinator , description , station_id , station_info )
113156 for description in SENSOR_DESCRIPTIONS
114157 ]
115158 async_add_entities (entities )
@@ -128,10 +171,12 @@ def __init__(
128171 coordinator : DataUpdateCoordinator [dict [str , Any ]],
129172 description : SigenEnergySensorDescription ,
130173 station_id : str ,
174+ station_info : dict [str , Any ],
131175 ) -> None :
132176 super ().__init__ (coordinator )
133177 self .entity_description = description
134178 self ._station_id = station_id
179+ self ._station_info = station_info
135180 self ._attr_unique_id = f"{ station_id } _{ description .key } "
136181 self ._attr_device_info = {
137182 "identifiers" : {(DOMAIN , station_id )},
@@ -144,4 +189,6 @@ def native_value(self) -> float | str | None:
144189 """Return the sensor value."""
145190 if self .coordinator .data is None :
146191 return None
147- return self .entity_description .value_fn (self .coordinator .data )
192+ merged_data = dict (self .coordinator .data )
193+ merged_data .setdefault ("station_info" , self ._station_info )
194+ return self .entity_description .value_fn (merged_data )
0 commit comments