diff --git a/README.md b/README.md index a3941f08..faf231b9 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/custom_components/feedparser/sensor.py b/custom_components/feedparser/sensor.py index 5524f823..bd2d9556 100644 --- a/custom_components/feedparser/sensor.py +++ b/custom_components/feedparser/sensor.py @@ -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]: diff --git a/tests/reader_harness.py b/tests/reader_harness.py index 92bc68c4..984fd690 100644 --- a/tests/reader_harness.py +++ b/tests/reader_harness.py @@ -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: diff --git a/tests/test_integration.py b/tests/test_integration.py index ed201148..a22654c2 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -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,