Skip to content

Commit

Permalink
add temperature attribute with single sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
patman15 committed Dec 22, 2024
1 parent cacbc61 commit 9cfa829
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion custom_components/bms_ble/plugins/dummy_bms.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# ATTR_DELTA_VOLTAGE,
ATTR_POWER,
# ATTR_RUNTIME,
# ATTR_TEMPERATURE,
ATTR_TEMPERATURE,
ATTR_VOLTAGE,
)

Expand Down Expand Up @@ -80,4 +80,5 @@ async def _async_update(self) -> BMSsample:
return {
ATTR_VOLTAGE: 12,
ATTR_CURRENT: 1.5,
ATTR_TEMPERATURE: 27.182,
} # fixed values, replace parsed data
17 changes: 10 additions & 7 deletions custom_components/bms_ble/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,16 @@ def extra_state_attributes(self) -> dict[str, list[float]] | None: # type: igno
}
# add individual temperature values to temperature sensor
if self.entity_description.key == ATTR_TEMPERATURE:
return {
ATTR_TEMP_SENSORS: [
v
for k, v in self.coordinator.data.items()
if k.startswith(KEY_TEMP_VALUE)
]
}
temp_sensors: Final = [
v
for k, v in self.coordinator.data.items()
if k.startswith(KEY_TEMP_VALUE)
]
if temp_sensors:
return {ATTR_TEMP_SENSORS: temp_sensors}
if temp := self.coordinator.data.get(ATTR_TEMPERATURE):
return {ATTR_TEMP_SENSORS: [temp]}

return None

@property
Expand Down
8 changes: 7 additions & 1 deletion tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async def patch_async_update(_self):
assert data == {
f"sensor.smartbat_b12345_{ATTR_VOLTAGE}": "12",
"sensor.smartbat_b12345_battery": "unknown",
f"sensor.smartbat_b12345_{ATTR_TEMPERATURE}": "unknown",
f"sensor.smartbat_b12345_{ATTR_TEMPERATURE}": "27.182",
f"sensor.smartbat_b12345_{ATTR_CURRENT}": "1.5",
"sensor.smartbat_b12345_stored_energy": "unknown",
f"sensor.smartbat_b12345_{ATTR_CYCLES}": "unknown",
Expand All @@ -74,6 +74,12 @@ async def patch_async_update(_self):
"sensor.smartbat_b12345_signal_strength": "-127",
f"sensor.smartbat_b12345_{ATTR_RUNTIME}": "unknown",
}
# check temperature sensor has individual sensors as attribute array
temp_state = hass.states.get(f"sensor.smartbat_b12345_{ATTR_TEMPERATURE}")
assert temp_state is not None and temp_state.attributes[
ATTR_TEMP_SENSORS
] == [27.182]


monkeypatch.setattr(
"custom_components.bms_ble.plugins.dummy_bms.BMS.async_update",
Expand Down

0 comments on commit 9cfa829

Please sign in to comment.