Skip to content

Commit

Permalink
Add more domains for sensors widget (#3270)
Browse files Browse the repository at this point in the history
<!-- 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 -->
CC: @Penait1 
## 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. -->
  • Loading branch information
bgoncal authored Dec 12, 2024
1 parent 600937a commit 94fe0c5
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 31 deletions.
6 changes: 6 additions & 0 deletions HomeAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,8 @@
4289DDB52C85D7B8003591C2 /* SceneAppIntent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4289DDB42C85D7B8003591C2 /* SceneAppIntent.swift */; };
428CB3372CF7FC0800F1320E /* WidgetFamilySizes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 428CB3362CF7FC0800F1320E /* WidgetFamilySizes.swift */; };
428CB3382CF7FC0800F1320E /* WidgetFamilySizes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 428CB3362CF7FC0800F1320E /* WidgetFamilySizes.swift */; };
428D31A52D0B33AF0025B1D7 /* WidgetSensorsConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 428D31A42D0B33AF0025B1D7 /* WidgetSensorsConfig.swift */; };
428D31A62D0B33AF0025B1D7 /* WidgetSensorsConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 428D31A42D0B33AF0025B1D7 /* WidgetSensorsConfig.swift */; };
429106872BA9D22500D452F9 /* AudioRecorder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 429106862BA9D22500D452F9 /* AudioRecorder.swift */; };
429106892BA9D5F700D452F9 /* AssistView+Build.swift in Sources */ = {isa = PBXBuildFile; fileRef = 429106882BA9D5F700D452F9 /* AssistView+Build.swift */; };
4291068C2BA9D79500D452F9 /* AudioPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4291068B2BA9D79500D452F9 /* AudioPlayer.swift */; };
Expand Down Expand Up @@ -1995,6 +1997,7 @@
4289DDB22C85D6B3003591C2 /* IntentSceneEntity.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntentSceneEntity.swift; sourceTree = "<group>"; };
4289DDB42C85D7B8003591C2 /* SceneAppIntent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneAppIntent.swift; sourceTree = "<group>"; };
428CB3362CF7FC0800F1320E /* WidgetFamilySizes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetFamilySizes.swift; sourceTree = "<group>"; };
428D31A42D0B33AF0025B1D7 /* WidgetSensorsConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WidgetSensorsConfig.swift; sourceTree = "<group>"; };
429106862BA9D22500D452F9 /* AudioRecorder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioRecorder.swift; sourceTree = "<group>"; };
429106882BA9D5F700D452F9 /* AssistView+Build.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AssistView+Build.swift"; sourceTree = "<group>"; };
4291068B2BA9D79500D452F9 /* AudioPlayer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioPlayer.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3563,6 +3566,7 @@
children = (
3E02C0E92CA7FD2A00102131 /* WidgetSensorsAppIntent.swift */,
3E02C0EA2CA7FD2A00102131 /* WidgetSensorsAppIntentTimelineProvider.swift */,
428D31A42D0B33AF0025B1D7 /* WidgetSensorsConfig.swift */,
);
path = Sensor;
sourceTree = "<group>";
Expand Down Expand Up @@ -6717,6 +6721,7 @@
42F9589F2BB4707F00497981 /* WidgetAssistView.swift in Sources */,
403AE9122C2E2BFC00D48147 /* WidgetGaugeAppIntentTimelineProvider.swift in Sources */,
42F9589C2BB4691D00497981 /* WidgetAssistProvider.swift in Sources */,
428D31A62D0B33AF0025B1D7 /* WidgetSensorsConfig.swift in Sources */,
428CB3382CF7FC0800F1320E /* WidgetFamilySizes.swift in Sources */,
429BA2AF2C800CAB00A50996 /* SFSymbolEntity.swift in Sources */,
420461692C8F29440062E89F /* ControlLight.swift in Sources */,
Expand Down Expand Up @@ -6988,6 +6993,7 @@
42BA1BC92C8864C200A2FC36 /* OpenPageAppIntent.swift in Sources */,
42333ADD2D0B1771001E8408 /* EntityRegistryListForDisplay.swift in Sources */,
B661FB7A226C197900E541DD /* OnboardingManualURLViewController.swift in Sources */,
428D31A52D0B33AF0025B1D7 /* WidgetSensorsConfig.swift in Sources */,
119A827C252A3C4700D7000D /* NFCNDEFPayload+Additions.swift in Sources */,
42AC94A52CF872520050A62C /* TileCardStyleModifier.swift in Sources */,
42DD84162B14D7AC00936F16 /* WebViewExternalBusMessage.swift in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/AppIntents/Script/ScriptAppIntent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct IntentScriptAppEntityQuery: EntityQuery, EntityStringQuery {

private func getScriptEntities(matching string: String? = nil) -> [(Server, [IntentScriptEntity])] {
var scriptEntities: [(Server, [IntentScriptEntity])] = []
let entities = ControlEntityProvider(domain: .script).getEntities(matching: string)
let entities = ControlEntityProvider(domains: [.script]).getEntities(matching: string)

for (server, values) in entities {
scriptEntities.append((server, values.map({ entity in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct IntentSensorsAppEntityQuery: EntityQuery {

private func getSensorEntities(matching string: String? = nil) -> [(Server, [IntentSensorsAppEntity])] {
var sensorEntities: [(Server, [IntentSensorsAppEntity])] = []
let entities = ControlEntityProvider(domain: .sensor).getEntities(matching: string)
let entities = ControlEntityProvider(domains: WidgetSensorsConfig.domains).getEntities(matching: string)

for (server, values) in entities {
sensorEntities.append((server, values.map({ entity in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,8 @@ struct WidgetSensorsAppIntentTimelineProvider: AppIntentTimelineProvider {
)
}

private func suggestions() async -> [Server: [HAAppEntity]] {
await withCheckedContinuation { continuation in
var entities: [Server: [HAAppEntity]] = [:]
for server in Current.servers.all.sorted(by: { $0.info.name < $1.info.name }) {
do {
let sensors: [HAAppEntity] = try Current.database.read { db in
try HAAppEntity
.filter(Column(DatabaseTables.AppEntity.serverId.rawValue) == server.identifier.rawValue)
.filter(Column(DatabaseTables.AppEntity.domain.rawValue) == Domain.sensor.rawValue)
.fetchAll(db)
}
entities[server] = sensors
} catch {
Current.Log.error("Failed to load sensors from database: \(error.localizedDescription)")
}
}
continuation.resume(returning: entities)
}
private func suggestions() async -> [(Server, [HAAppEntity])] {
ControlEntityProvider(domains: WidgetSensorsConfig.domains).getEntities()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Foundation
import Shared

enum WidgetSensorsConfig {
static var domains: [Domain] = [.sensor, .binarySensor, .inputBoolean, .person, .lock]
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct ControlCoverValueProvider: AppIntentControlValueProvider {
try await ControlRefreshDelay.wait()
guard let serverId = configuration.entity?.serverId,
let lightId = configuration.entity?.entityId,
let state: String = try await ControlEntityProvider(domain: .cover).currentState(
let state: String = try await ControlEntityProvider(domains: [.cover]).currentState(
serverId: serverId,
entityId: lightId
) else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/Widgets/Cover/IntentCoverEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct IntentCoverAppEntityQuery: EntityQuery, EntityStringQuery {

private func getCoverEntities(matching string: String? = nil) async -> [(Server, [IntentCoverEntity])] {
var coverEntities: [(Server, [IntentCoverEntity])] = []
let entities = ControlEntityProvider(domain: .cover).getEntities(matching: string)
let entities = ControlEntityProvider(domains: [.cover]).getEntities(matching: string)

for (server, values) in entities {
coverEntities.append((server, values.map({ entity in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct ControlLightsValueProvider: AppIntentControlValueProvider {
try await ControlRefreshDelay.wait()
guard let serverId = configuration.light?.serverId,
let lightId = configuration.light?.entityId,
let state: String = try await ControlEntityProvider(domain: .light).currentState(
let state: String = try await ControlEntityProvider(domains: [.light]).currentState(
serverId: serverId,
entityId: lightId
) else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/Widgets/Light/IntentLightEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct IntentLightAppEntityQuery: EntityQuery, EntityStringQuery {

private func getLightEntities(matching string: String? = nil) -> [(Server, [IntentLightEntity])] {
var lightEntities: [(Server, [IntentLightEntity])] = []
let entities = ControlEntityProvider(domain: .light).getEntities(matching: string)
let entities = ControlEntityProvider(domains: [.light]).getEntities(matching: string)

for (server, values) in entities {
lightEntities.append((server, values.map({ entity in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct IntentSceneAppEntityQuery: EntityQuery, EntityStringQuery {

private func getSceneEntities(matching string: String? = nil) -> [(Server, [IntentSceneEntity])] {
var sceneEntities: [(Server, [IntentSceneEntity])] = []
let entities = ControlEntityProvider(domain: .scene).getEntities(matching: string)
let entities = ControlEntityProvider(domains: [.scene]).getEntities(matching: string)

for (server, values) in entities {
sceneEntities.append((server, values.map({ entity in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct ControlSwitchValueProvider: AppIntentControlValueProvider {
try await ControlRefreshDelay.wait()
guard let serverId = configuration.entity?.serverId,
let switchId = configuration.entity?.entityId,
let state = try await ControlEntityProvider(domain: .switch).currentState(
let state = try await ControlEntityProvider(domains: [.switch]).currentState(
serverId: serverId,
entityId: switchId
) else {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Extensions/Widgets/Switch/IntentSwitchEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct IntentSwitchAppEntityQuery: EntityQuery, EntityStringQuery {

private func getSwitchEntities(matching string: String? = nil) async -> [(Server, [IntentSwitchEntity])] {
var switchEntities: [(Server, [IntentSwitchEntity])] = []
let entities = ControlEntityProvider(domain: .switch).getEntities(matching: string)
let entities = ControlEntityProvider(domains: [.switch]).getEntities(matching: string)

for (server, values) in entities {
switchEntities.append((server, values.map({ entity in
Expand Down
1 change: 1 addition & 0 deletions Sources/Shared/API/Models/AppEntitiesModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ final class AppEntitiesModel: AppEntitiesModelProtocol {
Domain.light,
Domain.switch,
Domain.sensor,
Domain.binarySensor,
Domain.cover,
Domain.button,
Domain.inputBoolean,
Expand Down
8 changes: 4 additions & 4 deletions Sources/Shared/ControlEntityProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ public final class ControlEntityProvider {
case off
}

public let domain: Domain
public let domains: [Domain]

public init(domain: Domain) {
self.domain = domain
public init(domains: [Domain]) {
self.domains = domains
}

public func currentState(serverId: String, entityId: String) async throws -> String? {
Expand Down Expand Up @@ -47,7 +47,7 @@ public final class ControlEntityProvider {
var entities: [HAAppEntity] = try Current.database.read { db in
try HAAppEntity
.filter(Column(DatabaseTables.AppEntity.serverId.rawValue) == server.identifier.rawValue)
.filter(Column(DatabaseTables.AppEntity.domain.rawValue) == domain.rawValue)
.filter(domains.map(\.rawValue).contains(Column(DatabaseTables.AppEntity.domain.rawValue)))
.fetchAll(db)
}
if let string {
Expand Down

0 comments on commit 94fe0c5

Please sign in to comment.