Skip to content

Commit bcb7c37

Browse files
authored
Merge pull request #89 from ogajduse:feat/more-typing-n-enhancements
Clear entities before updating the sensor to avoid duplicates
2 parents 3c73434 + c3e6f5a commit bcb7c37

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

custom_components/feedparser/sensor.py

+2
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def __init__(
104104
self._local_time = local_time
105105
self._entries: list[dict[str, str]] = []
106106
self._attr_extra_state_attributes = {"entries": self._entries}
107+
_attr_attribution = "Data retrieved using RSS feedparser"
107108

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

124126
def _generate_entries(

tests/test_sensors.py

+10
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,13 @@ def test_update_sensor_entries_time(
137137
# assert that the time of the first entry in the sensor is equal to
138138
# the time of the first entry in the feed
139139
assert first_entry_time == first_sensor_entry_time
140+
141+
142+
def test_check_duplicates(feed_sensor: FeedParserSensor) -> None:
143+
"""Test that the sensor stores only unique entries."""
144+
feed_sensor.update()
145+
assert feed_sensor.extra_state_attributes["entries"]
146+
after_first_update = len(feed_sensor.feed_entries)
147+
feed_sensor.update()
148+
after_second_update = len(feed_sensor.feed_entries)
149+
assert after_first_update == after_second_update

0 commit comments

Comments
 (0)