Skip System Monitor battery entities on hosts without a battery (#195840)

This commit is contained in:
Christopher Pruijsen
2026-12-01 17:02:40 +00:00
committed by GitHub
parent e3d80bac78
commit 65091130b7
5 changed files with 38 additions and 0 deletions
@@ -126,6 +126,7 @@ async def async_setup_entry(
"",
)
for sensor_description in BINARY_SENSOR_TYPES
if not coordinator.battery_missing
)
async_add_entities(entities)
@@ -118,6 +118,7 @@ class SystemMonitorCoordinator(TimestampDataUpdateCoordinator[SensorData]):
self._psutil = psutil_wrapper.psutil
self._arguments = arguments
self.boot_time: datetime | None = None
self.battery_missing = False
self._initial_update: bool = True
self.update_subscribers: dict[tuple[str, str], set[str]] = (
@@ -287,6 +288,7 @@ class SystemMonitorCoordinator(TimestampDataUpdateCoordinator[SensorData]):
try:
battery = self._psutil.sensors_battery()
_LOGGER.debug("battery: %s", battery)
self.battery_missing = battery is None
except (FileNotFoundError, PermissionError) as err:
_LOGGER.debug("OS error when accessing battery sensors: %s", err)
except AttributeError:
@@ -771,6 +771,8 @@ async def async_setup_entry(
continue
if _type.startswith(SENSORS_NO_ARG):
if _type.startswith("battery") and coordinator.battery_missing:
continue
argument = ""
is_enabled = check_legacy_resource(f"{_type}_{argument}", legacy_resources)
loaded_resources.add(slugify(f"{_type}_{argument}"))
@@ -103,3 +103,19 @@ async def test_sensor_process_fails(
assert process_sensor.state == STATE_OFF
assert "Failed to load process with ID: 1, old name: python3" in caplog.text
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_battery_binary_sensor_not_loaded_without_battery(
hass: HomeAssistant,
mock_psutil: Mock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test the charging binary sensor is not created without a battery."""
mock_psutil.sensors_battery.return_value = None
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get("binary_sensor.system_monitor_process_python3")
assert hass.states.get("binary_sensor.system_monitor_charging") is None
@@ -820,6 +820,23 @@ async def test_sensor_without_param_exception(
assert state.state == STATE_UNAVAILABLE
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_battery_sensors_not_loaded_without_battery(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_psutil: Mock,
) -> None:
"""Test the battery sensors are not created when the host has no battery."""
mock_psutil.sensors_battery.return_value = None
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get("sensor.system_monitor_processor_use")
assert hass.states.get("sensor.system_monitor_battery") is None
assert hass.states.get("sensor.system_monitor_battery_empty") is None
@pytest.mark.usefixtures("entity_registry_enabled_by_default")
async def test_psi_sensor_unavailable(
hass: HomeAssistant,