Skip to content

Commit

Permalink
Merge pull request #5 from firstof9/use-lat-lon
Browse files Browse the repository at this point in the history
Use lat lon
  • Loading branch information
finity69x2 authored Aug 8, 2020
2 parents 0fdbe5c + e79ef08 commit e0b4a39
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 44 deletions.
72 changes: 51 additions & 21 deletions custom_components/nws_alerts/config_flow.py
Original file line number Diff line number Diff line change
@@ -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."""
Expand All @@ -32,33 +67,32 @@ 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):
"""Show the configuration form to edit location data."""

# 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
Expand All @@ -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):
Expand All @@ -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
)
23 changes: 12 additions & 11 deletions custom_components/nws_alerts/const.py
Original file line number Diff line number Diff line change
@@ -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'
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"

36 changes: 24 additions & 12 deletions custom_components/nws_alerts/translations/en.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
}

0 comments on commit e0b4a39

Please sign in to comment.