Skip to content

Commit 1875632

Browse files
authored
add ability to convert dates into local timezone (custom-components#49)
1 parent 4ed7685 commit 1875632

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ key | description
4545
**name (Required)** | Name your feed
4646
**feed_url (Required)** | The RSS feed URL
4747
**date_format (Optional)** | strftime date format for date strings **Default** `%a, %b %d %I:%M %p`
48+
**local_time (Optional)** | Whether to convert date into local time **Default** false
4849
**show_topn (Optional)** | fetch how many entres from rss source,if not set then fetch all
4950
**inclusions (Optional)** | List of fields to include from populating the list
5051
**exclusions (Optional)** | List of fields to exclude from populating the list

custom_components/feedparser/sensor.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import homeassistant.helpers.config_validation as cv
1111
from homeassistant.components.sensor import PLATFORM_SCHEMA
1212
from homeassistant.const import CONF_NAME
13+
import homeassistant.util.dt as dt
1314

1415
__version__ = "0.1.2"
1516

@@ -20,6 +21,7 @@
2021
CONF_INCLUSIONS = "inclusions"
2122
CONF_EXCLUSIONS = "exclusions"
2223
CONF_SHOW_TOPN = "show_topn"
24+
CONF_LOCAL_TIME = 'local_time'
2325

2426
DEFAULT_SCAN_INTERVAL = timedelta(hours=1)
2527

@@ -35,6 +37,7 @@
3537
vol.Optional(CONF_SHOW_TOPN, default=9999): cv.positive_int,
3638
vol.Optional(CONF_INCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
3739
vol.Optional(CONF_EXCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
40+
vol.Optional(CONF_LOCAL_TIME, default=False): cv.boolean,
3841
}
3942
)
4043

@@ -47,6 +50,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
4750
feed=config[CONF_FEED_URL],
4851
name=config[CONF_NAME],
4952
date_format=config[CONF_DATE_FORMAT],
53+
local_time=config[CONF_LOCAL_TIME],
5054
show_topn=config[CONF_SHOW_TOPN],
5155
inclusions=config[CONF_INCLUSIONS],
5256
exclusions=config[CONF_EXCLUSIONS],
@@ -63,12 +67,14 @@ def __init__(
6367
name: str,
6468
date_format: str,
6569
show_topn: str,
70+
local_time: bool,
6671
exclusions: str,
6772
inclusions: str,
6873
):
6974
self._feed = feed
7075
self._name = name
7176
self._date_format = date_format
77+
self._local_time = local_time
7278
self._show_topn = show_topn
7379
self._inclusions = inclusions
7480
self._exclusions = exclusions
@@ -99,7 +105,11 @@ def update(self):
99105
):
100106
continue
101107
if key in ["published", "updated", "created", "expired"]:
102-
value = parser.parse(value).strftime(self._date_format)
108+
value = parser.parse(value)
109+
if self._local_time:
110+
value = dt.as_local(value)
111+
value = value.strftime(self._date_format)
112+
103113

104114
entryValue[key] = value
105115

0 commit comments

Comments
 (0)