-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Added DWD WarnApp Sensor #8657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added DWD WarnApp Sensor #8657
Changes from 2 commits
1af723f
7cb7924
17cc1a7
7b3e84c
a85e8d5
d9da925
d2f6676
df60b1a
deb31e7
5c270a4
7426e3e
43cc842
8a510f7
339b097
8095761
0e0a9e9
70d0195
0940766
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,245 @@ | ||
| """ | ||
| Support for getting statistical data from a DWD-Warnapp system. | ||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.dwd_warnapp/ | ||
|
|
||
| Data is fetched from DWD: | ||
| https://rcccm.dwd.de/DE/wetter/warnungen_aktuell/objekt_einbindung/objekteinbindung.html | ||
|
|
||
| Warnungen vor extremem Unwetter (Stufe 4) | ||
| Unwetterwarnungen (Stufe 3) | ||
| Warnungen vor markantem Wetter (Stufe 2) | ||
| Wetterwarnungen (Stufe 1) | ||
| """ | ||
| import logging | ||
| import json | ||
| import time | ||
| import datetime | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'datetime' imported but unused |
||
| from datetime import timedelta | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'homeassistant.const.CONF_HOST' imported but unused |
||
| CONF_NAME, CONF_HOST, CONF_SSL, CONF_VERIFY_SSL, CONF_MONITORED_CONDITIONS) | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (85 > 79 characters)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how ca we fix this one? I cannot change the corresponding url, splitting it into multiple lines seems odd to me.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix it by not hardcoding the url arguments in the endpoint. |
||
|
|
||
| DEFAULT_HOST = 'www.dwd.de' | ||
|
|
||
| DEFAULT_METHOD = 'GET' | ||
| DEFAULT_NAME = 'DWD-Warnapp' | ||
| DEFAULT_SSL = True | ||
| DEFAULT_VERIFY_SSL = True | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you implement this option? Doesn't it default fetch |
||
|
|
||
| CONF_REGION_NAME = 'region_name' | ||
| DEFAULT_REGION_NAME = 'Hansestadt Hamburg' | ||
|
|
||
| SCAN_INTERVAL = timedelta(minutes=15) | ||
|
|
||
| MONITORED_CONDITIONS = { | ||
| 'current_warning_level': ['Current Warning Level', | ||
| None, 'mdi:close-octagon-outline'], | ||
| 'advance_warning_level': ['Advance Warning Level', | ||
| None, 'mdi:close-octagon-outline'], | ||
| } | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Optional(CONF_REGION_NAME, default=DEFAULT_REGION_NAME): cv.string, | ||
| vol.Optional(CONF_HOST, default=DEFAULT_HOST): cv.string, | ||
| vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string, | ||
| vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean, | ||
| vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean, | ||
| vol.Optional(CONF_MONITORED_CONDITIONS, default=MONITORED_CONDITIONS): | ||
| vol.All(cv.ensure_list, [vol.In(MONITORED_CONDITIONS)]), | ||
| }) | ||
|
|
||
|
|
||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Set up the DWD-Warnapp sensor.""" | ||
| name = config.get(CONF_NAME) | ||
| host = config.get(CONF_HOST) | ||
| use_ssl = config.get(CONF_SSL) | ||
| verify_ssl = config.get(CONF_VERIFY_SSL) | ||
| region_name = config.get(CONF_REGION_NAME) | ||
|
|
||
| api = DwdWarnappAPI(host, use_ssl, verify_ssl, region_name) | ||
|
|
||
| sensors = [DwdWarnappSensor(hass, api, name, condition) | ||
| for condition in config[CONF_MONITORED_CONDITIONS]] | ||
|
|
||
| add_devices(sensors, True) | ||
|
|
||
|
|
||
| class DwdWarnappSensor(Entity): | ||
| """Representation of a DWD-Warnapp sensor.""" | ||
|
|
||
| def __init__(self, hass, api, name, variable): | ||
| """Initialize a DWD-Warnapp sensor.""" | ||
| self._hass = hass | ||
| self._api = api | ||
| self._name = name | ||
| self._var_id = variable | ||
|
|
||
| variable_info = MONITORED_CONDITIONS[variable] | ||
| self._var_name = variable_info[0] | ||
| self._var_units = variable_info[1] | ||
| self._var_icon = variable_info[2] | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return "{} {}".format(self._name, self._var_name) | ||
|
|
||
| @property | ||
| def icon(self): | ||
| """Icon to use in the frontend, if any.""" | ||
| return self._var_icon | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit the value is expressed in.""" | ||
| return self._var_units | ||
|
|
||
| # pylint: disable=no-member | ||
| @property | ||
| def state(self): | ||
| """Return the state of the device.""" | ||
| try: | ||
| return round(self._api.data[self._var_id], 2) | ||
| except TypeError: | ||
| return self._api.data[self._var_id] | ||
|
|
||
| # pylint: disable=no-member | ||
| @property | ||
| def device_state_attributes(self): | ||
| """Return the state attributes of the DWD-Warnapp.""" | ||
|
|
||
| data = {} | ||
| data['region_name'] = self._api.region_name | ||
|
|
||
| if self._api.region_id is not None: | ||
| data['region_id'] = self._api.region_id | ||
|
|
||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (165 > 79 characters) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (165 > 79 characters) |
||
|
|
||
| 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)) | ||
|
|
||
| if self._var_id == 'current_warning_level': | ||
| prefix = 'current' | ||
| elif self._var_id == 'advance_warning_level': | ||
| prefix = 'advance' | ||
|
|
||
| data['warning_count'] = self._api.data[prefix + '_warning_count'] | ||
| i = 0 | ||
| for event in self._api.data[prefix + '_warnings']: | ||
| i = i + 1 | ||
|
|
||
| data['warning_{}_name'.format(i)] = event['event'] | ||
| data['warning_{}_level'.format(i)] = event['level'] | ||
| data['warning_{}_type'.format(i)] = event['type'] | ||
| if len(event['headline']) > 0: | ||
| data['warning_{}_headline'.format(i)] = event['headline'] | ||
| if len(event['description']) > 0: | ||
| data['warning_{}_description'.format(i)] = event['description'] | ||
| if len(event['instruction']) > 0: | ||
| data['warning_{}_instruction'.format(i)] = event['instruction'] | ||
|
|
||
| if event['start'] is not None: | ||
| data['warning_{}_start'.format(i)] = dt_util.as_local( | ||
| dt_util.utc_from_timestamp(event['start'] / 1000)) | ||
|
|
||
| if event['end'] is not None: | ||
| data['warning_{}_end'.format(i)] = dt_util.as_local( | ||
| dt_util.utc_from_timestamp(event['end'] / 1000)) | ||
|
|
||
| return data | ||
|
|
||
| @property | ||
| def available(self): | ||
| """Could the device be accessed during the last update call.""" | ||
| return self._api.available | ||
|
|
||
| def update(self): | ||
| """Get the latest data from the DWD-Warnapp API.""" | ||
| self._api.update() | ||
|
|
||
|
|
||
| class DwdWarnappAPI(object): | ||
| """Get the latest data and update the states.""" | ||
|
|
||
| def __init__(self, host, use_ssl, verify_ssl, region_name): | ||
| """Initialize the data object.""" | ||
| from homeassistant.components.sensor.rest import RestData | ||
|
|
||
| uri_scheme = 'https://' if use_ssl else 'http://' | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this an option? Isn't the website always the same |
||
| resource = "{}{}{}".format(uri_scheme, host, _ENDPOINT) | ||
|
|
||
| self._rest = RestData('GET', resource, None, None, None, verify_ssl) | ||
| self.region_name = region_name | ||
| self.region_id = None | ||
| self.region_state = None | ||
| self.data = None | ||
| self.available = True | ||
| self.update() | ||
|
|
||
| #@Throttle(SCAN_INTERVAL) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. block comment should start with '# '
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will need to implement throttle or you're hammering their API |
||
| def update(self): | ||
| """Get the latest data from the DWD-Warnapp.""" | ||
| try: | ||
| self._rest.update() | ||
|
|
||
| json_string = self._rest.data[24:len(self._rest.data) - 2] | ||
| json_obj = json.loads(json_string) | ||
|
|
||
| data = {} | ||
|
|
||
| data['time'] = json_obj['time'] | ||
|
|
||
| for mykey, myvalue in {'current': 'warnings', 'advance': 'vorabInformation'}.items(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (97 > 79 characters) |
||
|
|
||
| _LOGGER.debug("Found {} {} global DWD warnings".format( | ||
| len(json_obj[myvalue]), mykey)) | ||
|
|
||
| data['{}_warning_level'.format(mykey)] = 0 | ||
| my_warnings = [] | ||
|
|
||
| if self.region_id is not None: | ||
| # get a specific region_id | ||
| if self.region_id in json_obj[myvalue]: | ||
| 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (105 > 79 characters) |
||
| for key in json_obj[myvalue]: | ||
| if json_obj[myvalue][key][0]['regionName'] != self.region_name: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (87 > 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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (83 > 79 characters) |
||
| break | ||
|
|
||
| # Get max warning level | ||
| for event in my_warnings: | ||
| if(event['level'] >= data['{}_warning_level'.format(mykey)]): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. line too long (81 > 79 characters) |
||
| data['{}_warning_level'.format(mykey)] = event['level'] | ||
|
|
||
| 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)) | ||
|
|
||
| self.data = data | ||
| self.available = True | ||
| except TypeError: | ||
| _LOGGER.error("Unable to fetch data from DWD-Warnapp") | ||
| self.available = False | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'time' imported but unused