Skip to content

Commit 83e6fb3

Browse files
authored
Add update interval (custom-components#52)
* use _attr * icon * remove typing * Add update_interval option * Merge branch 'update_interval' of https://github.com/yuvalabou/feedparser into update_interval
1 parent 0f335a6 commit 83e6fb3

File tree

3 files changed

+25
-21
lines changed

3 files changed

+25
-21
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"python.formatting.provider": "black"
3+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ sensor:
2626
name: Engineering Feed
2727
feed_url: 'https://www.sciencedaily.com/rss/matter_energy/engineering.xml'
2828
date_format: '%a, %b %d %I:%M %p'
29+
scan_interval:
30+
hours: 3
2931
inclusions:
3032
- title
3133
- link
@@ -49,6 +51,7 @@ key | description
4951
**show_topn (Optional)** | fetch how many entres from rss source,if not set then fetch all
5052
**inclusions (Optional)** | List of fields to include from populating the list
5153
**exclusions (Optional)** | List of fields to exclude from populating the list
54+
**scan_interval (Optional)** | Update interval in hours
5255

5356
***
5457

custom_components/feedparser/sensor.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,16 @@
99
import voluptuous as vol
1010
from dateutil import parser
1111
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
12-
from homeassistant.const import CONF_NAME
13-
import homeassistant.util.dt as dt
12+
from homeassistant.const import CONF_NAME, CONF_SCAN_INTERVAL
13+
from homeassistant.core import HomeAssistant
14+
from homeassistant.helpers.entity_platform import AddEntitiesCallback
15+
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
1416

1517
import feedparser
1618

17-
__version__ = "0.1.7"
19+
__version__ = "0.1.6"
20+
21+
COMPONENT_REPO = "https://github.com/custom-components/sensor.feedparser/"
1822

1923
REQUIREMENTS = ["feedparser"]
2024

@@ -23,13 +27,9 @@
2327
CONF_INCLUSIONS = "inclusions"
2428
CONF_EXCLUSIONS = "exclusions"
2529
CONF_SHOW_TOPN = "show_topn"
26-
CONF_LOCAL_TIME = 'local_time'
2730

2831
DEFAULT_SCAN_INTERVAL = timedelta(hours=1)
2932

30-
COMPONENT_REPO = "https://github.com/custom-components/sensor.feedparser/"
31-
SCAN_INTERVAL = timedelta(hours=1)
32-
3333
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
3434
{
3535
vol.Required(CONF_NAME): cv.string,
@@ -38,23 +38,28 @@
3838
vol.Optional(CONF_SHOW_TOPN, default=9999): cv.positive_int,
3939
vol.Optional(CONF_INCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
4040
vol.Optional(CONF_EXCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
41-
vol.Optional(CONF_LOCAL_TIME, default=False): cv.boolean,
41+
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): cv.time_period,
4242
}
4343
)
4444

4545

4646
@asyncio.coroutine
47-
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
47+
def async_setup_platform(
48+
hass: HomeAssistant,
49+
config: ConfigType,
50+
async_add_devices: AddEntitiesCallback,
51+
discovery_info: DiscoveryInfoType | None = None,
52+
) -> None:
4853
async_add_devices(
4954
[
5055
FeedParserSensor(
5156
feed=config[CONF_FEED_URL],
5257
name=config[CONF_NAME],
5358
date_format=config[CONF_DATE_FORMAT],
54-
local_time=config[CONF_LOCAL_TIME],
5559
show_topn=config[CONF_SHOW_TOPN],
5660
inclusions=config[CONF_INCLUSIONS],
5761
exclusions=config[CONF_EXCLUSIONS],
62+
scan_interval=config[CONF_SCAN_INTERVAL],
5863
)
5964
],
6065
True,
@@ -68,20 +73,21 @@ def __init__(
6873
name: str,
6974
date_format: str,
7075
show_topn: str,
71-
local_time: bool,
7276
exclusions: str,
7377
inclusions: str,
78+
scan_interval: int,
7479
) -> None:
7580
self._feed = feed
7681
self._attr_name = name
7782
self._attr_icon = "mdi:rss"
7883
self._date_format = date_format
79-
self._local_time = local_time
8084
self._show_topn = show_topn
8185
self._inclusions = inclusions
8286
self._exclusions = exclusions
87+
self._scan_interval = scan_interval
8388
self._attr_state = None
8489
self._entries = []
90+
self._attr_extra_state_attributes = {"entries": self._entries}
8591

8692
def update(self):
8793
parsed_feed = feedparser.parse(self._feed)
@@ -107,11 +113,7 @@ def update(self):
107113
):
108114
continue
109115
if key in ["published", "updated", "created", "expired"]:
110-
value = parser.parse(value)
111-
if self._local_time:
112-
value = dt.as_local(value)
113-
value = value.strftime(self._date_format)
114-
116+
value = parser.parse(value).strftime(self._date_format)
115117

116118
entry_value[key] = value
117119

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

131133
self._entries.append(entry_value)
132-
133-
@property
134-
def device_state_attributes(self):
135-
return {"entries": self._entries}

0 commit comments

Comments
 (0)