mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Prometheus to support input_number (#58964)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
8a8ffa1c08
commit
6af99882c5
@ -1,4 +1,5 @@
|
|||||||
"""Support for Prometheus metrics export."""
|
"""Support for Prometheus metrics export."""
|
||||||
|
from contextlib import suppress
|
||||||
import logging
|
import logging
|
||||||
import string
|
import string
|
||||||
|
|
||||||
@ -284,13 +285,24 @@ class PrometheusMetrics:
|
|||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
|
|
||||||
def _handle_input_number(self, state):
|
def _handle_input_number(self, state):
|
||||||
metric = self._metric(
|
if unit := self._unit_string(state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)):
|
||||||
"input_number_state",
|
metric = self._metric(
|
||||||
self.prometheus_cli.Gauge,
|
f"input_number_state_{unit}",
|
||||||
"State of the input number",
|
self.prometheus_cli.Gauge,
|
||||||
)
|
f"State of the input number measured in {unit}",
|
||||||
value = self.state_as_number(state)
|
)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
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):
|
def _handle_device_tracker(self, state):
|
||||||
metric = self._metric(
|
metric = self._metric(
|
||||||
|
@ -20,6 +20,7 @@ from homeassistant.const import (
|
|||||||
DEGREE,
|
DEGREE,
|
||||||
ENERGY_KILO_WATT_HOUR,
|
ENERGY_KILO_WATT_HOUR,
|
||||||
EVENT_STATE_CHANGED,
|
EVENT_STATE_CHANGED,
|
||||||
|
TEMP_CELSIUS,
|
||||||
TEMP_FAHRENHEIT,
|
TEMP_FAHRENHEIT,
|
||||||
)
|
)
|
||||||
from homeassistant.core import split_entity_id
|
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"
|
sensor5.entity_id = "sensor.sps30_pm_1um_weight_concentration"
|
||||||
await sensor5.async_update_ha_state()
|
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()
|
await hass.async_block_till_done()
|
||||||
body = await generate_latest_metrics(client)
|
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
|
'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):
|
async def test_sensor_without_unit(hass, hass_client):
|
||||||
"""Test prometheus metrics for sensors without a unit."""
|
"""Test prometheus metrics for sensors without a unit."""
|
||||||
@ -355,6 +369,11 @@ async def test_input_number(hass, hass_client):
|
|||||||
number2._attr_name = None
|
number2._attr_name = None
|
||||||
await number2.async_update_ha_state()
|
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()
|
await hass.async_block_till_done()
|
||||||
body = await generate_latest_metrics(client)
|
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
|
'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):
|
async def test_battery(hass, hass_client):
|
||||||
"""Test prometheus metrics for battery."""
|
"""Test prometheus metrics for battery."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user