Skip to content

Commit

Permalink
Merge pull request #89 from ogajduse:feat/more-typing-n-enhancements
Browse files Browse the repository at this point in the history
Clear entities before updating the sensor to avoid duplicates
  • Loading branch information
ogajduse authored Aug 9, 2023
2 parents 3c73434 + c3e6f5a commit bcb7c37
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
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

0 comments on commit bcb7c37

Please sign in to comment.