diff --git a/custom_components/bms_ble/plugins/dummy_bms.py b/custom_components/bms_ble/plugins/dummy_bms.py index 192a1bc..5f0de96 100644 --- a/custom_components/bms_ble/plugins/dummy_bms.py +++ b/custom_components/bms_ble/plugins/dummy_bms.py @@ -15,7 +15,7 @@ # ATTR_DELTA_VOLTAGE, ATTR_POWER, # ATTR_RUNTIME, - # ATTR_TEMPERATURE, + ATTR_TEMPERATURE, ATTR_VOLTAGE, ) @@ -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 diff --git a/custom_components/bms_ble/sensor.py b/custom_components/bms_ble/sensor.py index fe95bed..c874c19 100644 --- a/custom_components/bms_ble/sensor.py +++ b/custom_components/bms_ble/sensor.py @@ -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 diff --git a/tests/test_sensor.py b/tests/test_sensor.py index 99d8587..4e2fdc6 100644 --- a/tests/test_sensor.py +++ b/tests/test_sensor.py @@ -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", @@ -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",