Skip to content

make alarm.py more defensive to prevent failures #41

@mike-dubman

Description

@mike-dubman

When running test.py from the package and calling print(alarm.getEmergencyTypes()) it fails with KeyErrror: "has_fire"
proposing to add capability bits to make code more defensive as below

diff --git a/pyControl4/alarm.py b/pyControl4/alarm.py
index aecb476..c461254 100644
--- a/pyControl4/alarm.py
+++ b/pyControl4/alarm.py
@@ -141,13 +141,14 @@ class C4SecurityPanel(C4Entity):
         data = await self.director.getItemInfo(self.item_id)
         jsonDictionary = json.loads(data)

-        if jsonDictionary[0]["capabilities"]["has_fire"]:
+        capabilities = jsonDictionary[0].get("capabilities", {}) if jsonDictionary else {}
+        if capabilities.get("has_fire"):
             types_list.append("Fire")
-        if jsonDictionary[0]["capabilities"]["has_medical"]:
+        if capabilities.get("has_medical"):
             types_list.append("Medical")
-        if jsonDictionary[0]["capabilities"]["has_panic"]:
+        if capabilities.get("has_panic"):
             types_list.append("Panic")
-        if jsonDictionary[0]["capabilities"]["has_police"]:
+        if capabilities.get("has_police"):
             types_list.append("Police")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions