Skip to content

Commit e716158

Browse files
committed
Fixed issue related to null data in latest launch from SpaceX API. Fixes #33
1 parent fa896b2 commit e716158

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

custom_components/spacex/sensor.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -414,26 +414,37 @@ def device_state_attributes(self):
414414
elif self._kind == "spacex_latest_launch_rocket":
415415
core_counter = 1
416416
for this_core in latest_launch_data["cores_detail"]:
417-
self.attrs["core_" + str(core_counter) + "_serial"] = this_core["details"][
418-
"serial"
419-
]
420-
self.attrs["core_" + str(core_counter) + "_flight"] = this_core[
417+
if this_core.get("details"):
418+
self.attrs["core_" + str(core_counter) + "_serial"] = this_core.get("details",{}).get(
419+
"serial"
420+
)
421+
self.attrs["core_" + str(core_counter) + "_block"] = this_core.get("details",{}).get(
422+
"block"
423+
)
424+
else:
425+
self.attrs["core_" + str(core_counter) + "_serial"] = None
426+
self.attrs["core_" + str(core_counter) + "_block"] = None
427+
428+
self.attrs["core_" + str(core_counter) + "_flight"] = this_core.get(
421429
"flight"
422-
]
423-
self.attrs["core_" + str(core_counter) + "_block"] = this_core["details"][
424-
"block"
425-
]
430+
)
431+
426432
self.attrs[
427433
"core_" + str(core_counter) + "_landing_intent"
428-
] = this_core["landing_attempt"]
434+
] = this_core.get("landing_attempt")
435+
436+
if this_core.get("landpad"):
437+
self.attrs["core_" + str(core_counter) + "_lz"] = this_core.get("landpad",{}).get(
438+
"name"
439+
)
429440

430-
self.attrs["core_" + str(core_counter) + "_lz"] = this_core["landpad"][
431-
"name"
432-
]
441+
self.attrs["core_" + str(core_counter) + "_lz_long"] = this_core.get("landpad",{}).get(
442+
"full_name"
443+
)
444+
else:
445+
self.attrs["core_" + str(core_counter) + "_lz"] = None
446+
self.attrs["core_" + str(core_counter) + "_lz_long"] = None
433447

434-
self.attrs["core_" + str(core_counter) + "_lz_long"] = this_core["landpad"][
435-
"full_name"
436-
]
437448
core_counter = core_counter + 1
438449

439450
if latest_launch_data.get("fairings"):

0 commit comments

Comments
 (0)