Skip to content

Commit

Permalink
Add News Sensor + Remove News Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
jobvk committed Sep 6, 2022
1 parent e7fa282 commit 4d9f694
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 75 deletions.
1 change: 0 additions & 1 deletion custom_components/windcentrale/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ async def async_step_init(self, user_input=None):
errors["base"] = "unknown"

return self.async_show_form(step_id="init", data_schema= vol.Schema({
vol.Optional(CONF_NEWS_FILTER, default=self.config_entry.options.get(CONF_NEWS_FILTER, NEWS_FILTER[0])): vol.In(NEWS_FILTER),
vol.Optional(CONF_SHOW_ON_MAP, default=self.config_entry.options.get(CONF_SHOW_ON_MAP, DEFAULT_SHOW_ON_MAP)): bool
}),
errors=errors)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/windcentrale/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"iot_class": "cloud_polling",
"dependencies": [],
"codeowners": ["@jobvk"],
"version": "0.2.2",
"version": "0.2.3",
"loggers": ["boto3"]
}
32 changes: 20 additions & 12 deletions custom_components/windcentrale/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
await windturbine.schedule_update_live(timedelta())
# await windturbine.schedule_update_production(timedelta())

# new_entities.append(NewsSensor(wind))
# await wind.schedule_update_news(timedelta())
new_entities.append(NewsSensor(wind))
await wind.schedule_update_news(timedelta())

if new_entities:
async_add_entities(new_entities)
Expand Down Expand Up @@ -228,11 +228,11 @@ def update(self):
else:
return None

class NewsSensor(SensorEntity):
class NewsSensor(RestoreEntity, SensorEntity):
def __init__(self, wind):
"""Initialize the sensor."""
self.wind = wind
self._item = None
self.news_item = ""

@property
def unique_id(self) -> str:
Expand All @@ -247,7 +247,7 @@ def name(self) -> str:
@property
def state(self) -> str:
"""Static news value for the news sensor."""
return "News"
return self._state

@property
def icon(self) -> str:
Expand All @@ -258,18 +258,26 @@ def icon(self) -> str:
def extra_state_attributes(self):
"""Return the state attributes of the entity."""
attr = {}
attr["News Item"] = self._item
attr["News Item"] = self.news_item
return attr

@property
def available(self) -> bool:
"""Return true if windturbine news sensor is available."""
return self.wind.news_status
return True

async def async_added_to_hass(self):
"""Call when entity is about to be added to Home Assistant."""
if (state := await self.async_get_last_state()) is None:
self._state = "News"
return

self._state = state.state

if "News Item" in state.attributes:
self.news_item = state.attributes["News Item"]

def update(self):
"""Update the sensor."""
if self.wind.news_data:
self._item = self.wind.news_data
return self._item
else:
return None
if self.wind.news_data is not None:
self.news_item = self.wind.news_data
1 change: 0 additions & 1 deletion custom_components/windcentrale/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"title": "The Windcentrale Options",
"description": "Options for the Windcentrale integration. https://www.windcentrale.nl\n\nIf you need help with the configuration, have a look over here: https://github.com/jobvk/Home-Assistant-Windcentrale\n",
"data": {
"news_filter": "Filter for the news of the windturbines(s)",
"show_on_map": "Show the windturbine(s) on the map"
}
}
Expand Down
1 change: 0 additions & 1 deletion custom_components/windcentrale/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"title": "De Windcentrale Anpassen",
"description": "Optionen für die Windcentrale Integration. https://www.windcentrale.nl\n\nWenn man Hilfe braucht bei der Konfiguration, schau dann mal hier: https://github.com/jobvk/Home-Assistant-Windcentrale\n",
"data": {
"news_filter": "Filter für die Nachrichten der Windkraftanlage(n)",
"show_on_map": "Zeige die Windkraftanlage(n) auf der Karte"
}
}
Expand Down
1 change: 0 additions & 1 deletion custom_components/windcentrale/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"title": "The Windcentrale Options",
"description": "Options for the Windcentrale integration. https://www.windcentrale.nl\n\nIf you need help with the configuration, have a look over here: https://github.com/jobvk/Home-Assistant-Windcentrale\n",
"data": {
"news_filter": "Filter for the news of the windturbines(s)",
"show_on_map": "Show the windturbine(s) on the map"
}
}
Expand Down
1 change: 0 additions & 1 deletion custom_components/windcentrale/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"title": "De Windcentrale Aanpassen",
"description": "Opties voor de Windcentrale integratie. https://www.windcentrale.nl\n\nAls je hulp nodig hebt bij de configuratie, kijk dan hier: https://github.com/jobvk/Home-Assistant-Windcentrale\n",
"data": {
"news_filter": "Filter voor het nieuws van de windturbine(s)",
"show_on_map": "Laat de windturbine(s) op de kaart zien"
}
}
Expand Down
85 changes: 28 additions & 57 deletions custom_components/windcentrale/wind.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def __init__(self, hass, config_entry):
self.hass = hass
self.id = DOMAIN
self.tokens = None
self.news_filter = self.config_entry.options.get(CONF_NEWS_FILTER, DEFAULT_NEWS_FILTER)
self.show_on_map = self.config_entry.options.get(CONF_SHOW_ON_MAP, DEFAULT_SHOW_ON_MAP)
self.credentialsapi = Credentials(self.hass, self.config_entry.data[CONF_EMAIL], self.config_entry.data[CONF_PASSWORD])

