Skip to content

Commit 658a879

Browse files
authored
Merge pull request #72 from jeroenvanasten/master
Fix for Python 3.11 and HA 2023.6.x and bringing back local_time
2 parents f34ff66 + 2f7f4fb commit 658a879

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

custom_components/feedparser/sensor.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from homeassistant.core import HomeAssistant
1414
from homeassistant.helpers.entity_platform import AddEntitiesCallback
1515
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
16+
import homeassistant.util.dt as dt
1617

1718
import feedparser
1819

@@ -24,6 +25,7 @@
2425

2526
CONF_FEED_URL = "feed_url"
2627
CONF_DATE_FORMAT = "date_format"
28+
CONF_LOCAL_TIME = "local_time"
2729
CONF_INCLUSIONS = "inclusions"
2830
CONF_EXCLUSIONS = "exclusions"
2931
CONF_SHOW_TOPN = "show_topn"
@@ -35,6 +37,7 @@
3537
vol.Required(CONF_NAME): cv.string,
3638
vol.Required(CONF_FEED_URL): cv.string,
3739
vol.Required(CONF_DATE_FORMAT, default="%a, %b %d %I:%M %p"): cv.string,
40+
vol.Optional(CONF_LOCAL_TIME, default=False): cv.boolean,
3841
vol.Optional(CONF_SHOW_TOPN, default=9999): cv.positive_int,
3942
vol.Optional(CONF_INCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
4043
vol.Optional(CONF_EXCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
@@ -43,8 +46,8 @@
4346
)
4447

4548

46-
@asyncio.coroutine
47-
def async_setup_platform(
49+
"""@asyncio.coroutine"""
50+
async def async_setup_platform(
4851
hass: HomeAssistant,
4952
config: ConfigType,
5053
async_add_devices: AddEntitiesCallback,
@@ -56,6 +59,7 @@ def async_setup_platform(
5659
feed=config[CONF_FEED_URL],
5760
name=config[CONF_NAME],
5861
date_format=config[CONF_DATE_FORMAT],
62+
local_time=config[CONF_LOCAL_TIME],
5963
show_topn=config[CONF_SHOW_TOPN],
6064
inclusions=config[CONF_INCLUSIONS],
6165
exclusions=config[CONF_EXCLUSIONS],
@@ -72,6 +76,7 @@ def __init__(
7276
feed: str,
7377
name: str,
7478
date_format: str,
79+
local_time: bool,
7580
show_topn: str,
7681
exclusions: str,
7782
inclusions: str,
@@ -82,6 +87,7 @@ def __init__(
8287
self._attr_icon = "mdi:rss"
8388
self._date_format = date_format
8489
self._show_topn = show_topn
90+
self._local_time = local_time
8591
self._inclusions = inclusions
8692
self._exclusions = exclusions
8793
self._scan_interval = scan_interval
@@ -113,7 +119,10 @@ def update(self):
113119
):
114120
continue
115121
if key in ["published", "updated", "created", "expired"]:
116-
value = parser.parse(value).strftime(self._date_format)
122+
value = parser.parse(value)
123+
if self._local_time:
124+
value = dt.as_local(value)
125+
value = value.strftime(self._date_format)
117126

118127
entry_value[key] = value
119128

0 commit comments

Comments
 (0)