diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 9c3d3abffa5..995c65d7583 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -1,4 +1,5 @@ """Support for Prometheus metrics export.""" +from contextlib import suppress import logging import string @@ -284,13 +285,24 @@ class PrometheusMetrics: metric.labels(**self._labels(state)).set(value) def _handle_input_number(self, state): - metric = self._metric( - "input_number_state", - self.prometheus_cli.Gauge, - "State of the input number", - ) - value = self.state_as_number(state) - metric.labels(**self._labels(state)).set(value) + if unit := self._unit_string(state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)): + metric = self._metric( + f"input_number_state_{unit}", + self.prometheus_cli.Gauge, + f"State of the input number measured in {unit}", + ) + else: + metric = self._metric( + "input_number_state", + self.prometheus_cli.Gauge, + "State of the input number", + ) + + with suppress(ValueError): + value = self.state_as_number(state) + if state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == TEMP_FAHRENHEIT: + value = fahrenheit_to_celsius(value) + metric.labels(**self._labels(state)).set(value) def _handle_device_tracker(self, state): metric = self._metric( diff --git a/tests/components/prometheus/test_init.py b/tests/components/prometheus/test_init.py index 0a4e528a179..6b634701ff2 100644 --- a/tests/components/prometheus/test_init.py +++ b/tests/components/prometheus/test_init.py @@ -20,6 +20,7 @@ from homeassistant.const import ( DEGREE, ENERGY_KILO_WATT_HOUR, EVENT_STATE_CHANGED, + TEMP_CELSIUS, TEMP_FAHRENHEIT, ) from homeassistant.core import split_entity_id @@ -201,6 +202,13 @@ async def test_sensor_unit(hass, hass_client): sensor5.entity_id = "sensor.sps30_pm_1um_weight_concentration" await sensor5.async_update_ha_state() + sensor6 = DemoSensor( + None, "Target temperature", 22.7, None, None, TEMP_CELSIUS, None + ) + sensor6.hass = hass + sensor6.entity_id = "input_number.target_temperature" + await sensor6.async_update_ha_state() + await hass.async_block_till_done() body = await generate_latest_metrics(client) @@ -228,6 +236,12 @@ async def test_sensor_unit(hass, hass_client): 'friendly_name="SPS30 PM <1µm Weight concentration"} 3.7069' in body ) + assert ( + 'input_number_state_celsius{domain="input_number",' + 'entity="input_number.target_temperature",' + 'friendly_name="Target temperature"} 22.7' in body + ) + async def test_sensor_without_unit(hass, hass_client): """Test prometheus metrics for sensors without a unit.""" @@ -355,6 +369,11 @@ async def test_input_number(hass, hass_client): number2._attr_name = None await number2.async_update_ha_state() + number3 = DemoSensor(None, "Retry count", 5, None, None, None, None) + number3.hass = hass + number3.entity_id = "input_number.retry_count" + await number3.async_update_ha_state() + await hass.async_block_till_done() body = await generate_latest_metrics(client) @@ -370,6 +389,12 @@ async def test_input_number(hass, hass_client): 'friendly_name="None"} 60.0' in body ) + assert ( + 'input_number_state{domain="input_number",' + 'entity="input_number.retry_count",' + 'friendly_name="Retry count"} 5.0' in body + ) + async def test_battery(hass, hass_client): """Test prometheus metrics for battery."""