Skip to content

Commit

Permalink
Clean up Battery Level & Battery State sensor attributes/classes (#1372)
Browse files Browse the repository at this point in the history
Fixes #1311.

## Summary
Removes duplicate attributes from both (so they do not refer to each other in attributes) and stops using `device_class` of `battery` on the state sensor.

## Screenshots
![Image](https://user-images.githubusercontent.com/74188/104554534-2d466700-55f1-11eb-8eff-2dcd55889ece.png)
![Image 2](https://user-images.githubusercontent.com/74188/104554635-549d3400-55f1-11eb-8642-4deba335d034.png)

## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#430

## Any other notes
Also stops showing the non-user-facing-anywhere-else "unique id" in the sensor detail list, and fixes an empty set of attributes (but non-nil dictionary) showing an empty section.
  • Loading branch information
zacwest committed Jan 15, 2021
1 parent 396341e commit 9f2e04d
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ class SensorDetailViewController: FormViewController {
form.removeAll()

let baseSection = Section()
baseSection <<< LabelRow {
$0.title = L10n.SettingsSensors.Detail.uniqueId
$0.value = sensor.UniqueID
}
baseSection <<< LabelRow {
$0.title = L10n.SettingsSensors.Detail.state
$0.value = sensor.StateDescription
Expand All @@ -55,7 +51,7 @@ class SensorDetailViewController: FormViewController {
form.append(Self.settingsSection(from: sensor.Settings))
}

if let attributes = sensor.Attributes {
if let attributes = sensor.Attributes, !attributes.isEmpty {
let attributesSection = Section(header: L10n.SettingsSensors.Detail.attributes, footer: nil)
let attributeRows = attributes
.sorted(by: { lhs, rhs in lhs.0 < rhs.0 })
Expand Down
7 changes: 1 addition & 6 deletions Sources/Shared/API/Webhook/Sensors/BatterySensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,17 @@ public class BatterySensor: SensorProvider {
deviceClass: .battery,
state: battery.level
)) {
$0.Attributes = [
"Battery State": battery.state.description,
"Low Power Mode": isLowPowerMode
].merging(battery.attributes, uniquingKeysWith: { a, _ in a })
$0.Attributes = battery.attributes
$0.UnitOfMeasurement = "%"
}

let stateSensor = with(WebhookSensor(
name: "\(sensorNamePrefix) State",
uniqueID: "\(sensorIDPrefix)_state",
icon: icon,
deviceClass: .battery,
state: battery.state.description
)) {
$0.Attributes = [
"Battery Level": battery.level,
"Low Power Mode": isLowPowerMode
].merging(battery.attributes, uniquingKeysWith: { a, _ in a })
}
Expand Down
7 changes: 0 additions & 7 deletions Tests/Shared/Sensors/BatterySensor.test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ class BatterySensorTests: XCTestCase {
) {
XCTAssertEqual(sensor.Name, "\(name ?? "Battery") State", file: file, line: line)
XCTAssertEqual(sensor.UniqueID, "\(uniqueId ?? "battery")_state", file: file, line: line)
XCTAssertEqual(sensor.DeviceClass, .battery, file: file, line: line)
}

func sensors(
Expand Down Expand Up @@ -328,11 +327,8 @@ class BatterySensorTests: XCTestCase {
let uState = uSensors[idx + 1]

XCTAssertLevel(uLevel, name: names.isEmpty ? nil : names[idx/2], uniqueId: uniqueIds.isEmpty ? nil : uniqueIds[idx/2], file: file, line: line)
XCTAssertEqual(uLevel.Attributes?["Battery State"] as? String, uState.State as? String, file: file, line: line)
XCTAssertEqual(uLevel.Attributes?["Low Power Mode"] as? Bool, true)

XCTAssertState(uState, name: names.isEmpty ? nil : names[idx/2], uniqueId: uniqueIds.isEmpty ? nil : uniqueIds[idx/2], file: file, line: line)
XCTAssertEqual(uState.Attributes?["Battery Level"] as? Int, uLevel.State as? Int, file: file, line: line)
XCTAssertEqual(uState.Attributes?["Low Power Mode"] as? Bool, true)

uReturn.append((level: uLevel, state: uState))
Expand Down Expand Up @@ -375,11 +371,8 @@ class BatterySensorTests: XCTestCase {
let cState = cSensors[idx + 1]

XCTAssertLevel(cLevel, name: names.isEmpty ? nil : names[idx/2], uniqueId: uniqueIds.isEmpty ? nil : uniqueIds[idx/2],file: file, line: line)
XCTAssertEqual(cLevel.Attributes?["Battery State"] as? String, cState.State as? String, file: file, line: line)
XCTAssertEqual(cLevel.Attributes?["Low Power Mode"] as? Bool, true)

XCTAssertState(cState, name: names.isEmpty ? nil : names[idx/2], uniqueId: uniqueIds.isEmpty ? nil : uniqueIds[idx/2],file: file, line: line)
XCTAssertEqual(cState.Attributes?["Battery Level"] as? Int, cLevel.State as? Int, file: file, line: line)
XCTAssertEqual(cState.Attributes?["Low Power Mode"] as? Bool, true)

cReturn.append((level: cLevel, state: cState))
Expand Down

0 comments on commit 9f2e04d

Please sign in to comment.