Skip to content

Commit c46d418

Browse files
aalavenderiantrich
authored andcommitted
async_setup_platform (#22)
* async_setup_platform * async_setup_platform
1 parent 675378d commit c46d418

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

custom_components/feedparser/sensor.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
77
Following spec from https://validator.w3.org/feed/docs/rss2.html
88
"""
9+
import asyncio
910
import re
1011
import feedparser
1112
import logging
1213
import voluptuous as vol
1314
from datetime import timedelta
1415
from dateutil import parser
15-
from time import strftime
16-
from subprocess import check_output
1716
from homeassistant.helpers.entity import Entity
1817
import homeassistant.helpers.config_validation as cv
1918
from homeassistant.components.sensor import (PLATFORM_SCHEMA)
@@ -45,21 +44,29 @@
4544
vol.Optional(CONF_EXCLUSIONS, default=[]): vol.All(cv.ensure_list, [cv.string]),
4645
})
4746

48-
def setup_platform(hass, config, add_devices, discovery_info=None):
49-
add_devices([FeedParserSensor(hass, config)])
47+
48+
@asyncio.coroutine
49+
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
50+
async_add_devices([FeedParserSensor(
51+
feed=config[CONF_FEED_URL],
52+
name=config[CONF_NAME],
53+
date_format=config[CONF_DATE_FORMAT],
54+
show_topn=config[CONF_SHOW_TOPN],
55+
inclusions=config[CONF_INCLUSIONS],
56+
exclusions=config[CONF_EXCLUSIONS]
57+
)], True)
58+
5059

5160
class FeedParserSensor(Entity):
52-
def __init__(self, hass, config):
53-
self.hass = hass
54-
self._feed = config[CONF_FEED_URL]
55-
self._name = config[CONF_NAME]
56-
self._date_format = config[CONF_DATE_FORMAT]
57-
self._show_topn = config[CONF_SHOW_TOPN]
58-
self._inclusions = config[CONF_INCLUSIONS]
59-
self._exclusions = config[CONF_EXCLUSIONS]
61+
def __init__(self, feed: str, name: str, date_format: str, show_topn: str, exclusions: str, inclusions:str):
62+
self._feed = feed
63+
self._name = name
64+
self._date_format = date_format
65+
self._show_topn = show_topn
66+
self._inclusions = inclusions
67+
self._exclusions = exclusions
6068
self._state = None
6169
self._entries = []
62-
self.update()
6370

6471
def update(self):
6572
parsedFeed = feedparser.parse(self._feed)

0 commit comments

Comments
 (0)