diff --git a/custom_components/feedparser/sensor.py b/custom_components/feedparser/sensor.py index bd2d9556..4aa9d1f4 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 a22654c2..8231a620 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_empty_feed_state_is_zero(hass: HomeAssistant, tmp_path: Path) -> None: """A feed that loads but has no entries reports 0, not unknown (#182).""" url = write_feed(tmp_path / "feed.xml", [])