Add test for prometheus export of entities becoming unavailable and available again (#112157)

Add test for state change to unavailable and back
pull/115914/head
Adam Goode 2024-04-20 23:20:01 -04:00 committed by GitHub
parent 30a60fd38b
commit 27bccf0b24
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 121 additions and 0 deletions

View File

@ -57,6 +57,7 @@ from homeassistant.const import (
STATE_ON,
STATE_OPEN,
STATE_OPENING,
STATE_UNAVAILABLE,
STATE_UNLOCKED,
UnitOfEnergy,
UnitOfTemperature,
@ -1053,6 +1054,126 @@ async def test_disabling_entity(
)
@pytest.mark.parametrize("namespace", [""])
async def test_entity_becomes_unavailable_with_export(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
client: ClientSessionGenerator,
sensor_entities: dict[str, er.RegistryEntry],
) -> None:
"""Test an entity that becomes unavailable is still exported."""
data = {**sensor_entities}
await hass.async_block_till_done()
body = await generate_latest_metrics(client)
assert (
'sensor_temperature_celsius{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 15.6' in body
)
assert (
'state_change_total{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 1.0' in body
)
assert (
'entity_available{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 1.0' in body
)
assert (
'sensor_humidity_percent{domain="sensor",'
'entity="sensor.outside_humidity",'
'friendly_name="Outside Humidity"} 54.0' in body
)
assert (
'state_change_total{domain="sensor",'
'entity="sensor.outside_humidity",'
'friendly_name="Outside Humidity"} 1.0' in body
)
assert (
'entity_available{domain="sensor",'
'entity="sensor.outside_humidity",'
'friendly_name="Outside Humidity"} 1.0' in body
)
# Make sensor_1 unavailable.
set_state_with_entry(
hass, data["sensor_1"], STATE_UNAVAILABLE, data["sensor_1_attributes"]
)
await hass.async_block_till_done()
body = await generate_latest_metrics(client)
# Check that only the availability changed on sensor_1.
assert (
'sensor_temperature_celsius{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 15.6' in body
)
assert (
'state_change_total{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 2.0' in body
)
assert (
'entity_available{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 0.0' in body
)
# The other sensor should be unchanged.
assert (
'sensor_humidity_percent{domain="sensor",'
'entity="sensor.outside_humidity",'
'friendly_name="Outside Humidity"} 54.0' in body
)
assert (
'state_change_total{domain="sensor",'
'entity="sensor.outside_humidity",'
'friendly_name="Outside Humidity"} 1.0' in body
)
assert (
'entity_available{domain="sensor",'
'entity="sensor.outside_humidity",'
'friendly_name="Outside Humidity"} 1.0' in body
)
# Bring sensor_1 back and check that it is correct.
set_state_with_entry(hass, data["sensor_1"], 200.0, data["sensor_1_attributes"])
await hass.async_block_till_done()
body = await generate_latest_metrics(client)
assert (
'sensor_temperature_celsius{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 200.0' in body
)
assert (
'state_change_total{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 3.0' in body
)
assert (
'entity_available{domain="sensor",'
'entity="sensor.outside_temperature",'
'friendly_name="Outside Temperature"} 1.0' in body
)
@pytest.fixture(name="sensor_entities")
async def sensor_fixture(
hass: HomeAssistant, entity_registry: er.EntityRegistry