Skip to content

Commit

Permalink
Use correct sensor precision in sensors widget (#3269)
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 -->
Pending on: #3268

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 90e5c5d commit b3b3bd6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,12 @@ struct WidgetSensorsAppIntentTimelineProvider: AppIntentTimelineProvider {
throw WidgetSensorsDataError.badResponse
}

let stateValue = (state?["state"] as? String) ?? "N/A"
var stateValue = (state?["state"] as? String) ?? "N/A"
stateValue = adjustPrecision(
serverId: server.identifier.rawValue,
entityId: sensor.entityId,
stateValue: stateValue
)
let unitOfMeasurement = (state?["attributes"] as? [String: Any])?["unit_of_measurement"] as? String

return WidgetSensorsEntry.SensorData(
Expand All @@ -135,6 +140,39 @@ struct WidgetSensorsAppIntentTimelineProvider: AppIntentTimelineProvider {
)
}

private func adjustPrecision(serverId: String, entityId: String, stateValue: String) -> String {
guard let stateValueFloat = Float(stateValue) else {
return stateValue
}
if let decimalPlacesForEntityId: Int = {
do {
return try Current.database.read { db in
try AppEntityRegistryListForDisplay
.filter(
Column(DatabaseTables.AppEntityRegistryListForDisplay.id.rawValue) == ServerEntity
.uniqueId(
serverId: serverId,
entityId: entityId
)
)
.fetchOne(db)?.registry.decimalPlaces
}
} catch {
Current.Log.error("Failed to fetch decimal places for entity ID: \(entityId)")
return nil
}
}() {
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.locale = Locale.current
numberFormatter.maximumFractionDigits = decimalPlacesForEntityId
numberFormatter.minimumFractionDigits = decimalPlacesForEntityId
return numberFormatter.string(from: NSNumber(value: stateValueFloat)) ?? stateValue
} else {
return stateValue
}
}

private func suggestions() async -> [(Server, [HAAppEntity])] {
ControlEntityProvider(domains: WidgetSensorsConfig.domains).getEntities()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Shared/EntityRegistryListForDisplay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ public struct AppEntityRegistryListForDisplay: Codable, FetchableRecord, Persist
let id: String
let serverId: String
let entityId: String
let registry: EntityRegistryListForDisplay.Entity
public let registry: EntityRegistryListForDisplay.Entity
}

0 comments on commit b3b3bd6

Please sign in to comment.