From 8dc11cccef3abe004c30b5dbcdd33ece2bc758fb Mon Sep 17 00:00:00 2001 From: firstof9 Date: Mon, 3 Aug 2020 16:25:12 -0700 Subject: [PATCH 1/2] Added Zone auto detection based on lat/lon * Added zone auto detection based on user latitude and logitude from Home Assistant Config --- custom_components/nws_alerts/config_flow.py | 72 +++++++++++++------ .../nws_alerts/translations/en.json | 36 ++++++---- 2 files changed, 75 insertions(+), 33 deletions(-) diff --git a/custom_components/nws_alerts/config_flow.py b/custom_components/nws_alerts/config_flow.py index c8b31b8..c85e6ec 100644 --- a/custom_components/nws_alerts/config_flow.py +++ b/custom_components/nws_alerts/config_flow.py @@ -1,22 +1,57 @@ """Adds config flow for NWS Alerts.""" +import aiohttp import logging from collections import OrderedDict import voluptuous as vol +from homeassistant.const import CONF_NAME from homeassistant.core import callback from homeassistant import config_entries from .const import ( + API_ENDPOINT, DOMAIN, CONF_ZONE_ID, DEFAULT_NAME, + USER_AGENT, ) -from homeassistant.const import CONF_NAME +JSON_FEATURES = "features" +JSON_PROPERTIES = "properties" +JSON_ID = "id" _LOGGER = logging.getLogger(__name__) +async def _get_zone_list(self): + """Return list of zone by lat/lon""" + + data = None + lat = self.hass.config.latitude + lon = self.hass.config.longitude + + headers = {"User-Agent": USER_AGENT, "Accept": "application/geo+json"} + + url = API_ENDPOINT + "/zones?point=%s,%s" % (lat, lon) + + async with aiohttp.ClientSession() as session: + async with session.get(url, headers=headers) as r: + _LOGGER.debug("getting zone list for %s,%s from %s" % (lat, lon, url)) + if r.status == 200: + data = await r.json() + + zone_list = [] + if data is not None: + if "features" in data: + x = 0 + while len(data[JSON_FEATURES]) > x: + zone_list.append(data[JSON_FEATURES][x][JSON_PROPERTIES][JSON_ID]) + x += 1 + _LOGGER.debug("Zones list: %s", zone_list) + return zone_list + return None + + @config_entries.HANDLERS.register(DOMAIN) class NWSAlertsFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): """Config flow for NWS Alerts.""" @@ -32,13 +67,11 @@ def __init__(self): async def async_step_user(self, user_input={}): """Handle a flow initialized by the user.""" self._errors = {} + self._zone_list = await _get_zone_list(self) if user_input is not None: self._data.update(user_input) - return self.async_create_entry(title=self._data[CONF_NAME], - data=self._data) - return await self._show_config_form(user_input) - + return self.async_create_entry(title=self._data[CONF_NAME], data=self._data) return await self._show_config_form(user_input) async def _show_config_form(self, user_input): @@ -46,19 +79,20 @@ async def _show_config_form(self, user_input): # Defaults name = DEFAULT_NAME + zone_id = self._zone_list if user_input is not None: if "name" in user_input: name = user_input["name"] - if "zone_id" in user_input: - zone_id = user_input["zone_id"] + if CONF_ZONE_ID in user_input: + zone_id = user_input[CONF_ZONE_ID] data_schema = OrderedDict() - data_schema[vol.Required("name", default=name)] = str - data_schema[vol.Required("zone_id")] = str + data_schema[vol.Optional("name", default=name)] = str + data_schema[vol.Required(CONF_ZONE_ID, default=zone_id)] = str return self.async_show_form( - step_id="user", data_schema=vol.Schema(data_schema), - errors=self._errors) + step_id="user", data_schema=vol.Schema(data_schema), errors=self._errors + ) @staticmethod @callback @@ -79,11 +113,7 @@ async def async_step_init(self, user_input=None): """Manage Mail and Packages options.""" if user_input is not None: self._data.update(user_input) - return self.async_create_entry(title=self._data[CONF_NAME], - data=self._data) - - return await self._show_options_form(user_input) - + return self.async_create_entry(title=self._data[CONF_NAME], data=self._data) return await self._show_options_form(user_input) async def _show_options_form(self, user_input): @@ -96,12 +126,12 @@ async def _show_options_form(self, user_input): if user_input is not None: if "name" in user_input: name = user_input["name"] - if "zone_id" in user_input: - zone_id = user_input["zone_id"] + if CONF_ZONE_ID in user_input: + zone_id = user_input[CONF_ZONE_ID] data_schema = OrderedDict() - data_schema[vol.Required("name", default=name)] = str + data_schema[vol.Optional("name", default=name)] = str data_schema[vol.Required("zone_id", default=zone_id)] = str return self.async_show_form( - step_id="init", data_schema=vol.Schema(data_schema), - errors=self._errors) + step_id="init", data_schema=vol.Schema(data_schema), errors=self._errors + ) diff --git a/custom_components/nws_alerts/translations/en.json b/custom_components/nws_alerts/translations/en.json index 8df6d27..518fc25 100644 --- a/custom_components/nws_alerts/translations/en.json +++ b/custom_components/nws_alerts/translations/en.json @@ -1,14 +1,26 @@ { - "config": { - "step": { - "user": { - "data": { - "name": "Friendly Name", - "zone_id": "Zone ID(s)" - }, - "description": "You can find your Zone or County ID by going to https://alerts.weather.gov/, scroll down to your state and click on the 'zone list' and/or 'county list' then look for the entry for your county.\nSeperate multiple zones with commas ie: PAC049,WVC031", - "title": "NWS Alerts" - } - } - } + "config": { + "step": { + "user": { + "data": { + "name": "Friendly Name", + "zone_id": "Zone ID(s)" + }, + "description": "You can find your Zone or County ID by going to https://alerts.weather.gov/,\nscroll down to your state and click on the 'zone list' and/or 'county list' then look for the entry for your county.\nSeperate multiple zones with commas ie: PAC049,WVC031\n\nZones closest to you will be populated automaticly.", + "title": "NWS Alerts" + } + } + }, + "options": { + "step": { + "init": { + "data": { + "name": "Friendly Name", + "zone_id": "Zone ID(s)" + }, + "description": "You can find your Zone or County ID by going to https://alerts.weather.gov/,\nscroll down to your state and click on the 'zone list' and/or 'county list' then look for the entry for your county.\nSeperate multiple zones with commas ie: PAC049,WVC031", + "title": "NWS Alerts" + } + } + } } From e79ef080285d2d1a9654c77773c4f583cc1315c2 Mon Sep 17 00:00:00 2001 From: firstof9 Date: Mon, 3 Aug 2020 16:26:28 -0700 Subject: [PATCH 2/2] Bump version in const.py --- custom_components/nws_alerts/const.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/custom_components/nws_alerts/const.py b/custom_components/nws_alerts/const.py index 9ebbb73..48fa56f 100644 --- a/custom_components/nws_alerts/const.py +++ b/custom_components/nws_alerts/const.py @@ -1,11 +1,12 @@ -API_ENDPOINT = 'https://api.weather.gov' -USER_AGENT = 'Home Assistant' -DEFAULT_ICON = 'mdi:alert' -DEFAULT_NAME = 'NWS Alerts' -CONF_ZONE_ID = 'zone_id' -ZONE_ID = '' -VERSION = '1.0' -ISSUE_URL = 'https://github.com/finity69x2/nws_alert' -DOMAIN = 'nws_alerts' -PLATFORM = 'sensor' -ATTRIBUTION = 'Data provided by Weather.gov' \ No newline at end of file +API_ENDPOINT = "https://api.weather.gov" +USER_AGENT = "Home Assistant" +DEFAULT_ICON = "mdi:alert" +DEFAULT_NAME = "NWS Alerts" +CONF_ZONE_ID = "zone_id" +ZONE_ID = "" +VERSION = "1.4" +ISSUE_URL = "https://github.com/finity69x2/nws_alert" +DOMAIN = "nws_alerts" +PLATFORM = "sensor" +ATTRIBUTION = "Data provided by Weather.gov" +