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 @@ -210,7 +210,7 @@ The integration supports both UI and YAML setup. Most options are the same in bo
- UI uses comma-separated text for `inclusions` and `exclusions`.
- YAML uses proper lists.
- UI uses a single duration control for `scan_interval`; YAML uses `scan_interval` object keys such as `hours` and `minutes`.
- **Date parsing**: RSS (RFC 822) and Atom (ISO 8601) dates are both supported, including US timezone abbreviations such as `EDT`. If a date cannot be parsed at all, the original text is kept.
- **Date parsing**: RSS (RFC 822) and Atom (ISO 8601) dates are both supported, including US timezone abbreviations such as `EDT`. A date without a time zone is taken to be UTC. `%Z` in `date_format` prints a zone name only when there is one (`UTC`, or Home Assistant's zone with `local_time`); for a numeric offset such as `+0100` it prints nothing, so use `%z` to show the offset. If a date cannot be parsed at all, the original text is kept.

Due to how `custom_components` are loaded, it is normal to see a `ModuleNotFoundError` error on first boot after adding this, to resolve it, restart Home-Assistant.

Expand Down
65 changes: 43 additions & 22 deletions custom_components/feedparser/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
import time
import warnings
from collections.abc import Mapping
from datetime import UTC, datetime, timezone
from datetime import UTC, datetime, timezone, tzinfo
from typing import Protocol, cast

import feedparser # type: ignore[import]
from dateutil import parser
from dateutil import parser, tz
from homeassistant.util import dt

from .fetch import USER_AGENT, fetch_feed, redact_proxy
Expand All @@ -33,6 +33,25 @@
DATE_KEYS = ("published", "updated", "created", "expired")


def _without_offset_name(value: datetime) -> tzinfo | None:
"""Return `value`'s tzinfo, minus any name that is only its UTC offset.

Feeds mostly give a numeric offset (``+0100``) rather than a zone name. The
stdlib names such a zone ``UTC+01:00``, which ``%Z`` in `date_format` would
print; keep the offset but drop that made-up name so ``%Z`` prints nothing,
as it did before 1.0. UTC itself and real zone names are kept.
"""
zone = value.tzinfo
offset = value.utcoffset()
if (
offset is None
or not offset
or (not isinstance(zone, timezone) and value.tzname())
):
return zone
return tz.tzoffset(None, offset)


# Entry fields searched by the `match` / `exclude_match` filters. RSS
# <description> is exposed by feedparser as `summary`.
FILTER_KEYS = ("title", "summary")
Expand Down Expand Up @@ -282,12 +301,16 @@ def _parse_date(
"""Parse a feed date into an aware datetime, or None if impossible.

Tried in order, keeping the feed's own UTC offset where possible:
RFC 822 (RSS), ISO 8601 / W3CDTF (Atom), feedparser's own normalized UTC
``*_parsed`` value, and finally dateutil as a best effort.
RFC 822 (RSS), ISO 8601 / W3CDTF (Atom), dateutil, feedparser's own
normalized UTC ``*_parsed`` value, and finally a date without a time zone
(from any of the string parsers), which is taken to be UTC.
"""
parsed_time = self._parse_date_string(date)
parsed_time, naive_time = self._parse_date_string(date)
if parsed_time is None and isinstance(parsed, time.struct_time):
parsed_time = datetime.fromtimestamp(calendar.timegm(parsed), tz=UTC)
if parsed_time is None and naive_time is not None:
# same assumption feedparser makes for ISO dates without a zone
parsed_time = naive_time.replace(tzinfo=UTC)

if parsed_time is None:
_LOGGER.debug(
Expand All @@ -297,38 +320,36 @@ def _parse_date(
)
return None

if not parsed_time.tzname():
# replace tzinfo with UTC offset if tzinfo does not contain a TZ name
parsed_time = parsed_time.replace(
tzinfo=timezone(parsed_time.utcoffset()), # type: ignore[arg-type]
)
parsed_time = parsed_time.replace(tzinfo=_without_offset_name(parsed_time))

if self.local_time:
parsed_time = dt.as_local(parsed_time)
_LOGGER.debug("Feed %s: Parsed date: %s", self.name, parsed_time)
return parsed_time

@staticmethod
def _parse_date_string(date: str) -> datetime | None:
"""Parse a date string with the stdlib, then dateutil. Return aware or None."""
def _parse_date_string(date: str) -> tuple[datetime | None, datetime | None]:
"""Parse a date string with the stdlib, then dateutil.

Return ``(aware, naive)``: the first result that has a time zone, and
otherwise the first one without, so the caller can prefer a better source
(feedparser's ``*_parsed``) before assuming a zone for the naive one.
"""
candidates = []
with contextlib.suppress(TypeError, ValueError, IndexError):
candidates.append(email.utils.parsedate_to_datetime(date))
with contextlib.suppress(ValueError):
candidates.append(datetime.fromisoformat(date.strip()))
for candidate in candidates:
if candidate.tzinfo is not None:
return candidate

# dateutil warns (UnknownTimezoneWarning) on abbreviations such as EDT and
# returns a naive datetime; a naive result is treated as unparseable.
# returns a naive datetime for them.
with warnings.catch_warnings():
warnings.simplefilter("ignore")
try:
fallback = parser.parse(date)
except (ValueError, OverflowError):
return None
return fallback if fallback.tzinfo is not None else None
with contextlib.suppress(ValueError, OverflowError):
candidates.append(parser.parse(date))

aware = next((c for c in candidates if c.tzinfo is not None), None)
naive = next((c for c in candidates if c.tzinfo is None), None)
return aware, naive

def _process_image(self: FeedReader, feed_entry: Mapping[str, object]) -> str:
"""Return the best image URL for an entry, or the default thumbnail.
Expand Down
68 changes: 66 additions & 2 deletions tests/test_hardening.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
from __future__ import annotations

import warnings
import zoneinfo
from pathlib import Path
from typing import Any
from unittest.mock import patch

import pytest
import requests
from homeassistant.util import dt
from reader_harness import ReaderHarness

from custom_components.feedparser import fetch
Expand Down Expand Up @@ -39,12 +42,14 @@ def _sensor(
inclusions: list[str | None] | None = None,
*,
remove_summary_image: bool = False,
date_format: str = DATE_FORMAT,
local_time: bool = False,
) -> ReaderHarness:
return ReaderHarness(
feed=path.absolute().as_uri(),
name="test",
date_format=DATE_FORMAT,
local_time=False,
date_format=date_format,
local_time=local_time,
show_topn=10,
remove_summary_image=remove_summary_image,
inclusions=inclusions or [],
Expand Down Expand Up @@ -90,6 +95,10 @@ def test_non_utf8_feed_is_decoded_from_xml_prolog(tmp_path: Path) -> None:
("2024-02-01T02:00:01+02:00", "2024-02-01T02:00:01+0200"),
# US timezone abbreviation (#132): dateutil can't resolve it, feedparser can
("Tue, 08 Sep 2026 08:00:00 EDT", "2026-09-08T08:00:00-0400"),
# no time zone (#186): taken as UTC instead of keeping the raw string
("Mon, 01 Jan 2024 10:00:00", "2024-01-01T10:00:00+0000"),
("01/02/2024 10:00", "2024-01-02T10:00:00+0000"),
("2024-01-01 10:00:00", "2024-01-01T10:00:00+0000"),
],
)
def test_date_formats(tmp_path: Path, raw: str, expected: str) -> None:
Expand All @@ -106,6 +115,61 @@ def test_date_formats(tmp_path: Path, raw: str, expected: str) -> None:
assert sensor.feed_entries[0]["published"] == expected


def _published(sensor_path: Path, raw: str, **kwargs: Any) -> object: # noqa: ANN401
sensor_path.write_text(
_rss(f"<item><title>a</title><pubDate>{raw}</pubDate></item>"),
encoding="utf-8",
)
sensor = _sensor(sensor_path, ["title", "published"], **kwargs)
sensor.update()
return sensor.feed_entries[0]["published"]


@pytest.mark.parametrize(
("raw", "expected"),
[
# a bare offset has no zone name: %Z prints nothing, as before 1.0 (#184)
("Mon, 01 Jan 2024 10:00:00 +0100", "10:00 "),
("2024-01-01T10:00:00-05:30", "10:00 "),
("Mon, 01 Jan 2024 10:00:00 EDT", "10:00 "),
# UTC keeps its name
("Mon, 01 Jan 2024 10:00:00 GMT", "10:00 UTC"),
("2024-01-01T10:00:00Z", "10:00 UTC"),
# no zone at all: assumed UTC
("Mon, 01 Jan 2024 10:00:00", "10:00 UTC"),
],
)
def test_zone_name_in_date_format(tmp_path: Path, raw: str, expected: str) -> None:
"""``%Z`` prints a real zone name, never a made-up ``UTC+01:00``."""
published = _published(tmp_path / "zone.xml", raw, date_format="%H:%M %Z")
assert published == expected


@pytest.mark.parametrize(
("raw", "expected"),
[
("Mon, 01 Jan 2024 10:00:00 +0100", "10:00 CET"),
("Mon, 01 Jan 2024 10:00:00 EDT", "15:00 CET"),
("Mon, 01 Jan 2024 10:00:00", "11:00 CET"),
("01/02/2024 10:00", "11:00 CET"),
],
)
def test_local_time_conversion(tmp_path: Path, raw: str, expected: str) -> None:
"""With `local_time`, every date, zoned or not, ends up in HA's zone."""
original = dt.get_default_time_zone()
dt.set_default_time_zone(zoneinfo.ZoneInfo("Europe/Prague"))
try:
published = _published(
tmp_path / "local.xml",
raw,
date_format="%H:%M %Z",
local_time=True,
)
finally:
dt.set_default_time_zone(original)
assert published == expected


def test_unparseable_date_keeps_raw_value(tmp_path: Path) -> None:
"""A garbage date keeps the raw string instead of failing the whole update."""
feed = tmp_path / "bad_date.xml"
Expand Down
Loading