Skip to content

Commit b7101dd

Browse files
authored
Add "search devices", "search commands" and "Assist" to App gestures (home-assistant#3592)
1 parent 3eca615 commit b7101dd

File tree

6 files changed

+146
-8
lines changed

6 files changed

+146
-8
lines changed

Sources/App/Resources/en.lproj/Localizable.strings

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,17 @@
269269
"gestures.swipe_right.title" = "Swipe Right";
270270
"gestures.value.option.back_page" = "Back to previous page";
271271
"gestures.value.option.more_info.search_entities" = "Search entities";
272+
"gestures.value.option.more_info.search_devices" = "Search devices";
273+
"gestures.value.option.more_info.search_commands" = "Search commands";
272274
"gestures.value.option.next_page" = "Go to next page";
273275
"gestures.value.option.next_server" = "Next server";
274276
"gestures.value.option.none" = "None";
275277
"gestures.value.option.open_debug" = "Open debug";
276278
"gestures.value.option.previous_server" = "Previous server";
277279
"gestures.value.option.search_entities" = "Search entities";
280+
"gestures.value.option.search_devices" = "Search devices";
281+
"gestures.value.option.search_commands" = "Search commands";
282+
"gestures.value.option.assist" = "Open Assist";
278283
"gestures.value.option.servers_list" = "Servers list";
279284
"gestures.value.option.show_settings" = "Open App settings";
280285
"gestures.value.option.show_sidebar" = "Show sidebar";
@@ -1235,4 +1240,4 @@ Home Assistant is free and open source home automation software with a focus on
12351240
"widgets.sensors.description" = "Display state of sensors";
12361241
"widgets.sensors.not_configured" = "No Sensors Configured";
12371242
"widgets.sensors.title" = "Sensors";
1238-
"yes_label" = "Yes";
1243+
"yes_label" = "Yes";

Sources/App/WebView/WebViewController+Gestures.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import Shared
77
extension WebViewController {
88
func handleGestureAction(_ action: HAGestureAction) {
99
switch action {
10+
case .assist:
11+
showAssistThroughKeyEvent()
1012
case .showSidebar:
1113
showSidebar()
1214
case .backPage:
@@ -25,6 +27,10 @@ extension WebViewController {
2527
openDebug()
2628
case .searchEntities:
2729
showSearchEntities()
30+
case .searchDevices:
31+
showSearchDevices()
32+
case .searchCommands:
33+
showSearchCommands()
2834
case .none:
2935
/* no-op */
3036
break
@@ -66,6 +72,36 @@ extension WebViewController {
6672
}
6773
}
6874

75+
private func showSearchDevices() {
76+
webView.evaluateJavaScript(WebViewJavascriptCommands.searchDevicesKeyEvent) { _, error in
77+
if let error {
78+
Current.Log.error("JavaScript error while trying to open devices search: \(error)")
79+
} else {
80+
Current.Log.info("Open devices search command sent to webview")
81+
}
82+
}
83+
}
84+
85+
private func showSearchCommands() {
86+
webView.evaluateJavaScript(WebViewJavascriptCommands.searchCommandsKeyEvent) { _, error in
87+
if let error {
88+
Current.Log.error("JavaScript error while trying to open commands search: \(error)")
89+
} else {
90+
Current.Log.info("Open commands search command sent to webview")
91+
}
92+
}
93+
}
94+
95+
private func showAssistThroughKeyEvent() {
96+
webView.evaluateJavaScript(WebViewJavascriptCommands.assistKeyEvent) { _, error in
97+
if let error {
98+
Current.Log.error("JavaScript error while trying to open assist: \(error)")
99+
} else {
100+
Current.Log.info("Open assist command sent to webview")
101+
}
102+
}
103+
}
104+
69105
private func moveToServer(next: Bool) {
70106
let servers = Current.servers.all
71107
guard servers.count > 1, let currentIndex = servers.firstIndex(of: server) else { return }

Sources/App/WebView/WebViewJavascriptCommands.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,40 @@ enum WebViewJavascriptCommands {
1212
});
1313
document.dispatchEvent(event);
1414
"""
15+
16+
static var searchDevicesKeyEvent = """
17+
var event = new KeyboardEvent('keydown', {
18+
key: 'd',
19+
code: 'KeyD',
20+
keyCode: 68,
21+
which: 68,
22+
bubbles: true,
23+
cancelable: true
24+
});
25+
document.dispatchEvent(event);
26+
"""
27+
28+
static var searchCommandsKeyEvent = """
29+
var event = new KeyboardEvent('keydown', {
30+
key: 'c',
31+
code: 'KeyC',
32+
keyCode: 67,
33+
which: 67,
34+
bubbles: true,
35+
cancelable: true
36+
});
37+
document.dispatchEvent(event);
38+
"""
39+
40+
static var assistKeyEvent = """
41+
var event = new KeyboardEvent('keydown', {
42+
key: 'a',
43+
code: 'KeyA',
44+
keyCode: 65,
45+
which: 65,
46+
bubbles: true,
47+
cancelable: true
48+
});
49+
document.dispatchEvent(event);
50+
"""
1551
}

Sources/Shared/AppGesture.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public enum HAGestureAction: String, Codable, CaseIterable {
2828
// Home Assistant
2929
case showSidebar
3030
case searchEntities
31+
case searchDevices
32+
case searchCommands
33+
case assist
3134
// Page
3235
case backPage
3336
case nextPage
@@ -43,7 +46,7 @@ public enum HAGestureAction: String, Codable, CaseIterable {
4346

4447
public var category: HAGestureActionCategory {
4548
switch self {
46-
case .showSidebar, .searchEntities:
49+
case .showSidebar, .searchEntities, .searchDevices, .searchCommands, .assist:
4750
.homeAssistant
4851
case .backPage, .nextPage:
4952
.page
@@ -78,6 +81,12 @@ public enum HAGestureAction: String, Codable, CaseIterable {
7881
L10n.Gestures.Value.Option.none
7982
case .openDebug:
8083
L10n.Gestures.Value.Option.openDebug
84+
case .searchDevices:
85+
L10n.Gestures.Value.Option.searchDevices
86+
case .searchCommands:
87+
L10n.Gestures.Value.Option.searchCommands
88+
case .assist:
89+
L10n.Gestures.Value.Option.assist
8190
}
8291
}
8392

@@ -91,6 +100,10 @@ public enum HAGestureAction: String, Codable, CaseIterable {
91100
nil
92101
case .searchEntities:
93102
L10n.Gestures.Value.Option.MoreInfo.searchEntities
103+
case .searchCommands:
104+
L10n.Gestures.Value.Option.MoreInfo.searchCommands
105+
case .searchDevices:
106+
L10n.Gestures.Value.Option.MoreInfo.searchDevices
94107
case .showServersList:
95108
nil
96109
case .nextServer:
@@ -103,6 +116,8 @@ public enum HAGestureAction: String, Codable, CaseIterable {
103116
nil
104117
case .openDebug:
105118
nil
119+
case .assist:
120+
nil
106121
}
107122
}
108123
}
@@ -195,10 +210,6 @@ public extension [AppGesture: HAGestureAction] {
195210
switch numberOfTouches {
196211
case 1:
197212
return .none
198-
// case 2:
199-
// return Current.settingsStore.gestures[._2FingersSwipeDown] ?? .none
200-
// case 3:
201-
// return Current.settingsStore.gestures[._3FingersSwipeDown] ?? .none
202213
default:
203214
return .none
204215
}
@@ -229,8 +240,6 @@ public extension [AppGesture: HAGestureAction] {
229240
switch numberOfTouches {
230241
case 1:
231242
return .none
232-
// case 2:
233-
// return Current.settingsStore.gestures[._2FingersSwipeUp] ?? .none
234243
case 3:
235244
return Current.settingsStore.gestures[._3FingersSwipeUp] ?? .none
236245
default:

Sources/Shared/Resources/Swiftgen/Strings.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,8 @@ public enum L10n {
10761076
}
10771077
public enum Value {
10781078
public enum Option {
1079+
/// Open Assist
1080+
public static var assist: String { return L10n.tr("Localizable", "gestures.value.option.assist") }
10791081
/// Back to previous page
10801082
public static var backPage: String { return L10n.tr("Localizable", "gestures.value.option.back_page") }
10811083
/// Go to next page
@@ -1088,6 +1090,10 @@ public enum L10n {
10881090
public static var openDebug: String { return L10n.tr("Localizable", "gestures.value.option.open_debug") }
10891091
/// Previous server
10901092
public static var previousServer: String { return L10n.tr("Localizable", "gestures.value.option.previous_server") }
1093+
/// Search commands
1094+
public static var searchCommands: String { return L10n.tr("Localizable", "gestures.value.option.search_commands") }
1095+
/// Search devices
1096+
public static var searchDevices: String { return L10n.tr("Localizable", "gestures.value.option.search_devices") }
10911097
/// Search entities
10921098
public static var searchEntities: String { return L10n.tr("Localizable", "gestures.value.option.search_entities") }
10931099
/// Servers list
@@ -1097,6 +1103,10 @@ public enum L10n {
10971103
/// Show sidebar
10981104
public static var showSidebar: String { return L10n.tr("Localizable", "gestures.value.option.show_sidebar") }
10991105
public enum MoreInfo {
1106+
/// Search commands
1107+
public static var searchCommands: String { return L10n.tr("Localizable", "gestures.value.option.more_info.search_commands") }
1108+
/// Search devices
1109+
public static var searchDevices: String { return L10n.tr("Localizable", "gestures.value.option.more_info.search_devices") }
11001110
/// Search entities
11011111
public static var searchEntities: String { return L10n.tr("Localizable", "gestures.value.option.more_info.search_entities") }
11021112
}

Tests/App/WebView/WebViewJavascriptCommandsTests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,46 @@ struct WebViewJavascriptCommandsTests {
2222
document.dispatchEvent(event);
2323
""")
2424
}
25+
26+
@Test func testWebViewJavascriptCommandsSearchDevices() async throws {
27+
assert(WebViewJavascriptCommands.searchDevicesKeyEvent == """
28+
var event = new KeyboardEvent('keydown', {
29+
key: 'd',
30+
code: 'KeyD',
31+
keyCode: 68,
32+
which: 68,
33+
bubbles: true,
34+
cancelable: true
35+
});
36+
document.dispatchEvent(event);
37+
""")
38+
}
39+
40+
@Test func testWebViewJavascriptCommandsSearchCommands() async throws {
41+
assert(WebViewJavascriptCommands.searchCommandsKeyEvent == """
42+
var event = new KeyboardEvent('keydown', {
43+
key: 'c',
44+
code: 'KeyC',
45+
keyCode: 67,
46+
which: 67,
47+
bubbles: true,
48+
cancelable: true
49+
});
50+
document.dispatchEvent(event);
51+
""")
52+
}
53+
54+
@Test func testWebViewJavascriptCommandsAssist() async throws {
55+
assert(WebViewJavascriptCommands.assistKeyEvent == """
56+
var event = new KeyboardEvent('keydown', {
57+
key: 'a',
58+
code: 'KeyA',
59+
keyCode: 65,
60+
which: 65,
61+
bubbles: true,
62+
cancelable: true
63+
});
64+
document.dispatchEvent(event);
65+
""")
66+
}
2567
}

0 commit comments

Comments
 (0)