From dd961ce77f7342661a005416a09e8a7e35475341 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 23:16:44 +0000 Subject: [PATCH] Stop writing a state on every poll: remove force_update _attr_force_update was added in 0.2.0 because the entries list was changed in place, and Home Assistant's shallow attribute comparison missed changes to the same list object (#104). Since 1.0, FeedReader.read() returns a new list on every poll, so changed entries are detected without it. With force_update, every poll wrote a state and fired state_changed even when the feed hadn't changed: a recorder row per poll, automations triggered on the sensor running every poll, and logbook/history noise. Fixes #185 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FvqVzfJq7yZSkZfSyG3YHT --- custom_components/feedparser/sensor.py | 8 +++---- tests/test_integration.py | 31 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/custom_components/feedparser/sensor.py b/custom_components/feedparser/sensor.py index 5524f823..8b104ccc 100644 --- a/custom_components/feedparser/sensor.py +++ b/custom_components/feedparser/sensor.py @@ -133,11 +133,11 @@ async def async_setup_entry( class FeedParserSensor(CoordinatorEntity[FeedparserCoordinator], SensorEntity): """Number of feed entries, with the entries themselves as an attribute.""" - # force update the entity since the number of feed entries does not necessarily - # change, but we still want to update the extra_state_attributes - _attr_force_update = True + # No force_update: FeedReader.read() returns a new entries list on every poll, + # so Home Assistant sees changed entries even when their count stays the same, + # and a poll that changes nothing writes no state. # The entries attribute regularly exceeds the recorder's 16 KiB attribute limit - # and changes on every poll; keep it out of the database. + # and changes often; keep it out of the database. _unrecorded_attributes = frozenset({"entries"}) _attr_icon = "mdi:rss" _attr_attribution = "Data retrieved using RSS feedparser" diff --git a/tests/test_integration.py b/tests/test_integration.py index ed201148..9fc8561f 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -10,12 +10,13 @@ import pytest import requests from homeassistant import config_entries -from homeassistant.const import CONF_NAME +from homeassistant.const import CONF_NAME, EVENT_STATE_CHANGED from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import issue_registry as ir from homeassistant.setup import async_setup_component from pytest_homeassistant_custom_component.common import ( MockConfigEntry, + async_capture_events, async_fire_time_changed, ) @@ -265,6 +266,34 @@ async def test_yaml_filter_is_imported(hass: HomeAssistant, tmp_path: Path) -> N assert get_state(hass, "sensor.yaml_feed").state == "1" +async def test_unchanged_poll_writes_no_state( + hass: HomeAssistant, + tmp_path: Path, +) -> None: + """Only polls that change the feed write a state (#185). + + Changed entries still count when their number stays the same. + """ + feed = tmp_path / "feed.xml" + url = write_feed(feed, ["one", "two"]) + entry = await add_entry(hass, "Test Feed", url) + coordinator = entry.runtime_data + events = async_capture_events(hass, EVENT_STATE_CHANGED) + + for _ in range(2): + await coordinator.async_refresh() + await hass.async_block_till_done() + assert events == [] + + write_feed(feed, ["three", "four"]) + await coordinator.async_refresh() + await hass.async_block_till_done() + assert len(events) == 1 + state = get_state(hass) + assert state.state == "2" + assert [e["title"] for e in state.attributes["entries"]] == ["three", "four"] + + async def test_options_change_reloads_entry( hass: HomeAssistant, tmp_path: Path,