Expand All @@ -31,27 +30,22 @@ def __init__(self, hass, config_entry):
project = json.loads(self.config_entry.data[windturbine])
self.windturbines.append(Windturbine(self, self.hass, project["name"], project["code"], project["shares"]))

# self.newsapi = NewsAPI(self.hass, self.news_filter, self.windturbines)
self.newsapi = NewsAPI(self, self.hass)

# @property
# def news_status(self):
# "Set news status as result of api"
# return self.newsapi.status

# @property
# def news_data(self):
# "Set news data form news api result"
# return self.newsapi.response_data
@property
def news_data(self):
"Set news data form news api result"
return self.newsapi.response_data

# async def schedule_update_news(self, interval):
# "Schedule update based on news interval"
# nxt = dt_util.utcnow() + interval
# async_track_point_in_utc_time(self.hass, self.async_update_news, nxt)
async def schedule_update_news(self, interval):
"Schedule update based on news interval"
nxt = dt_util.utcnow() + interval
async_track_point_in_utc_time(self.hass, self.async_update_news, nxt)

# async def async_update_news(self, *_):
# "Start update and schedule update based on news interval"
# await self.newsapi.update()
# await self.schedule_update_news(timedelta(minutes=NEWS_INTERVAL))
async def async_update_news(self, *_):
"Start update and schedule update based on news interval"
await self.newsapi.update()
await self.schedule_update_news(timedelta(minutes=NEWS_INTERVAL))

async def schedule_update_token(self, interval):
"Schedule update based on token interval"
Expand Down Expand Up @@ -259,72 +253,49 @@ async def update(self):

class NewsAPI:
"Collect news data"
def __init__(self, hass, news_filter, windturbines):
def __init__(self, wind, hass):
self.wind = wind
self.hass = hass
self.news_filter = news_filter
self.response_data = None
self.windturbines_ids = []
self.status = None
self.main_url = "https://zep-api.windcentrale.nl/app/config"
for windturbine in windturbines:
self.windturbines_ids.append(windturbine.id)
self.response_data = ""
self.main_url = "https://mijn.windcentrale.nl/api/v0/sustainable/notices"

def __get_data(self):
"Collect data form url"
return requests.get(self.main_url, verify=True)
return requests.get(self.main_url, headers=self.wind.tokens, verify=True)

async def update(self):
"Get data ready for home assitant"
_LOGGER.info('Updating news data sensor')

try:
if self.wind.tokens is None:
return

request_data = await self.hass.async_add_executor_job(self.__get_data)

if not request_data.status_code == HTTPStatus.OK:
_LOGGER.error('Invalid response from server for collection news data')
self.status = False
return

if request_data.text == "":
_LOGGER.error('No news data found')
self.status = False
return

self.response_data = None
response_data_list = []

root = ElementTree.fromstring(request_data.text)
for newsitems in root.findall('./news/'):
value_data = {}
value_data[0] = newsitems.attrib.get('id')
value_data[1] = newsitems.attrib.get('i')
value_data[2] = int(newsitems.attrib.get('m'))
Windturbine_id = int(newsitems.attrib.get('m'))
value_data[3] = newsitems.attrib.get('t')
value_data[4] = newsitems.find('t').text
value_data[5] = newsitems.find('c').text
if self.news_filter == NEWS_FILTER[0]:
response_data_list.append(value_data)
elif self.news_filter == NEWS_FILTER[1]:
if Windturbine_id == 0 or Windturbine_id in self.windturbines_ids:
response_data_list.append(value_data)
elif self.news_filter == NEWS_FILTER[2]:
if Windturbine_id in self.windturbines_ids:
response_data_list.append(value_data)

first_item = response_data_list[0]
self.response_data = '{}\n---------\n{}\n\nPublicatiedatum: {}'.format(first_item[4], first_item[5], first_item[3])
self.status = True
self.response_data = ""
json_items = json.loads(json.dumps(request_data.json()))
news_item = json_items[0]
publication_date = datetime.datetime.fromisoformat(news_item['publication_date_time']).strftime("%d-%m-%Y %H:%M:%S")
self.response_data = "{}\n---------\n{}\n\nPublicatiedatum: {}".format(news_item['title'], news_item['message'], publication_date)
_LOGGER.error(self.response_data)

except requests.exceptions.Timeout:
"""Time out error of server connection"""
_LOGGER.error('Timeout response from server for collection news data')
self.status = False
return

except requests.exceptions.RequestException as exc:
"""Error of server RequestException"""
_LOGGER.error('Error occurred while fetching data: %r', exc)
self.status = False
return

class Credentials:
Expand Down

0 comments on commit 4d9f694

Please sign in to comment.