-
-
Notifications
You must be signed in to change notification settings - Fork 37.8k
Add google_assistant alarm_control_panel #26249
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
Merged
balloob
merged 7 commits into
home-assistant:dev
from
engrbm87:alarm_control_panel_google_assistant
Sep 25, 2019
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e10312b
add alarm_control_panel to google_assistant
engrbm87 36327af
add cancel arming option
engrbm87 9b08bc2
raise error if requested state is same as current
engrbm87 bfd9cd8
rework executing command logic
engrbm87 c4dfd4c
Add tests
engrbm87 29bb6e6
fixed tests
engrbm87 e5d94d0
fixed level synonyms
engrbm87 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| sensor, | ||
| switch, | ||
| vacuum, | ||
| alarm_control_panel, | ||
| ) | ||
| from homeassistant.components.climate import const as climate | ||
| from homeassistant.const import ( | ||
|
|
@@ -31,6 +32,20 @@ | |
| ATTR_SUPPORTED_FEATURES, | ||
| ATTR_TEMPERATURE, | ||
| ATTR_ASSUMED_STATE, | ||
| SERVICE_ALARM_DISARM, | ||
| SERVICE_ALARM_ARM_HOME, | ||
| SERVICE_ALARM_ARM_AWAY, | ||
| SERVICE_ALARM_ARM_NIGHT, | ||
| SERVICE_ALARM_ARM_CUSTOM_BYPASS, | ||
| SERVICE_ALARM_TRIGGER, | ||
| STATE_ALARM_ARMED_HOME, | ||
| STATE_ALARM_ARMED_AWAY, | ||
| STATE_ALARM_ARMED_NIGHT, | ||
| STATE_ALARM_ARMED_CUSTOM_BYPASS, | ||
| STATE_ALARM_DISARMED, | ||
| STATE_ALARM_TRIGGERED, | ||
| STATE_ALARM_PENDING, | ||
| ATTR_CODE, | ||
| STATE_UNKNOWN, | ||
| ) | ||
| from homeassistant.core import DOMAIN as HA_DOMAIN | ||
|
|
@@ -43,6 +58,8 @@ | |
| CHALLENGE_ACK_NEEDED, | ||
| CHALLENGE_PIN_NEEDED, | ||
| CHALLENGE_FAILED_PIN_NEEDED, | ||
| ERR_ALREADY_DISARMED, | ||
| ERR_ALREADY_ARMED, | ||
| ) | ||
| from .error import SmartHomeError, ChallengeNeeded | ||
|
|
||
|
|
@@ -62,6 +79,7 @@ | |
| TRAIT_MODES = PREFIX_TRAITS + "Modes" | ||
| TRAIT_OPENCLOSE = PREFIX_TRAITS + "OpenClose" | ||
| TRAIT_VOLUME = PREFIX_TRAITS + "Volume" | ||
| TRAIT_ARMDISARM = PREFIX_TRAITS + "ArmDisarm" | ||
|
|
||
| PREFIX_COMMANDS = "action.devices.commands." | ||
| COMMAND_ONOFF = PREFIX_COMMANDS + "OnOff" | ||
|
|
@@ -85,6 +103,7 @@ | |
| COMMAND_OPENCLOSE = PREFIX_COMMANDS + "OpenClose" | ||
| COMMAND_SET_VOLUME = PREFIX_COMMANDS + "setVolume" | ||
| COMMAND_VOLUME_RELATIVE = PREFIX_COMMANDS + "volumeRelative" | ||
| COMMAND_ARMDISARM = PREFIX_COMMANDS + "ArmDisarm" | ||
|
|
||
| TRAITS = [] | ||
|
|
||
|
|
@@ -873,6 +892,103 @@ async def execute(self, command, data, params, challenge): | |
| ) | ||
|
|
||
|
|
||
| @register_trait | ||
| class ArmDisArmTrait(_Trait): | ||
| """Trait to Arm or Disarm a Security System. | ||
|
|
||
| https://developers.google.com/actions/smarthome/traits/armdisarm | ||
| """ | ||
|
|
||
| name = TRAIT_ARMDISARM | ||
| commands = [COMMAND_ARMDISARM] | ||
|
|
||
| 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_CUSTOM_BYPASS: SERVICE_ALARM_ARM_CUSTOM_BYPASS, | ||
| STATE_ALARM_TRIGGERED: SERVICE_ALARM_TRIGGER, | ||
| } | ||
|
|
||
| @staticmethod | ||
| def supported(domain, features, device_class): | ||
| """Test if state is supported.""" | ||
| return domain == alarm_control_panel.DOMAIN | ||
|
|
||
| @staticmethod | ||
| def might_2fa(domain, features, device_class): | ||
| """Return if the trait might ask for 2FA.""" | ||
| return True | ||
|
|
||
| def sync_attributes(self): | ||
| """Return ArmDisarm attributes for a sync request.""" | ||
| response = {} | ||
| levels = [] | ||
| for state in self.state_to_service: | ||
| level = { | ||
| "level_name": state, | ||
| "level_values": [ | ||
| { | ||
| "level_synonym": [ | ||
| state.replace("_", " "), | ||
| state.split("_")[ | ||
| 1 if state != STATE_ALARM_TRIGGERED else 0 | ||
| ], | ||
| ], | ||
| "lang": "en", | ||
| } | ||
| ], | ||
| } | ||
| levels.append(level) | ||
| response["availableArmLevels"] = {"levels": levels, "ordered": False} | ||
| return response | ||
|
|
||
| def query_attributes(self): | ||
| """Return ArmDisarm query attributes.""" | ||
| if "post_pending_state" in self.state.attributes: | ||
| armed_state = self.state.attributes["post_pending_state"] | ||
| else: | ||
| armed_state = self.state.state | ||
| response = {"isArmed": armed_state in self.state_to_service} | ||
| if response["isArmed"]: | ||
| response.update({"currentArmLevel": armed_state}) | ||
| return response | ||
|
|
||
| async def execute(self, command, data, params, challenge): | ||
| """Execute an ArmDisarm command.""" | ||
| if params["arm"] and not params.get("cancel"): | ||
| # _LOGGER.debug("%s - %s", self.state.state, params["armLevel"]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove |
||
| if self.state.state == params["armLevel"]: | ||
| raise SmartHomeError(ERR_ALREADY_ARMED, "System is already armed") | ||
| if self.state.attributes["code_arm_required"]: | ||
| _verify_pin_challenge(data, self.state, challenge) | ||
| service = self.state_to_service[params["armLevel"]] | ||
| # disarm the system without asking for code when | ||
| # 'cancel' arming action is received current status is pending | ||
| elif ( | ||
| params["arm"] | ||
| and params.get("cancel") | ||
| and self.state.state == STATE_ALARM_PENDING | ||
| ): | ||
| service = SERVICE_ALARM_DISARM | ||
| else: | ||
| if self.state.state == STATE_ALARM_DISARMED: | ||
| raise SmartHomeError(ERR_ALREADY_DISARMED, "System is already disarmed") | ||
| _verify_pin_challenge(data, self.state, challenge) | ||
| service = SERVICE_ALARM_DISARM | ||
|
|
||
| await self.hass.services.async_call( | ||
| alarm_control_panel.DOMAIN, | ||
| service, | ||
| { | ||
| ATTR_ENTITY_ID: self.state.entity_id, | ||
| ATTR_CODE: data.config.secure_devices_pin, | ||
| }, | ||
| blocking=True, | ||
| context=data.context, | ||
| ) | ||
|
|
||
|
|
||
| @register_trait | ||
| class FanSpeedTrait(_Trait): | ||
| """Trait to control speed of Fan. | ||
|
|
@@ -1343,7 +1459,6 @@ def _verify_pin_challenge(data, state, challenge): | |
| """Verify a pin challenge.""" | ||
| if not data.config.should_2fa(state): | ||
| return | ||
|
|
||
| if not data.config.secure_devices_pin: | ||
| raise SmartHomeError(ERR_CHALLENGE_NOT_SETUP, "Challenge is not set up") | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's going on here ? why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to generate the synonyms from the state name itself. so
armed_awayturns intoarmed awayorawayinstead of hard coding it.Do you recommend to keep it this way or just had code the synonyms?