Skip to content

Commit 1bca68e

Browse files
Copilotbgoncal
andcommitted
Add deviceClass support to ControlEntityProvider for proper widget icons
Co-authored-by: bgoncal <5808343+bgoncal@users.noreply.github.com>
1 parent ae53c00 commit 1bca68e

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

Sources/Extensions/AppIntents/Widget/Sensor/WidgetSensorsAppIntentTimelineProvider.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,15 @@ struct WidgetSensorsAppIntentTimelineProvider: AppIntentTimelineProvider {
6060
func placeholder(in context: Context) -> WidgetSensorsEntry {
6161
let count = WidgetFamilySizes.size(for: context.family)
6262
let sensors = stride(from: 0, to: count, by: 1).map { index in
63-
WidgetSensorsEntry.SensorData(id: String(index), key: "?", value: "?")
63+
WidgetSensorsEntry.SensorData(
64+
id: String(index),
65+
key: "?",
66+
value: "?",
67+
unitOfMeasurement: nil,
68+
icon: nil,
69+
deviceClass: nil,
70+
domainState: nil
71+
)
6472
}
6573

6674
return WidgetSensorsEntry(
@@ -90,13 +98,15 @@ struct WidgetSensorsAppIntentTimelineProvider: AppIntentTimelineProvider {
9098
let state: ControlEntityProvider.State = await ControlEntityProvider(domains: Domain.allCases).state(
9199
server: server,
92100
entityId: sensor.entityId
93-
) ?? ControlEntityProvider.State(value: "", unitOfMeasurement: nil, domainState: nil)
101+
) ?? ControlEntityProvider.State(value: "", unitOfMeasurement: nil, domainState: nil, deviceClass: nil)
94102
return WidgetSensorsEntry.SensorData(
95103
id: sensor.id,
96104
key: sensor.displayString,
97105
value: state.value,
98106
unitOfMeasurement: state.unitOfMeasurement,
99-
icon: sensor.icon ?? Domain(entityId: sensor.entityId)?.icon().name
107+
icon: sensor.icon ?? Domain(entityId: sensor.entityId)?.icon(deviceClass: state.deviceClass).name,
108+
deviceClass: state.deviceClass,
109+
domainState: state.domainState
100110
)
101111
}
102112

@@ -127,6 +137,8 @@ struct WidgetSensorsEntry: TimelineEntry {
127137
var value: String
128138
var unitOfMeasurement: String?
129139
var icon: String?
140+
var deviceClass: String?
141+
var domainState: Domain.State?
130142
}
131143
}
132144

Sources/Extensions/Widgets/Custom/WidgetCustom.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ struct WidgetCustom: Widget {
7070
}
7171

7272
let icon: MaterialDesignIcons = {
73-
if let info {
73+
// When showing states and we have device class info, use dynamic icon based on domain, device class, and state
74+
if showStates, let domain = magicItem.domain, let state = state {
75+
return domain.icon(deviceClass: state.deviceClass, state: state.domainState)
76+
} else if let info {
7477
return magicItem.icon(info: info)
7578
} else {
7679
return .gridIcon

Sources/Extensions/Widgets/Custom/WidgetCustomTimelineProvider.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct WidgetCustomEntry: TimelineEntry {
1414
struct ItemState: Codable {
1515
let value: String
1616
let domainState: Domain.State?
17+
let deviceClass: String?
1718
}
1819
}
1920

@@ -157,7 +158,8 @@ struct WidgetCustomTimelineProvider: AppIntentTimelineProvider {
157158
states[item] =
158159
.init(
159160
value: "\(StatePrecision.adjustPrecision(serverId: serverId, entityId: entityId, stateValue: state.value)) \(state.unitOfMeasurement ?? "")",
160-
domainState: state.domainState
161+
domainState: state.domainState,
162+
deviceClass: state.deviceClass
161163
)
162164
} else {
163165
Current.Log

Sources/Shared/ControlEntityProvider.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ public final class ControlEntityProvider {
1616
public let value: String
1717
public let unitOfMeasurement: String?
1818
public let domainState: Domain.State?
19+
public let deviceClass: String?
1920

20-
public init(value: String, unitOfMeasurement: String?, domainState: Domain.State?) {
21+
public init(value: String, unitOfMeasurement: String?, domainState: Domain.State?, deviceClass: String? = nil) {
2122
self.value = value
2223
self.unitOfMeasurement = unitOfMeasurement
2324
self.domainState = domainState
25+
self.deviceClass = deviceClass
2426
}
2527
}
2628

@@ -157,11 +159,13 @@ public final class ControlEntityProvider {
157159
stateValue: stateValue
158160
)
159161
let unitOfMeasurement = (state?["attributes"] as? [String: Any])?["unit_of_measurement"] as? String
162+
let deviceClass = (state?["attributes"] as? [String: Any])?["device_class"] as? String
160163
stateValue = stateValue.capitalizedFirst
161164
return .init(
162165
value: stateValue,
163166
unitOfMeasurement: unitOfMeasurement,
164-
domainState: .init(rawValue: stateValue.trimmingCharacters(in: .whitespacesAndNewlines).lowercased())
167+
domainState: .init(rawValue: stateValue.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()),
168+
deviceClass: deviceClass
165169
)
166170
}
167171
}

0 commit comments

Comments
 (0)