From 94ffde0b65c1905df4dc7b864363000e65249fdf Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Wed, 7 Oct 2026 06:41:38 +0200 Subject: [PATCH] Always create the APC UPS Daemon battery transfer sensors (#184559) --- homeassistant/components/apcupsd/sensor.py | 18 ++++++++---------- tests/components/apcupsd/test_sensor.py | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/apcupsd/sensor.py b/homeassistant/components/apcupsd/sensor.py index 6abad3646c50..56be7af27dbe 100644 --- a/homeassistant/components/apcupsd/sensor.py +++ b/homeassistant/components/apcupsd/sensor.py @@ -47,6 +47,11 @@ IGNORED_SENSORS: Final = { "serialno", } +# Sensors that only appear after an event since the last daemon restart: a self +# test for "laststest", a transfer to or from the battery for "xonbatt" and +# "xoffbatt". They are always added, and stay unknown until the event happens. +EVENT_SENSORS: Final = {LAST_S_TEST, "xoffbatt", "xonbatt"} + SENSORS: dict[str, SensorEntityDescription] = { "alarmdel": SensorEntityDescription( key="alarmdel", @@ -430,16 +435,9 @@ async def async_setup_entry( entities = [] - # "laststest" is a special sensor that only appears when - # the APC UPS daemon has done a periodical (or manual) self - # test since last daemon restart. It might not be available - # when we set up the integration, and we do not know if it - # would ever be available. Here we add it anyway and mark it - # as unknown initially. - # - # We also sort the resources to ensure the order of entities - # created is deterministic - for resource in sorted(available_resources | {LAST_S_TEST}): + # We sort the resources to ensure the order of entities created is + # deterministic + for resource in sorted(available_resources | EVENT_SENSORS): if resource in IGNORED_SENSORS: continue if resource not in SENSORS: diff --git a/tests/components/apcupsd/test_sensor.py b/tests/components/apcupsd/test_sensor.py index c302e64e0864..4a08b36a1f20 100644 --- a/tests/components/apcupsd/test_sensor.py +++ b/tests/components/apcupsd/test_sensor.py @@ -147,6 +147,20 @@ async def test_manual_update_entity( MOCK_MINIMAL_STATUS | {"LASTSTEST": "1970-01-01 00:00:00 +0000"}, id="last_self_test_missing", ), + pytest.param( + # The transfer fields only appear after a transfer to or from the + # battery since the daemon started. + MOCK_MINIMAL_STATUS, + "sensor.apc_ups_transfer_to_battery", + MOCK_MINIMAL_STATUS | {"XONBATT": "1970-01-01 00:00:00 +0000"}, + id="xonbatt_missing", + ), + pytest.param( + MOCK_MINIMAL_STATUS, + "sensor.apc_ups_transfer_from_battery", + MOCK_MINIMAL_STATUS | {"XOFFBATT": "1970-01-01 00:00:00 +0000"}, + id="xoffbatt_missing", + ), pytest.param( MOCK_MINIMAL_STATUS | {"XOFFBATT": "N/A"}, "sensor.apc_ups_transfer_from_battery",