Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Hemera/Entities/AreaDisplayHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ enum AreaDisplayHelpers {
let humidity = sensors.first { $0.deviceClass == "humidity" }

let tempStr = temp.flatMap { Double($0.state) }
.map { "\(Int($0.rounded()))\(temp?.unitOfMeasurement ?? "°")" }
.map { (value: Double) -> String in
let number = value.formatted(.number.precision(.fractionLength(1)))
return number + (temp?.unitOfMeasurement ?? "°")
}
let humStr = humidity.flatMap { Double($0.state) }
.map { "\(Int($0.rounded()))\(humidity?.unitOfMeasurement ?? "%")" }
.map { (value: Double) -> String in
let number = value.formatted(.number.precision(.fractionLength(1)))
return number + (humidity?.unitOfMeasurement ?? "%")
}

return (tempStr, humStr)
}
Expand Down
20 changes: 16 additions & 4 deletions HemeraTests/Entities/AreaDisplayHelpersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,29 @@ struct AreaDisplayHelpersTests {

let (t, h) = AreaDisplayHelpers.climateSummary(from: [temp, humidity])

#expect(t == "21°C")
#expect(h == "47%")
// Whole values still render with one decimal, using the current locale's separator.
#expect(t == 21.0.formatted(.number.precision(.fractionLength(1))) + "°C")
#expect(h == 47.0.formatted(.number.precision(.fractionLength(1))) + "%")
}

@Test
func climateSummary_roundsFractionalValues() {
func climateSummary_formatsFractionalValuesToOneDecimal() {
let temp = SensorEntity(entityId: "sensor.temp", name: "T", state: "21.6", deviceClass: "temperature", unitOfMeasurement: "°C")

let (t, _) = AreaDisplayHelpers.climateSummary(from: [temp])

#expect(t == "22°C")
// Fractional value is preserved to one decimal, not rounded to a whole number.
#expect(t == 21.6.formatted(.number.precision(.fractionLength(1))) + "°C")
}

@Test
func climateSummary_roundsToOneDecimal() {
let temp = SensorEntity(entityId: "sensor.temp", name: "T", state: "21.68", deviceClass: "temperature", unitOfMeasurement: "°C")

let (t, _) = AreaDisplayHelpers.climateSummary(from: [temp])

// More than one decimal is rounded, not truncated.
#expect(t == 21.7.formatted(.number.precision(.fractionLength(1))) + "°C")
}

@Test
Expand Down
4 changes: 2 additions & 2 deletions HemeraTests/HomeAssistant/StateUpdatePropagationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ struct StateUpdatePropagationTests {
context.insert(sensor)

let (tempBefore, _) = AreaDisplayHelpers.climateSummary(from: area.sensors)
#expect(tempBefore == "21°C")
#expect(tempBefore == 21.0.formatted(.number.precision(.fractionLength(1))) + "°C")

let haEntity = try HAEntity(
entityId: "sensor.temp",
Expand All @@ -137,6 +137,6 @@ struct StateUpdatePropagationTests {
EntityRegistry.shared.upsert(from: haEntity, in: context)

let (tempAfter, _) = AreaDisplayHelpers.climateSummary(from: area.sensors)
#expect(tempAfter == "24°C")
#expect(tempAfter == 24.0.formatted(.number.precision(.fractionLength(1))) + "°C")
}
}
Loading