Skip to content

Commit

Permalink
Always provide a currentArmLevel in Google assistant (#119238)
Browse files Browse the repository at this point in the history
  • Loading branch information
elupus authored and frenck committed Jun 11, 2024
1 parent 1e7ab07 commit 119d4c2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
32 changes: 19 additions & 13 deletions homeassistant/components/google_assistant/trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,17 @@ def _supported_states(self):
if features & required_feature != 0
]

def _default_arm_state(self):
states = self._supported_states()

if STATE_ALARM_TRIGGERED in states:
states.remove(STATE_ALARM_TRIGGERED)

if len(states) != 1:
raise SmartHomeError(ERR_NOT_SUPPORTED, "ArmLevel missing")

return states[0]

def sync_attributes(self):
"""Return ArmDisarm attributes for a sync request."""
response = {}
Expand All @@ -1609,26 +1620,21 @@ def sync_attributes(self):
def query_attributes(self):
"""Return ArmDisarm query attributes."""
armed_state = self.state.attributes.get("next_state", self.state.state)
response = {"isArmed": armed_state in self.state_to_service}
if response["isArmed"]:
response.update({"currentArmLevel": armed_state})
return response

if armed_state in self.state_to_service:
return {"isArmed": True, "currentArmLevel": armed_state}
return {
"isArmed": False,
"currentArmLevel": self._default_arm_state(),
}

async def execute(self, command, data, params, challenge):
"""Execute an ArmDisarm command."""
if params["arm"] and not params.get("cancel"):
# If no arm level given, we can only arm it if there is
# only one supported arm type. We never default to triggered.
if not (arm_level := params.get("armLevel")):
states = self._supported_states()

if STATE_ALARM_TRIGGERED in states:
states.remove(STATE_ALARM_TRIGGERED)

if len(states) != 1:
raise SmartHomeError(ERR_NOT_SUPPORTED, "ArmLevel missing")

arm_level = states[0]
arm_level = self._default_arm_state()

if self.state.state == arm_level:
raise SmartHomeError(ERR_ALREADY_ARMED, "System is already armed")
Expand Down
5 changes: 4 additions & 1 deletion tests/components/google_assistant/test_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -1931,7 +1931,10 @@ async def test_arm_disarm_disarm(hass: HomeAssistant) -> None:
}
}

assert trt.query_attributes() == {"isArmed": False}
assert trt.query_attributes() == {
"currentArmLevel": "armed_custom_bypass",
"isArmed": False,
}

assert trt.can_execute(trait.COMMAND_ARMDISARM, {"arm": False})

Expand Down

0 comments on commit 119d4c2

Please sign in to comment.