Skip to content

Commit

Permalink
Allow arm levels be in order for google assistant (#119645)
Browse files Browse the repository at this point in the history
  • Loading branch information
elupus authored Jun 14, 2024
1 parent 95b9e15 commit 097844a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
13 changes: 7 additions & 6 deletions homeassistant/components/google_assistant/trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,19 +1553,20 @@ class ArmDisArmTrait(_Trait):

state_to_service = {
STATE_ALARM_ARMED_HOME: SERVICE_ALARM_ARM_HOME,
STATE_ALARM_ARMED_AWAY: SERVICE_ALARM_ARM_AWAY,
STATE_ALARM_ARMED_NIGHT: SERVICE_ALARM_ARM_NIGHT,
STATE_ALARM_ARMED_AWAY: SERVICE_ALARM_ARM_AWAY,
STATE_ALARM_ARMED_CUSTOM_BYPASS: SERVICE_ALARM_ARM_CUSTOM_BYPASS,
STATE_ALARM_TRIGGERED: SERVICE_ALARM_TRIGGER,
}

state_to_support = {
STATE_ALARM_ARMED_HOME: AlarmControlPanelEntityFeature.ARM_HOME,
STATE_ALARM_ARMED_AWAY: AlarmControlPanelEntityFeature.ARM_AWAY,
STATE_ALARM_ARMED_NIGHT: AlarmControlPanelEntityFeature.ARM_NIGHT,
STATE_ALARM_ARMED_AWAY: AlarmControlPanelEntityFeature.ARM_AWAY,
STATE_ALARM_ARMED_CUSTOM_BYPASS: AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS,
STATE_ALARM_TRIGGERED: AlarmControlPanelEntityFeature.TRIGGER,
}
"""The list of states to support in increasing security state."""

@staticmethod
def supported(domain, features, device_class, _):
Expand All @@ -1592,7 +1593,7 @@ def _default_arm_state(self):
if STATE_ALARM_TRIGGERED in states:
states.remove(STATE_ALARM_TRIGGERED)

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

return states[0]
Expand All @@ -1614,7 +1615,7 @@ def sync_attributes(self):
}
levels.append(level)

response["availableArmLevels"] = {"levels": levels, "ordered": False}
response["availableArmLevels"] = {"levels": levels, "ordered": True}
return response

def query_attributes(self):
Expand All @@ -1631,8 +1632,8 @@ def query_attributes(self):
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 no arm level given, we we arm the first supported
# level in state_to_support.
if not (arm_level := params.get("armLevel")):
arm_level = self._default_arm_state()

Expand Down
22 changes: 16 additions & 6 deletions tests/components/google_assistant/test_trait.py
Original file line number Diff line number Diff line change
Expand Up @@ -1763,7 +1763,7 @@ async def test_arm_disarm_arm_away(hass: HomeAssistant) -> None:
],
},
],
"ordered": False,
"ordered": True,
}
}

Expand Down Expand Up @@ -1905,7 +1905,8 @@ async def test_arm_disarm_disarm(hass: HomeAssistant) -> None:
{
alarm_control_panel.ATTR_CODE_ARM_REQUIRED: True,
ATTR_SUPPORTED_FEATURES: AlarmControlPanelEntityFeature.TRIGGER
| AlarmControlPanelEntityFeature.ARM_CUSTOM_BYPASS,
| AlarmControlPanelEntityFeature.ARM_HOME
| AlarmControlPanelEntityFeature.ARM_AWAY,
},
),
PIN_CONFIG,
Expand All @@ -1914,10 +1915,19 @@ async def test_arm_disarm_disarm(hass: HomeAssistant) -> None:
"availableArmLevels": {
"levels": [
{
"level_name": "armed_custom_bypass",
"level_name": "armed_home",
"level_values": [
{
"level_synonym": ["armed home", "home"],
"lang": "en",
}
],
},
{
"level_name": "armed_away",
"level_values": [
{
"level_synonym": ["armed custom bypass", "custom"],
"level_synonym": ["armed away", "away"],
"lang": "en",
}
],
Expand All @@ -1927,12 +1937,12 @@ async def test_arm_disarm_disarm(hass: HomeAssistant) -> None:
"level_values": [{"level_synonym": ["triggered"], "lang": "en"}],
},
],
"ordered": False,
"ordered": True,
}
}

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

Expand Down

0 comments on commit 097844a

Please sign in to comment.