Added DWD WarnApp Sensor#8657
Conversation
|
Hi @runningman84, It seems you haven't yet signed a CLA. Please do so here. Once you do that we will be able to review and accept this pull request. Thanks! |
| data['{}_warning_count'.format(mykey)] = len(my_warnings) | ||
| data['{}_warnings'.format(mykey)] = my_warnings | ||
|
|
||
| _LOGGER.debug("Found {} {} local DWD warnings".format(len(my_warnings), mykey)) |
There was a problem hiding this comment.
line too long (95 > 79 characters)
|
|
||
| # Get max warning level | ||
| for event in my_warnings: | ||
| if(event['level'] >= data['{}_warning_level'.format(mykey)]): |
There was a problem hiding this comment.
line too long (81 > 79 characters)
| continue | ||
| my_warnings = json_obj[myvalue][key] | ||
| self.region_id = key | ||
| self.region_state = json_obj[myvalue][key][0]['stateShort'] |
There was a problem hiding this comment.
line too long (83 > 79 characters)
| else: | ||
| # loop through all items to find warnings, region_id and region_state for region_name | ||
| for key in json_obj[myvalue]: | ||
| if json_obj[myvalue][key][0]['regionName'] != self.region_name: |
There was a problem hiding this comment.
line too long (87 > 79 characters)
| my_warnings = json_obj[myvalue][self.region_id] | ||
|
|
||
| else: | ||
| # loop through all items to find warnings, region_id and region_state for region_name |
There was a problem hiding this comment.
line too long (105 > 79 characters)
| # data['region_map_url'] = 'https://www.dwd.de/DWD/warnungen/warnapp_gemeinden/json/warnungen_gemeinde_map_' + str(data['region_state'].lower()) + '.png' | ||
|
|
||
| if self._api.data['time'] is not None: | ||
| data['last_update'] = dt_util.as_local(dt_util.utc_from_timestamp(self._api.data['time'] / 1000)) |
There was a problem hiding this comment.
line too long (110 > 79 characters)
multiple spaces before operator
|
|
||
| if self._api.region_state is not None: | ||
| data['region_state'] = self._api.region_state | ||
| # data['region_map_url'] = 'https://www.dwd.de/DWD/warnungen/warnapp_gemeinden/json/warnungen_gemeinde_map_' + str(data['region_state'].lower()) + '.png' |
There was a problem hiding this comment.
line too long (165 > 79 characters)
| if self._api.region_id is not None: | ||
| data['region_id'] = self._api.region_id | ||
|
|
||
| if self._api.region_state is not None: |
| data = {} | ||
| data['region_name'] = self._api.region_name | ||
|
|
||
| if self._api.region_id is not None: |
| 'current_warning_level': ['Current Warning Level', | ||
| None, 'mdi:close-octagon-outline'], | ||
| 'advance_warning_level': ['Advance Warning Level', | ||
| None, 'mdi:close-octagon-outline'], |
There was a problem hiding this comment.
continuation line under-indented for visual indent
| import homeassistant.util.dt as dt_util | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
| _ENDPOINT = '/DWD/warnungen/warnapp_landkreise/json/warnings.json?jsonp=loadWarnings' |
There was a problem hiding this comment.
line too long (85 > 79 characters)
There was a problem hiding this comment.
how ca we fix this one? I cannot change the corresponding url, splitting it into multiple lines seems odd to me.
There was a problem hiding this comment.
Fix it by not hardcoding the url arguments in the endpoint.
| import logging | ||
| import json | ||
| import time | ||
| import datetime |
| """ | ||
| import logging | ||
| import json | ||
| import time |
|
|
||
| if self._api.region_state is not None: | ||
| data['region_state'] = self._api.region_state | ||
| # data['region_map_url'] = 'https://www.dwd.de/DWD/warnungen/warnapp_gemeinden/json/warnungen_gemeinde_map_' + str(data['region_state'].lower()) + '.png' |
There was a problem hiding this comment.
line too long (165 > 79 characters)
balloob
left a comment
There was a problem hiding this comment.
There are a lot of options in here that I don't really see why they would make sense for querying a web service (verify SSL, use SSL etc)
| DEFAULT_METHOD = 'GET' | ||
| DEFAULT_NAME = 'DWD-Warnapp' | ||
| DEFAULT_SSL = True | ||
| DEFAULT_VERIFY_SSL = True |
There was a problem hiding this comment.
Why did you implement this option? Doesn't it default fetch dwd.de which I assume has SSL working?
| self.available = True | ||
| self.update() | ||
|
|
||
| #@Throttle(SCAN_INTERVAL) |
There was a problem hiding this comment.
You will need to implement throttle or you're hammering their API
| """Initialize the data object.""" | ||
| from homeassistant.components.sensor.rest import RestData | ||
|
|
||
| uri_scheme = 'https://' if use_ssl else 'http://' |
There was a problem hiding this comment.
Why is this an option? Isn't the website always the same
| self.available = True | ||
| self.update() | ||
|
|
||
| @Throttle(SCAN_INTERVAL) |
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.entity import Entity | ||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| from homeassistant.const import ( |
There was a problem hiding this comment.
'homeassistant.const.CONF_HOST' imported but unused
'homeassistant.const.CONF_SSL' imported but unused
'homeassistant.const.CONF_VERIFY_SSL' imported but unused
|
|
||
| # Get max warning level | ||
| for event in my_warnings: | ||
| if(event['level'] >= data['{}_warning_level'.format(mykey)]): |
| continue | ||
| my_warnings = json_obj[myvalue][key] | ||
| self.region_id = key | ||
| self.region_state = json_obj[myvalue][key][0]['stateShort'] |
| else: | ||
| # loop through all items to find warnings, region_id and region_state for region_name | ||
| for key in json_obj[myvalue]: | ||
| if json_obj[myvalue][key][0]['regionName'] != self.region_name: |
| my_warnings = json_obj[myvalue][self.region_id] | ||
|
|
||
| else: | ||
| # loop through all items to find warnings, region_id and region_state for region_name |
There was a problem hiding this comment.
line too long (105 > 79 characters)
|
|
||
| data['time'] = json_obj['time'] | ||
|
|
||
| for mykey, myvalue in {'current': 'warnings', 'advance': 'vorabInformation'}.items(): |
|
|
||
| if self._api.region_state is not None: | ||
| data['region_state'] = self._api.region_state | ||
| # data['region_map_url'] = 'https://www.dwd.de/DWD/warnungen/warnapp_gemeinden/json/warnungen_gemeinde_map_' + str(data['region_state'].lower()) + '.png' |
There was a problem hiding this comment.
line too long (165 > 79 characters)
|
@balloob I added the throttle function and renamed it to dwd weather warnings. |
if (event['level'] >=
data['{}_warning_level'.format(mykey)]): |
|
Yes, it is a hard limit. |
| # loop through all items to find warnings, region_id | ||
| # and region_state for region_name | ||
| for key in json_obj[myvalue]: | ||
| if json_obj[myvalue][key][0]['regionName'] != self.region_name: |
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| DEFAULT_NAME = 'DWD-Wetter-Warnungen' |
There was a problem hiding this comment.
Should be in English. If the user can set name: if he/she want it in German.
| DEFAULT_NAME = 'DWD-Wetter-Warnungen' | ||
|
|
||
| CONF_REGION_NAME = 'region_name' | ||
| DEFAULT_REGION_NAME = 'Hansestadt Hamburg' |
There was a problem hiding this comment.
This default does only make sense for a small group of users.
There was a problem hiding this comment.
I will remove the default region
|
|
||
| # pylint: disable=no-member | ||
| @property | ||
| def device_state_attributes(self): |
There was a problem hiding this comment.
Can you please add an attribution?
There was a problem hiding this comment.
what do you mean?
|
|
||
| def __init__(self, hass, api, name, variable): | ||
| """Initialize a DWD-Weather-Warnings sensor.""" | ||
| self._hass = hass |
There was a problem hiding this comment.
You doesn't seems to use self._hass.
There was a problem hiding this comment.
I will remove it
|
|
||
| def __init__(self, region_name): | ||
| """Initialize the data object.""" | ||
| from homeassistant.components.sensor.rest import RestData |
There was a problem hiding this comment.
Please place this in the file header.
| return self._api.data[self._var_id] | ||
|
|
||
| # pylint: disable=no-member | ||
| @property |
There was a problem hiding this comment.
redefinition of unused 'device_state_attributes' from line 96
|
@fabaff can you accept the pull request or is still something missing? |
|
There are still lint issues. |
| data['{}_warnings'.format(mykey)] = my_warnings | ||
|
|
||
| _LOGGER.debug("Found %d %s local DWD warnings", | ||
| len(my_warnings), mykey) |
There was a problem hiding this comment.
continuation line under-indented for visual indent
| }.items(): | ||
|
|
||
| _LOGGER.debug("Found %d %s global DWD warnings", | ||
| len(json_obj[myvalue]), mykey) |
There was a problem hiding this comment.
continuation line under-indented for visual indent
| elif self._var_id == 'advance_warning_level': | ||
| prefix = 'advance' | ||
|
|
||
| data['warning_count'] = self._api.data[prefix + '_warning_count'] |
There was a problem hiding this comment.
Local variable 'prefix' might be referenced before assignment. Compare L122, L124.
There was a problem hiding this comment.
ok I will add a else path and throw an exception there...
|
|
||
| def __init__(self, region_name): | ||
| """Initialize the data object.""" | ||
|
|
| json_string = self._rest.data[24:len(self._rest.data) - 2] | ||
| json_obj = json.loads(json_string) | ||
|
|
||
| data = {} |
There was a problem hiding this comment.
You could save a line here. My IDE complains about. ;-)
- data = {}
-
- data['time'] = json_obj['time']
+ data = {'time': json_obj['time']}| @property | ||
| def device_state_attributes(self): | ||
| """Return the state attributes of the DWD-Weather-Warnings.""" | ||
| data = {} |
There was a problem hiding this comment.
Same here:
data = {
ATTR_ATTRIBUTION: ATTRIBUTION,
'region_name': self._api.region_name,
}
|
Good job! 👍 |
|
Thanks! 🐬 |
Description:
This sensor adds support for current and advance weather warnings from DWD (Deutsche Wetter Dienst) for Germany. This sensor uses the same data as the WarnWetter Android/IOS App.
The warning level is between 0 (no danger) and 4:
It can be a bit tricky to find out the correct region name because the source json files only shows a region if a current or advance warning is there:
https://www.dwd.de/DWD/warnungen/warnapp_landkreise/json/warnings.json?jsonp=loadWarnings
The region_name should match the name of the regions listed on this page:
https://www.dwd.de/DE/wetter/warnungen_landkreise/warnWetter_node.html?ort=Hamburg
Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#3062
Example entry for
configuration.yaml(if applicable):Here is a screenshot:
https://community.home-assistant.io/t/dwd-warnapp-sensor-amtliche-warnungen-des-deutschen-wetterdienstes/22699/1
Checklist:
If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
toxrun successfully. Your PR cannot be merged unless tests passREQUIREMENTSvariable (example).requirements_all.txtby runningscript/gen_requirements_all.py..coveragerc.If the code does not interact with devices:
toxrun successfully. Your PR cannot be merged unless tests pass