From e180f1e30214172e0a2907d82a82fae85301328d Mon Sep 17 00:00:00 2001 From: alim4r <7687869+alim4r@users.noreply.github.com> Date: Thu, 18 Nov 2021 04:35:48 +0100 Subject: [PATCH] Add input_number state to prometheus metrics (#56507) * Add input_number to prometheus metrics * Add prometheus input_number tests * Removed unused import from test --- .../components/prometheus/__init__.py | 9 +++++++ tests/components/prometheus/test_init.py | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 7c531a292b1..e0c82061c2c 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -276,6 +276,15 @@ class PrometheusMetrics: value = self.state_as_number(state) 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) + def _handle_device_tracker(self, state): metric = self._metric( "device_tracker_state", diff --git a/tests/components/prometheus/test_init.py b/tests/components/prometheus/test_init.py index 827190a4b41..0fdbc23bb07 100644 --- a/tests/components/prometheus/test_init.py +++ b/tests/components/prometheus/test_init.py @@ -7,6 +7,7 @@ import unittest.mock as mock import pytest from homeassistant.components import climate, humidifier, sensor +from homeassistant.components.demo.number import DemoNumber from homeassistant.components.demo.sensor import DemoSensor import homeassistant.components.prometheus as prometheus from homeassistant.const import ( @@ -99,6 +100,17 @@ async def prometheus_client(hass, hass_client, namespace): sensor5.entity_id = "sensor.sps30_pm_1um_weight_concentration" await sensor5.async_update_ha_state() + number1 = DemoNumber(None, "Threshold", 5.2, None, False, 0, 10, 0.1) + number1.hass = hass + number1.entity_id = "input_number.threshold" + await number1.async_update_ha_state() + + number2 = DemoNumber(None, None, 60, None, False, 0, 100) + number2.hass = hass + number2.entity_id = "input_number.brightness" + number2._attr_name = None + await number2.async_update_ha_state() + return await hass_client() @@ -229,6 +241,18 @@ async def test_view_empty_namespace(hass, hass_client): 'friendly_name="SPS30 PM <1µm Weight concentration"} 3.7069' in body ) + assert ( + 'input_number_state{domain="input_number",' + 'entity="input_number.threshold",' + 'friendly_name="Threshold"} 5.2' in body + ) + + assert ( + 'input_number_state{domain="input_number",' + 'entity="input_number.brightness",' + 'friendly_name="None"} 60.0' in body + ) + async def test_view_default_namespace(hass, hass_client): """Test prometheus metrics view."""