mirror of
https://github.com/home-assistant/iOS.git
synced 2026-06-24 20:17:30 -05:00
<!-- Thank you for submitting a Pull Request and helping to improve Home Assistant. Please complete the following sections to help the processing and review of your changes. Please do not delete anything from this template. --> ## Summary <!-- Provide a brief summary of the changes you have made and most importantly what they aim to achieve --> This PR adds device and area context to Watch, CarPlay, Widgets and App Icon shortcuts configuration screens. ## Screenshots <!-- If this is a user-facing change not in the frontend, please include screenshots in light and dark mode. --> ## Link to pull request in Documentation repository <!-- Pull requests that add, change or remove functionality must have a corresponding pull request in the Companion App Documentation repository (https://github.com/home-assistant/companion.home-assistant). Please add the number of this pull request after the "#" --> Documentation: home-assistant/companion.home-assistant# ## Any other notes <!-- If there is any other information of note, like if this Pull Request is part of a bigger change, please include it here. -->
106 lines
3.7 KiB
Swift
106 lines
3.7 KiB
Swift
import AppIntents
|
|
import Foundation
|
|
import GRDB
|
|
import SFSafeSymbols
|
|
import Shared
|
|
|
|
@available(iOS 18.0, *)
|
|
struct IntentSwitchEntity: AppEntity, EntityContextRepresentable {
|
|
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Switch")
|
|
|
|
static let defaultQuery = IntentSwitchAppEntityQuery()
|
|
|
|
// UniqueID: serverId-entityId
|
|
var id: String
|
|
var entityId: String
|
|
var serverId: String
|
|
var areaName: String?
|
|
var deviceName: String?
|
|
var displayString: String
|
|
var iconName: String
|
|
var displayRepresentation: DisplayRepresentation {
|
|
DisplayRepresentation(
|
|
title: "\(displayString)",
|
|
subtitle: contextSubtitle.map { LocalizedStringResource(stringLiteral: $0) }
|
|
)
|
|
}
|
|
|
|
init(
|
|
id: String,
|
|
entityId: String,
|
|
serverId: String,
|
|
areaName: String? = nil,
|
|
deviceName: String? = nil,
|
|
displayString: String,
|
|
iconName: String
|
|
) {
|
|
self.id = id
|
|
self.entityId = entityId
|
|
self.serverId = serverId
|
|
self.areaName = areaName
|
|
self.deviceName = deviceName
|
|
self.displayString = displayString
|
|
self.iconName = iconName
|
|
}
|
|
}
|
|
|
|
@available(iOS 18.0, *)
|
|
struct IntentSwitchAppEntityQuery: EntityQuery, EntityStringQuery {
|
|
#if WIDGET_EXTENSION
|
|
@IntentParameterDependency<ControlSwitchConfiguration>(\.$server)
|
|
var config
|
|
#endif
|
|
|
|
func entities(for identifiers: [String]) async throws -> [IntentSwitchEntity] {
|
|
await getSwitchEntities().flatMap(\.1).filter { identifiers.contains($0.id) }
|
|
}
|
|
|
|
func entities(matching string: String) async throws -> IntentItemCollection<IntentSwitchEntity> {
|
|
await collection(for: getSwitchEntities(matching: string))
|
|
}
|
|
|
|
func suggestedEntities() async throws -> IntentItemCollection<IntentSwitchEntity> {
|
|
await collection(for: getSwitchEntities())
|
|
}
|
|
|
|
/// Scopes the list to the server picked in the configuration (flat list). When no server is
|
|
/// selected (e.g. a widget configured before this option existed), falls back to grouping
|
|
/// every server's entities into sections.
|
|
private func collection(
|
|
for entitiesPerServer: [(Server, [IntentSwitchEntity])]
|
|
) -> IntentItemCollection<IntentSwitchEntity> {
|
|
#if WIDGET_EXTENSION
|
|
if let server = config?.server {
|
|
let items = entitiesPerServer.first { $0.0.identifier.rawValue == server.id }?.1 ?? []
|
|
return .init(items: items)
|
|
}
|
|
#endif
|
|
return .init(sections: entitiesPerServer.map { server, items in
|
|
.init(.init(stringLiteral: server.info.name), items: items)
|
|
})
|
|
}
|
|
|
|
private func getSwitchEntities(matching string: String? = nil) async -> [(Server, [IntentSwitchEntity])] {
|
|
var switchEntities: [(Server, [IntentSwitchEntity])] = []
|
|
let entities = ControlEntityProvider(domains: [.switch, .inputBoolean]).getEntities(matching: string)
|
|
|
|
for (server, values) in entities {
|
|
let deviceMap = values.devicesMap(for: server.identifier.rawValue)
|
|
let areasMap = values.areasMap(for: server.identifier.rawValue)
|
|
switchEntities.append((server, values.map({ entity in
|
|
IntentSwitchEntity(
|
|
id: entity.id,
|
|
entityId: entity.entityId,
|
|
serverId: entity.serverId,
|
|
areaName: areasMap[entity.entityId]?.name ?? "",
|
|
deviceName: deviceMap[entity.entityId]?.name ?? "",
|
|
displayString: entity.name,
|
|
iconName: entity.icon ?? SFSymbol.lightswitchOnFill.rawValue
|
|
)
|
|
})))
|
|
}
|
|
|
|
return switchEntities
|
|
}
|
|
}
|