From 65091130b78e6ac72ba5da463018dbc6be6e97ea Mon Sep 17 00:00:00 2001 From: Christopher Pruijsen Date: Tue, 1 Dec 2026 17:02:40 +0000 Subject: [PATCH] Skip System Monitor battery entities on hosts without a battery (#195840) --- .../components/systemmonitor/binary_sensor.py | 1 + .../components/systemmonitor/coordinator.py | 2 ++ .../components/systemmonitor/sensor.py | 2 ++ .../systemmonitor/test_binary_sensor.py | 16 ++++++++++++++++ tests/components/systemmonitor/test_sensor.py | 17 +++++++++++++++++ 5 files changed, 38 insertions(+) diff --git a/homeassistant/components/systemmonitor/binary_sensor.py b/homeassistant/components/systemmonitor/binary_sensor.py index 3ec218c36872..f665416f210d 100644 --- a/homeassistant/components/systemmonitor/binary_sensor.py +++ b/homeassistant/components/systemmonitor/binary_sensor.py @@ -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) diff --git a/homeassistant/components/systemmonitor/coordinator.py b/homeassistant/components/systemmonitor/coordinator.py index 91f0cc991219..623de25e4f7b 100644 --- a/homeassistant/components/systemmonitor/coordinator.py +++ b/homeassistant/components/systemmonitor/coordinator.py @@ -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: diff --git a/homeassistant/components/systemmonitor/sensor.py b/homeassistant/components/systemmonitor/sensor.py index 4fce01ff02b6..fd92451d22e3 100644 --- a/homeassistant/components/systemmonitor/sensor.py +++ b/homeassistant/components/systemmonitor/sensor.py @@ -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}")) diff --git a/tests/components/systemmonitor/test_binary_sensor.py b/tests/components/systemmonitor/test_binary_sensor.py index 18a61cb43bc8..74bda9c38bb5 100644 --- a/tests/components/systemmonitor/test_binary_sensor.py +++ b/tests/components/systemmonitor/test_binary_sensor.py @@ -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 diff --git a/tests/components/systemmonitor/test_sensor.py b/tests/components/systemmonitor/test_sensor.py index 61028f0e4447..b9f44434fda9 100644 --- a/tests/components/systemmonitor/test_sensor.py +++ b/tests/components/systemmonitor/test_sensor.py @@ -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,