Fix feedreader events for unsorted feeds (#177954)

This commit is contained in:
Michael
2026-08-01 21:20:16 +02:00
committed by GitHub
parent c8e72e87a4
commit 245d8d4268
5 changed files with 102 additions and 3 deletions
@@ -171,13 +171,15 @@ class FeedReaderCoordinator(
"""Update last_entry_timestamp and fire entry."""
# Check if the entry has a updated or published date.
# Start from a updated date because generally `updated` > `published`.
if time_stamp := entry.get("updated_parsed") or entry.get("published_parsed"):
self._last_entry_timestamp = time_stamp
else:
time_stamp = entry.get("updated_parsed") or entry.get("published_parsed")
if time_stamp is None:
_LOGGER.debug(
"No updated_parsed or published_parsed info available for entry %s",
entry,
)
elif time_stamp and time_stamp > self._last_entry_timestamp:
self._last_entry_timestamp = time_stamp
entry["feed_url"] = self.url
self.hass.bus.async_fire(self._event_type, entry)
_LOGGER.debug("New event fired for entry %s", entry.get("link"))
+12
View File
@@ -75,6 +75,18 @@ def fixture_feed_atom_htmlentities(hass: HomeAssistant) -> bytes:
return load_fixture_bytes("feedreader10.xml", DOMAIN)
@pytest.fixture(name="feed_unsorted")
def fixture_feed_unsorted(hass: HomeAssistant) -> bytes:
"""Load test ATOM feed data with HTML Entities."""
return load_fixture_bytes("feedreader11.xml", DOMAIN)
@pytest.fixture(name="feed_unsorted_update")
def fixture_feed_unsorted_update(hass: HomeAssistant) -> bytes:
"""Load test ATOM feed data with HTML Entities."""
return load_fixture_bytes("feedreader12.xml", DOMAIN)
@pytest.fixture(name="events")
async def fixture_events(hass: HomeAssistant) -> list[Event]:
"""Fixture that catches alexa events."""
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 3</title>
<pubDate>Mon, 30 Apr 2018 15:02:00 +1000</pubDate>
<content>Content 3</content>
</item>
<item>
<title>Title 1</title>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<content>Content 1</content>
</item>
<item>
<title>Title 2</title>
<pubDate>Mon, 30 Apr 2018 15:01:00 +1000</pubDate>
<content>Content 2</content>
</item>
</channel>
</rss>
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Sample</title>
<description>This is an example of an RSS feed</description>
<link>http://www.example.com/main.html</link>
<lastBuildDate>Mon, 30 Apr 2018 12:00:00 +1000 </lastBuildDate>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<ttl>1800</ttl>
<item>
<title>Title 3</title>
<pubDate>Mon, 30 Apr 2018 15:02:00 +1000</pubDate>
<content>Content 3</content>
</item>
<item>
<title>Title 4</title>
<pubDate>Mon, 30 Apr 2018 15:03:00 +1000</pubDate>
<content>Content 4</content>
</item>
<item>
<title>Title 1</title>
<pubDate>Mon, 30 Apr 2018 15:00:00 +1000</pubDate>
<content>Content 1</content>
</item>
<item>
<title>Title 2</title>
<pubDate>Mon, 30 Apr 2018 15:01:00 +1000</pubDate>
<content>Content 2</content>
</item>
</channel>
</rss>
+28
View File
@@ -237,6 +237,34 @@ async def test_feed_updates(
assert len(events) == 2
async def test_unsorted_feed_updates(
hass: HomeAssistant, events, feed_unsorted, feed_unsorted_update
) -> None:
"""Test feed updates."""
side_effect = [
feed_unsorted,
feed_unsorted_update,
]
entry = create_mock_entry(VALID_CONFIG_DEFAULT)
entry.add_to_hass(hass)
with patch(
"homeassistant.components.feedreader.coordinator.feedparser.http.get",
side_effect=side_effect,
):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert len(events) == 3
# Change time and fetch one more unordered entry
future = dt_util.utcnow() + timedelta(hours=1, seconds=1)
async_fire_time_changed(hass, future)
await hass.async_block_till_done(wait_background_tasks=True)
assert len(events) == 4
async def test_feed_default_max_length(
hass: HomeAssistant, events, feed_21_events
) -> None: