mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Handle attributes set to None in prometheus (#104247)
Better handle attributes set to None
This commit is contained in:
parent
58a73f7723
commit
80f8e76fa3
@ -19,6 +19,7 @@ from homeassistant.components.climate import (
|
|||||||
from homeassistant.components.cover import ATTR_POSITION, ATTR_TILT_POSITION
|
from homeassistant.components.cover import ATTR_POSITION, ATTR_TILT_POSITION
|
||||||
from homeassistant.components.http import HomeAssistantView
|
from homeassistant.components.http import HomeAssistantView
|
||||||
from homeassistant.components.humidifier import ATTR_AVAILABLE_MODES, ATTR_HUMIDITY
|
from homeassistant.components.humidifier import ATTR_AVAILABLE_MODES, ATTR_HUMIDITY
|
||||||
|
from homeassistant.components.light import ATTR_BRIGHTNESS
|
||||||
from homeassistant.components.sensor import SensorDeviceClass
|
from homeassistant.components.sensor import SensorDeviceClass
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_BATTERY_LEVEL,
|
ATTR_BATTERY_LEVEL,
|
||||||
@ -323,14 +324,14 @@ class PrometheusMetrics:
|
|||||||
}
|
}
|
||||||
|
|
||||||
def _battery(self, state):
|
def _battery(self, state):
|
||||||
if "battery_level" in state.attributes:
|
if (battery_level := state.attributes.get(ATTR_BATTERY_LEVEL)) is not None:
|
||||||
metric = self._metric(
|
metric = self._metric(
|
||||||
"battery_level_percent",
|
"battery_level_percent",
|
||||||
self.prometheus_cli.Gauge,
|
self.prometheus_cli.Gauge,
|
||||||
"Battery level as a percentage of its capacity",
|
"Battery level as a percentage of its capacity",
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
value = float(state.attributes[ATTR_BATTERY_LEVEL])
|
value = float(battery_level)
|
||||||
metric.labels(**self._labels(state)).set(value)
|
metric.labels(**self._labels(state)).set(value)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
@ -440,8 +441,9 @@ class PrometheusMetrics:
|
|||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if "brightness" in state.attributes and state.state == STATE_ON:
|
brightness = state.attributes.get(ATTR_BRIGHTNESS)
|
||||||
value = state.attributes["brightness"] / 255.0
|
if state.state == STATE_ON and brightness is not None:
|
||||||
|
value = brightness / 255.0
|
||||||
else:
|
else:
|
||||||
value = self.state_as_number(state)
|
value = self.state_as_number(state)
|
||||||
value = value * 100
|
value = value * 100
|
||||||
|
@ -491,6 +491,12 @@ async def test_light(client, light_entities) -> None:
|
|||||||
'friendly_name="PC"} 70.58823529411765' in body
|
'friendly_name="PC"} 70.58823529411765' in body
|
||||||
)
|
)
|
||||||
|
|
||||||
|
assert (
|
||||||
|
'light_brightness_percent{domain="light",'
|
||||||
|
'entity="light.hallway",'
|
||||||
|
'friendly_name="Hallway"} 100.0' in body
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("namespace", [""])
|
@pytest.mark.parametrize("namespace", [""])
|
||||||
async def test_lock(client, lock_entities) -> None:
|
async def test_lock(client, lock_entities) -> None:
|
||||||
@ -1557,6 +1563,19 @@ async def light_fixture(
|
|||||||
data["light_4"] = light_4
|
data["light_4"] = light_4
|
||||||
data["light_4_attributes"] = light_4_attributes
|
data["light_4_attributes"] = light_4_attributes
|
||||||
|
|
||||||
|
light_5 = entity_registry.async_get_or_create(
|
||||||
|
domain=light.DOMAIN,
|
||||||
|
platform="test",
|
||||||
|
unique_id="light_5",
|
||||||
|
suggested_object_id="hallway",
|
||||||
|
original_name="Hallway",
|
||||||
|
)
|
||||||
|
# Light is on, but brightness is unset; expect metrics to report
|
||||||
|
# brightness of 100%.
|
||||||
|
light_5_attributes = {light.ATTR_BRIGHTNESS: None}
|
||||||
|
set_state_with_entry(hass, light_5, STATE_ON, light_5_attributes)
|
||||||
|
data["light_5"] = light_5
|
||||||
|
data["light_5_attributes"] = light_5_attributes
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user