-
-
Notifications
You must be signed in to change notification settings - Fork 957
Makes alarm_control_panels more flexible on Android auto #6374
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
Open
poupounetjoyeux
wants to merge
7
commits into
home-assistant:main
Choose a base branch
from
poupounetjoyeux:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
27320f7
Makes alarm_control_panels more flexible on Android auto
poupounetjoyeux 986625c
Merge branch 'main' into main
poupounetjoyeux 197c724
Fix lint errors and add testing
poupounetjoyeux 1d31803
Merge branch 'main' into main
poupounetjoyeux f7b4041
Address remarks
poupounetjoyeux 933c834
Address remarks
poupounetjoyeux b976a13
Address remarks
poupounetjoyeux 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
74 changes: 74 additions & 0 deletions
74
.../io/homeassistant/companion/android/common/data/integration/AlarmControlPanelEntityExt.kt
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 |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package io.homeassistant.companion.android.common.data.integration | ||
|
|
||
| import androidx.annotation.VisibleForTesting | ||
| import timber.log.Timber | ||
| import io.homeassistant.companion.android.common.data.integration.IntegrationDomains.ALARM_CONTROL_PANEL_DOMAIN | ||
|
|
||
| @VisibleForTesting | ||
| internal const val ALARM_CONTROL_PANEL_SUPPORT_ARM_AWAY = 2 | ||
|
|
||
| private fun Entity.isAlarmControlPanelEntity(): Boolean { | ||
| return domain == ALARM_CONTROL_PANEL_DOMAIN | ||
| } | ||
|
|
||
| private fun Entity.alarmHasNoCode(): Boolean { | ||
| return isAlarmControlPanelEntity() && (attributes["code_format"] as? String)?.isNotEmpty() != true | ||
| } | ||
|
|
||
| private fun Entity.alarmCanBeArmedWithoutCode(): Boolean { | ||
| return isAlarmControlPanelEntity() && attributes["code_arm_required"] as? Boolean == false | ||
| } | ||
|
|
||
| private fun Entity.supportsAlarmControlPanelArmAway(): Boolean { | ||
| return try { | ||
| if (!isAlarmControlPanelEntity()) { | ||
| return false | ||
| } | ||
|
|
||
| (attributes["supported_features"] as Number).toInt() and | ||
| ALARM_CONTROL_PANEL_SUPPORT_ARM_AWAY == ALARM_CONTROL_PANEL_SUPPORT_ARM_AWAY | ||
| } catch (e: Exception) { | ||
| Timber.tag(EntityExt.TAG).e(e, "Unable to get supportsArmedAway") | ||
| false | ||
| } | ||
| } | ||
|
|
||
| private fun Entity.alarmIsDisarmed(): Boolean { | ||
| return isAlarmControlPanelEntity() && state == "disarmed" | ||
| } | ||
|
|
||
| private fun Entity.alarmCanBeArmedAway(): Boolean { | ||
| if(!isAlarmControlPanelEntity()) { | ||
| return false | ||
| } | ||
|
|
||
| if (!alarmIsDisarmed() || !supportsAlarmControlPanelArmAway()) { | ||
| return false | ||
| } | ||
|
|
||
| return alarmHasNoCode() || alarmCanBeArmedWithoutCode() | ||
| } | ||
|
|
||
| private fun Entity.alarmCanBeDisarmed(): Boolean { | ||
| return isAlarmControlPanelEntity() && !alarmIsDisarmed() && alarmHasNoCode() | ||
| } | ||
|
|
||
| fun Entity.isAlarmActionable(): Boolean { | ||
| return isAlarmControlPanelEntity() && alarmCanBeDisarmed() || alarmCanBeArmedAway() | ||
| } | ||
|
|
||
| fun Entity.getAlarmOnPressedAction(): String? { | ||
| if(!isAlarmControlPanelEntity()) { | ||
| return null | ||
| } | ||
|
|
||
| if (alarmCanBeDisarmed()) { | ||
| return "alarm_disarm" | ||
| } | ||
|
|
||
| if (alarmCanBeArmedAway()) { | ||
| return "alarm_arm_away" | ||
| } | ||
|
|
||
| return null | ||
| } | ||
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
75 changes: 75 additions & 0 deletions
75
...homeassistant/companion/android/common/data/integration/AlarmControlPanelEntityExtTest.kt
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 |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package io.homeassistant.companion.android.common.data.integration | ||
|
|
||
| import java.time.LocalDateTime | ||
| import org.junit.jupiter.api.Test | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertFalse | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
|
|
||
| class AlarmControlPanelEntityExtTest { | ||
|
poupounetjoyeux marked this conversation as resolved.
|
||
|
|
||
| @Test | ||
| fun `Given disarmed alarm without code supporting arm_away When pressed Then alarm is actionable and action is alarm_arm_away`() { | ||
| val alarmEntity = createAlarmEntity("", requiredArmCode = false, supportArmAway = true, isArmed = false) | ||
|
|
||
| assertTrue(alarmEntity.isAlarmActionable()) | ||
| assertEquals("alarm_arm_away", alarmEntity.getAlarmOnPressedAction()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given disarmed alarm with not required arm code supporting arm_away When pressed Then alarm is actionable and action is alarm_arm_away`() { | ||
| val alarmEntity = createAlarmEntity("A_C0DE", requiredArmCode = false, supportArmAway = true, isArmed = false) | ||
|
|
||
| assertTrue(alarmEntity.isAlarmActionable()) | ||
| assertEquals("alarm_arm_away", alarmEntity.getAlarmOnPressedAction()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given armed alarm without code When pressed Then alarm is actionable and action is alarm_disarm`() { | ||
| val alarmEntity = createAlarmEntity("", requiredArmCode = false, supportArmAway = false, isArmed = true) | ||
|
|
||
| assertTrue(alarmEntity.isAlarmActionable()) | ||
| assertEquals("alarm_disarm", alarmEntity.getAlarmOnPressedAction()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given disarmed alarm without code not supporting arm_away When pressed Then alarm is not actionable and action is null`() { | ||
| val alarmEntity = createAlarmEntity("", requiredArmCode = false, supportArmAway = false, isArmed = false) | ||
|
|
||
| assertFalse(alarmEntity.isAlarmActionable()) | ||
| assertEquals(null, alarmEntity.getAlarmOnPressedAction()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given disarmed alarm with required arm code supporting arm_away When pressed Then alarm is not actionable and action is null`() { | ||
| val alarmEntity = createAlarmEntity("A_C0DE", requiredArmCode = true, supportArmAway = true, isArmed = false) | ||
|
|
||
| assertFalse(alarmEntity.isAlarmActionable()) | ||
| assertEquals(null, alarmEntity.getAlarmOnPressedAction()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given armed alarm with code When pressed Then alarm is not actionable and action is null`() { | ||
| val alarmEntity = createAlarmEntity("A_C0DE", requiredArmCode = false, supportArmAway = true, isArmed = true) | ||
|
|
||
| assertFalse(alarmEntity.isAlarmActionable()) | ||
| assertEquals(null, alarmEntity.getAlarmOnPressedAction()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Given not an alarm entity When pressed Then alarm is not actionable and action is null`() { | ||
| val otherEntity = Entity("other_domain.an_entity_id", "", mapOf(), LocalDateTime.now(), LocalDateTime.now()) | ||
| assertFalse(otherEntity.isAlarmActionable()) | ||
| assertEquals(null, otherEntity.getAlarmOnPressedAction()) | ||
| } | ||
|
|
||
| private fun createAlarmEntity(code: String, requiredArmCode: Boolean, supportArmAway: Boolean, isArmed: Boolean): Entity { | ||
| val state = if (isArmed) "armed_away" else "disarmed" | ||
|
|
||
| val attributes = mutableMapOf<String, Any?>() | ||
| attributes["code_format"] = if (code.isEmpty()) null else "text" | ||
| attributes["code_arm_required"] = if (code.isEmpty()) false else requiredArmCode | ||
| attributes["supported_features"] = if (supportArmAway) ALARM_CONTROL_PANEL_SUPPORT_ARM_AWAY + 4 else 4 | ||
| return Entity("alarm_control_panel.an_alarm_id", state, attributes, LocalDateTime.now(), LocalDateTime.now()) | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.