Skip to content

Commit

Permalink
Add HTTP headers to the request
Browse files Browse the repository at this point in the history
  • Loading branch information
ogajduse committed Dec 12, 2023
1 parent e617dbe commit 09f7ded
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ repos:
voluptuous-stubs,
types-python-dateutil,
types-PyYAML,
types-requests,
]
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
Expand Down
22 changes: 14 additions & 8 deletions custom_components/feedparser/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@

import feedparser # type: ignore[import]
import homeassistant.helpers.config_validation as cv
import requests
import voluptuous as vol
from dateutil import parser
from feedparser import FeedParserDict
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL
from homeassistant.util import dt
from requests_file import FileAdapter

if TYPE_CHECKING:
from homeassistant.core import HomeAssistant
Expand All @@ -38,6 +40,7 @@
DEFAULT_SCAN_INTERVAL = timedelta(hours=1)
DEFAULT_THUMBNAIL = "https://www.home-assistant.io/images/favicon-192x192-full.png"
DEFAULT_TOPN = 9999
USER_AGENT = f"Home Assistant Feedparser Integration {__version__}"

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
Expand Down Expand Up @@ -124,9 +127,14 @@ def __repr__(self: FeedParserSensor) -> str:
def update(self: FeedParserSensor) -> None:
"""Parse the feed and update the state of the sensor."""
_LOGGER.debug("Feed %s: Polling feed data from %s", self.name, self._feed)
parsed_feed: FeedParserDict = feedparser.parse(self._feed)

if not parsed_feed:
s: requests.Session = requests.Session()
s.mount("file://", FileAdapter())
s.headers.update({"User-Agent": USER_AGENT})
res: requests.Response = s.get(self._feed)
res.raise_for_status()
parsed_feed: FeedParserDict = feedparser.parse(res.text)

if not parsed_feed.entries:
self._attr_native_value = None
_LOGGER.warning("Feed %s: No data received.", self.name)
return
Expand Down Expand Up @@ -183,12 +191,10 @@ def _generate_sensor_entry(
else:
sensor_entry[key] = value

if "image" in self._inclusions and "image" not in sensor_entry:
if "image" not in sensor_entry:
sensor_entry["image"] = self._process_image(feed_entry)
if (
"link" in self._inclusions
and "link" not in sensor_entry
and (processed_link := self._process_link(feed_entry))
if "link" not in sensor_entry and (
processed_link := self._process_link(feed_entry)
):
sensor_entry["link"] = processed_link
_LOGGER.debug("Feed %s: Generated sensor entry: %s", self.name, sensor_entry)
Expand Down

0 comments on commit 09f7ded

Please sign in to comment.