From 4d9f6949519a4379a49d7f96ffe7a40beab9e8ad Mon Sep 17 00:00:00 2001 From: Job van Koeveringe Date: Tue, 6 Sep 2022 12:42:55 +0300 Subject: [PATCH] Add News Sensor + Remove News Filter --- custom_components/windcentrale/config_flow.py | 1 - custom_components/windcentrale/manifest.json | 2 +- custom_components/windcentrale/sensor.py | 32 ++++--- custom_components/windcentrale/strings.json | 1 - .../windcentrale/translations/de.json | 1 - .../windcentrale/translations/en.json | 1 - .../windcentrale/translations/nl.json | 1 - custom_components/windcentrale/wind.py | 85 ++++++------------- 8 files changed, 49 insertions(+), 75 deletions(-) diff --git a/custom_components/windcentrale/config_flow.py b/custom_components/windcentrale/config_flow.py index 8cba3f0..29bb13c 100644 --- a/custom_components/windcentrale/config_flow.py +++ b/custom_components/windcentrale/config_flow.py @@ -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) diff --git a/custom_components/windcentrale/manifest.json b/custom_components/windcentrale/manifest.json index 0a67dc6..b86ceb8 100644 --- a/custom_components/windcentrale/manifest.json +++ b/custom_components/windcentrale/manifest.json @@ -8,6 +8,6 @@ "iot_class": "cloud_polling", "dependencies": [], "codeowners": ["@jobvk"], - "version": "0.2.2", + "version": "0.2.3", "loggers": ["boto3"] } \ No newline at end of file diff --git a/custom_components/windcentrale/sensor.py b/custom_components/windcentrale/sensor.py index 2184f9d..e685d07 100644 --- a/custom_components/windcentrale/sensor.py +++ b/custom_components/windcentrale/sensor.py @@ -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) @@ -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: @@ -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: @@ -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 \ No newline at end of file + if self.wind.news_data is not None: + self.news_item = self.wind.news_data \ No newline at end of file diff --git a/custom_components/windcentrale/strings.json b/custom_components/windcentrale/strings.json index 6016253..fdbc8bd 100644 --- a/custom_components/windcentrale/strings.json +++ b/custom_components/windcentrale/strings.json @@ -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" } } diff --git a/custom_components/windcentrale/translations/de.json b/custom_components/windcentrale/translations/de.json index 61140c7..0e5ab53 100644 --- a/custom_components/windcentrale/translations/de.json +++ b/custom_components/windcentrale/translations/de.json @@ -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" } } diff --git a/custom_components/windcentrale/translations/en.json b/custom_components/windcentrale/translations/en.json index 6016253..fdbc8bd 100644 --- a/custom_components/windcentrale/translations/en.json +++ b/custom_components/windcentrale/translations/en.json @@ -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" } } diff --git a/custom_components/windcentrale/translations/nl.json b/custom_components/windcentrale/translations/nl.json index df36d22..bed032f 100644 --- a/custom_components/windcentrale/translations/nl.json +++ b/custom_components/windcentrale/translations/nl.json @@ -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" } } diff --git a/custom_components/windcentrale/wind.py b/custom_components/windcentrale/wind.py index f558e46..c1a7874 100644 --- a/custom_components/windcentrale/wind.py +++ b/custom_components/windcentrale/wind.py @@ -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]) @@ -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" @@ -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: