Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions custom_components/feedparser/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 30 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)

Expand Down Expand Up @@ -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", [])
Expand Down
Loading