Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always provide a currentArmLevel in Google assistant #119238

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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