fix: state-to-watts conversion in case of non-w sensor
This commit is contained in:
@@ -75,6 +75,17 @@ class EaseeSolarCoordinator(DataUpdateCoordinator):
|
||||
)
|
||||
self._setup_state_listeners()
|
||||
|
||||
@staticmethod
|
||||
def _state_to_watts(state) -> float | None:
|
||||
"""Parse a power sensor state, converting kW → W when the unit says so."""
|
||||
try:
|
||||
value = float(state.state)
|
||||
except (ValueError, TypeError):
|
||||
return None
|
||||
if state.attributes.get("unit_of_measurement", "").upper() == "KW":
|
||||
value *= 1000
|
||||
return value
|
||||
|
||||
def _setup_state_listeners(self) -> None:
|
||||
@callback
|
||||
def _on_state_change(event) -> None:
|
||||
@@ -82,9 +93,8 @@ class EaseeSolarCoordinator(DataUpdateCoordinator):
|
||||
new_state = event.data.get("new_state")
|
||||
if new_state is None or new_state.state in ("unknown", "unavailable"):
|
||||
return
|
||||
try:
|
||||
value = float(new_state.state)
|
||||
except ValueError:
|
||||
value = self._state_to_watts(new_state)
|
||||
if value is None:
|
||||
return
|
||||
samples = self._samples[entity_id]
|
||||
samples.append((datetime.now(timezone.utc), value))
|
||||
@@ -105,10 +115,7 @@ class EaseeSolarCoordinator(DataUpdateCoordinator):
|
||||
state = self.hass.states.get(entity_id)
|
||||
if state is None or state.state in ("unknown", "unavailable"):
|
||||
return None
|
||||
try:
|
||||
return float(state.state)
|
||||
except ValueError:
|
||||
return None
|
||||
return self._state_to_watts(state)
|
||||
|
||||
@property
|
||||
def _stop_grace(self) -> timedelta:
|
||||
|
||||
Reference in New Issue
Block a user