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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.formatting.provider": "black"
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ sensor:
name: Engineering Feed
feed_url: 'https://www.sciencedaily.com/rss/matter_energy/engineering.xml'
date_format: '%a, %b %d %I:%M %p'
scan_interval:
hours: 3
inclusions:
- title
- link
Expand All @@ -49,6 +51,7 @@ key | description
**show_topn (Optional)** | fetch how many entres from rss source,if not set then fetch all
**inclusions (Optional)** | List of fields to include from populating the list
**exclusions (Optional)** | List of fields to exclude from populating the list
**scan_interval (Optional)** | Update interval in hours

***

Expand Down
40 changes: 19 additions & 21 deletions custom_components/feedparser/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@
import voluptuous as vol
from dateutil import parser
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_NAME
import homeassistant.util.dt as dt
from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

import feedparser

__version__ = "0.1.7"
__version__ = "0.1.6"

COMPONENT_REPO = "https://github.com/custom-components/sensor.feedparser/"

REQUIREMENTS = ["feedparser"]

Expand All @@ -23,13 +27,9 @@
CONF_INCLUSIONS = "inclusions"
CONF_EXCLUSIONS = "exclusions"
CONF_SHOW_TOPN = "show_topn"
CONF_LOCAL_TIME = 'local_time'

DEFAULT_SCAN_INTERVAL = timedelta(hours=1)

COMPONENT_REPO = "https://github.com/custom-components/sensor.feedparser/"
SCAN_INTERVAL = timedelta(hours=1)

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_NAME): cv.string,
Expand All @@ -38,23 +38,28 @@
vol.Optional(CONF_SHOW_TOPN, default=9999): cv.positive_int,
vol.Optional(CONF_INCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_EXCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_LOCAL_TIME, default=False): cv.boolean,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): cv.time_period,
}
)


@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_devices: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
async_add_devices(
[
FeedParserSensor(
feed=config[CONF_FEED_URL],
name=config[CONF_NAME],
date_format=config[CONF_DATE_FORMAT],
local_time=config[CONF_LOCAL_TIME],
show_topn=config[CONF_SHOW_TOPN],
inclusions=config[CONF_INCLUSIONS],
exclusions=config[CONF_EXCLUSIONS],
scan_interval=config[CONF_SCAN_INTERVAL],
)
],
True,
Expand All @@ -68,20 +73,21 @@ def __init__(
name: str,
date_format: str,
show_topn: str,
local_time: bool,
exclusions: str,
inclusions: str,
scan_interval: int,
) -> None:
self._feed = feed
self._attr_name = name
self._attr_icon = "mdi:rss"
self._date_format = date_format
self._local_time = local_time
self._show_topn = show_topn
self._inclusions = inclusions
self._exclusions = exclusions
self._scan_interval = scan_interval
self._attr_state = None
self._entries = []
self._attr_extra_state_attributes = {"entries": self._entries}

def update(self):
parsed_feed = feedparser.parse(self._feed)
Expand All @@ -107,11 +113,7 @@ def update(self):
):
continue
if key in ["published", "updated", "created", "expired"]:
value = parser.parse(value)
if self._local_time:
value = dt.as_local(value)
value = value.strftime(self._date_format)

value = parser.parse(value).strftime(self._date_format)

entry_value[key] = value

Expand All @@ -129,7 +131,3 @@ def update(self):
] = "https://www.home-assistant.io/images/favicon-192x192-full.png"

self._entries.append(entry_value)

@property
def device_state_attributes(self):
return {"entries": self._entries}