Skip to content

Commit c4b93a0

Browse files
authored
Use entity class atrributes (#50)
* use _attr * icon * remove typing
1 parent 1875632 commit c4b93a0

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

custom_components/feedparser/sensor.py

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
"""Feedparser sensor"""
2+
from __future__ import annotations
23

34
import asyncio
45
import re
5-
import feedparser
6-
import voluptuous as vol
76
from datetime import timedelta
8-
from dateutil import parser
9-
from homeassistant.components.sensor import SensorEntity
7+
108
import homeassistant.helpers.config_validation as cv
11-
from homeassistant.components.sensor import PLATFORM_SCHEMA
9+
import voluptuous as vol
10+
from dateutil import parser
11+
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
1212
from homeassistant.const import CONF_NAME
1313
import homeassistant.util.dt as dt
1414

15-
__version__ = "0.1.2"
15+
import feedparser
16+
17+
__version__ = "0.1.6"
1618

1719
REQUIREMENTS = ["feedparser"]
1820

@@ -27,7 +29,6 @@
2729

2830
COMPONENT_REPO = "https://github.com/custom-components/sensor.feedparser/"
2931
SCAN_INTERVAL = timedelta(hours=1)
30-
ICON = "mdi:rss"
3132

3233
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
3334
{
@@ -70,32 +71,33 @@ def __init__(
7071
local_time: bool,
7172
exclusions: str,
7273
inclusions: str,
73-
):
74+
) -> None:
7475
self._feed = feed
75-
self._name = name
76+
self._attr_name = name
77+
self._attr_icon = "mdi:rss"
7678
self._date_format = date_format
7779
self._local_time = local_time
7880
self._show_topn = show_topn
7981
self._inclusions = inclusions
8082
self._exclusions = exclusions
81-
self._state = None
83+
self._attr_state = None
8284
self._entries = []
8385

8486
def update(self):
85-
parsedFeed = feedparser.parse(self._feed)
87+
parsed_feed = feedparser.parse(self._feed)
8688

87-
if not parsedFeed:
89+
if not parsed_feed:
8890
return False
8991
else:
90-
self._state = (
92+
self._attr_state = (
9193
self._show_topn
92-
if len(parsedFeed.entries) > self._show_topn
93-
else len(parsedFeed.entries)
94+
if len(parsed_feed.entries) > self._show_topn
95+
else len(parsed_feed.entries)
9496
)
9597
self._entries = []
9698

97-
for entry in parsedFeed.entries[: self._state]:
98-
entryValue = {}
99+
for entry in parsed_feed.entries[: self._attr_state]:
100+
entry_value = {}
99101

100102
for key, value in entry.items():
101103
if (
@@ -111,34 +113,22 @@ def update(self):
111113
value = value.strftime(self._date_format)
112114

113115

114-
entryValue[key] = value
116+
entry_value[key] = value
115117

116-
if "image" in self._inclusions and "image" not in entryValue.keys():
118+
if "image" in self._inclusions and "image" not in entry_value.keys():
117119
images = []
118120
if "summary" in entry.keys():
119121
images = re.findall(
120122
r"<img.+?src=\"(.+?)\".+?>", entry["summary"]
121123
)
122124
if images:
123-
entryValue["image"] = images[0]
125+
entry_value["image"] = images[0]
124126
else:
125-
entryValue[
127+
entry_value[
126128
"image"
127129
] = "https://www.home-assistant.io/images/favicon-192x192-full.png"
128130

129-
self._entries.append(entryValue)
130-
131-
@property
132-
def name(self):
133-
return self._name
134-
135-
@property
136-
def state(self):
137-
return self._state
138-
139-
@property
140-
def icon(self):
141-
return ICON
131+
self._entries.append(entry_value)
142132

143133
@property
144134
def device_state_attributes(self):

0 commit comments

Comments
 (0)