Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear entities before updating the sensor to avoid duplicates #89

Merged
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
2 changes: 2 additions & 0 deletions custom_components/feedparser/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def __init__(
self._local_time = local_time
self._entries: list[dict[str, str]] = []
self._attr_extra_state_attributes = {"entries": self._entries}
_attr_attribution = "Data retrieved using RSS feedparser"

def update(self: FeedParserSensor) -> None:
"""Parse the feed and update the state of the sensor."""
Expand All @@ -119,6 +120,7 @@ def update(self: FeedParserSensor) -> None:
if len(parsed_feed.entries) > self._show_topn
else len(parsed_feed.entries)
)
self._entries.clear() # clear the entries to avoid duplicates
self._entries.extend(self._generate_entries(parsed_feed))

def _generate_entries(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,13 @@ def test_update_sensor_entries_time(
# assert that the time of the first entry in the sensor is equal to
# the time of the first entry in the feed
assert first_entry_time == first_sensor_entry_time


def test_check_duplicates(feed_sensor: FeedParserSensor) -> None:
"""Test that the sensor stores only unique entries."""
feed_sensor.update()
assert feed_sensor.extra_state_attributes["entries"]
after_first_update = len(feed_sensor.feed_entries)
feed_sensor.update()
after_second_update = len(feed_sensor.feed_entries)
assert after_first_update == after_second_update