Files
iOS/Sources/Extensions/AppIntents/Action/IntentActionAppEntity.swift
Bruno Pantaleão Gonçalves c65519658e Add "Run Script" AppIntent and Widget (#2900)
<!-- 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 -->
Pending:
- Allow color customisation
- Choose to receive or not notification confirmation

## 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. -->
2024-08-06 16:24:08 +02:00

41 lines
1.4 KiB
Swift

import AppIntents
import Foundation
import Shared
@available(iOS 16.0, macOS 13.0, watchOS 9.0, tvOS 16.0, *)
struct IntentActionAppEntity: AppEntity {
static let typeDisplayRepresentation = TypeDisplayRepresentation(name: "Action")
struct IntentActionAppEntityQuery: EntityQuery, EntityStringQuery {
func entities(for identifiers: [IntentActionAppEntity.ID]) async throws -> [IntentActionAppEntity] {
getActionEntities().filter { identifiers.contains($0.id) }
}
func entities(matching string: String) async throws -> [IntentActionAppEntity] {
getActionEntities().filter { $0.displayString.contains(string) }
}
func suggestedEntities() async throws -> [IntentActionAppEntity] {
getActionEntities()
}
private func getActionEntities() -> [IntentActionAppEntity] {
let actions = Current.realm().objects(Action.self).sorted(byKeyPath: #keyPath(Action.Position))
return Array(actions.map { IntentActionAppEntity(id: $0.ID, displayString: $0.Name) })
}
}
static let defaultQuery = IntentActionAppEntityQuery()
var id: String
var displayString: String
var displayRepresentation: DisplayRepresentation {
DisplayRepresentation(title: "\(displayString)")
}
init(id: String, displayString: String) {
self.id = id
self.displayString = displayString
}
}