From 8a3fc2e82b5b0e6f341ed40534e3a753453c03f9 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Wed, 21 Dec 2022 01:32:00 +0100 Subject: [PATCH] Fix attribute check in prometheus exporter (#84321) * fix attribute check * add test --- .../components/prometheus/__init__.py | 2 +- tests/components/prometheus/test_init.py | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 1d5e45c414e..f6bd4430623 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -396,7 +396,7 @@ class PrometheusMetrics: metric.labels(**self._labels(state)).set(value) def _handle_climate_temp(self, state, attr, metric_name, metric_description): - if temp := state.attributes.get(attr): + if (temp := state.attributes.get(attr)) is not None: if self._climate_units == UnitOfTemperature.FAHRENHEIT: temp = TemperatureConverter.convert( temp, UnitOfTemperature.FAHRENHEIT, UnitOfTemperature.CELSIUS diff --git a/tests/components/prometheus/test_init.py b/tests/components/prometheus/test_init.py index 1730e6c3f23..febbc72ec31 100644 --- a/tests/components/prometheus/test_init.py +++ b/tests/components/prometheus/test_init.py @@ -293,6 +293,12 @@ async def test_climate(client, climate_entities): 'friendly_name="Ecobee"} 24.0' in body ) + assert ( + 'climate_target_temperature_celsius{domain="climate",' + 'entity="climate.fritzdect",' + 'friendly_name="Fritz!DECT"} 0.0' in body + ) + @pytest.mark.parametrize("namespace", [""]) async def test_humidifier(client, humidifier_entities): @@ -1001,6 +1007,23 @@ async def climate_fixture(hass, registry): data["climate_2"] = climate_2 data["climate_2_attributes"] = climate_2_attributes + climate_3 = registry.async_get_or_create( + domain=climate.DOMAIN, + platform="test", + unique_id="climate_3", + unit_of_measurement=TEMP_CELSIUS, + suggested_object_id="fritzdect", + original_name="Fritz!DECT", + ) + climate_3_attributes = { + ATTR_TEMPERATURE: 0, + ATTR_CURRENT_TEMPERATURE: 22, + ATTR_HVAC_ACTION: climate.HVACAction.OFF, + } + set_state_with_entry(hass, climate_3, climate.HVACAction.OFF, climate_3_attributes) + data["climate_3"] = climate_3 + data["climate_3_attributes"] = climate_3_attributes + await hass.async_block_till_done() return data