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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ To see every field a feed provides, leave `inclusions` empty and look at the sen

## Output

The sensor's state is the number of entries. The entries themselves are in the `entries` attribute, in the order the feed lists them (usually newest first). With `inclusions: [title, link, published, summary, image]` an entry looks like this:
The sensor's state is the number of entries: `0` when the feed loads but has no entries (or none match `match`/`exclude_match`), and `unavailable` while the feed can't be fetched or parsed. The entries themselves are in the `entries` attribute, in the order the feed lists them (usually newest first). With `inclusions: [title, link, published, summary, image]` an entry looks like this:

```yaml
entries:
Expand Down
10 changes: 8 additions & 2 deletions custom_components/feedparser/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ def feed_entries(self) -> FeedEntries:

@property
def native_value(self) -> int | None:
"""Return the number of entries, or None if the feed has none."""
return len(self.feed_entries) or None
"""Return the number of entries, 0 if the feed has none (or none match).

None (``unknown``) only while no feed has been loaded yet; a feed that
can't be fetched makes the entity ``unavailable`` instead.
"""
if self.coordinator.data is None:
return None
return len(self.coordinator.data)

@property
def extra_state_attributes(self) -> dict[str, FeedEntries]:
Expand Down
4 changes: 2 additions & 2 deletions tests/reader_harness.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def update(self: ReaderHarness) -> None:
self.feed_entries = self.reader.read()

@property
def native_value(self: ReaderHarness) -> int | None:
def native_value(self: ReaderHarness) -> int:
"""Return the entry count the sensor would show."""
return len(self.feed_entries) or None
return len(self.feed_entries)

@property
def local_time(self: ReaderHarness) -> bool:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,28 @@ 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_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", [])
await add_entry(hass, "Test Feed", url)
state = get_state(hass)
assert state.state == "0"
assert state.attributes["entries"] == []


async def test_no_matching_entries_state_is_zero(
hass: HomeAssistant,
tmp_path: Path,
) -> None:
"""A filter that matches nothing reports 0, not unknown (#182)."""
url = write_feed(tmp_path / "feed.xml", ["rain", "fog"])
await setup_yaml(
hass,
{"name": "Yaml Feed", "feed_url": url, "match": "Leicester"},
)
assert get_state(hass, "sensor.yaml_feed").state == "0"


async def test_options_change_reloads_entry(
hass: HomeAssistant,
tmp_path: Path,
Expand Down
Loading