Skip to content

Commit dbd8b1b

Browse files
Bre77edenhaus
authored andcommitted
Fix history coordinator in Tesla Fleet and Teslemetry (home-assistant#153068)
Co-authored-by: Robert Resch <[email protected]>
1 parent d135f1c commit dbd8b1b

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

homeassistant/components/tesla_fleet/coordinator.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,11 +251,14 @@ async def _async_update_data(self) -> dict[str, Any]:
251251
raise UpdateFailed("Received invalid data")
252252

253253
# Add all time periods together
254-
output = dict.fromkeys(ENERGY_HISTORY_FIELDS, 0)
254+
output = dict.fromkeys(ENERGY_HISTORY_FIELDS, None)
255255
for period in data.get("time_series", []):
256256
for key in ENERGY_HISTORY_FIELDS:
257257
if key in period:
258-
output[key] += period[key]
258+
if output[key] is None:
259+
output[key] = period[key]
260+
else:
261+
output[key] += period[key]
259262

260263
return output
261264

homeassistant/components/teslemetry/coordinator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,13 @@ async def _async_update_data(self) -> dict[str, Any]:
199199
raise UpdateFailed("Received invalid data")
200200

201201
# Add all time periods together
202-
output = dict.fromkeys(ENERGY_HISTORY_FIELDS, 0)
203-
for period in data["time_series"]:
202+
output = dict.fromkeys(ENERGY_HISTORY_FIELDS, None)
203+
for period in data.get("time_series", []):
204204
for key in ENERGY_HISTORY_FIELDS:
205205
if key in period:
206-
output[key] += period[key]
206+
if output[key] is None:
207+
output[key] = period[key]
208+
else:
209+
output[key] += period[key]
207210

208211
return output

tests/components/tesla_fleet/snapshots/test_sensor.ambr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
'last_changed': <ANY>,
7272
'last_reported': <ANY>,
7373
'last_updated': <ANY>,
74-
'state': '0.0',
74+
'state': 'unknown',
7575
})
7676
# ---
7777
# name: test_sensors[sensor.energy_site_battery_discharged-entry]
@@ -146,7 +146,7 @@
146146
'last_changed': <ANY>,
147147
'last_reported': <ANY>,
148148
'last_updated': <ANY>,
149-
'state': '0.0',
149+
'state': 'unknown',
150150
})
151151
# ---
152152
# name: test_sensors[sensor.energy_site_battery_exported-entry]
@@ -1121,7 +1121,7 @@
11211121
'last_changed': <ANY>,
11221122
'last_reported': <ANY>,
11231123
'last_updated': <ANY>,
1124-
'state': '0.0',
1124+
'state': 'unknown',
11251125
})
11261126
# ---
11271127
# name: test_sensors[sensor.energy_site_grid_exported_from_battery-entry]
@@ -1881,7 +1881,7 @@
18811881
'last_changed': <ANY>,
18821882
'last_reported': <ANY>,
18831883
'last_updated': <ANY>,
1884-
'state': '0.0',
1884+
'state': 'unknown',
18851885
})
18861886
# ---
18871887
# name: test_sensors[sensor.energy_site_load_power-entry]
@@ -2178,7 +2178,7 @@
21782178
'last_changed': <ANY>,
21792179
'last_reported': <ANY>,
21802180
'last_updated': <ANY>,
2181-
'state': '0.0',
2181+
'state': 'unknown',
21822182
})
21832183
# ---
21842184
# name: test_sensors[sensor.energy_site_solar_power-entry]

0 commit comments

Comments
 (0)