Skip to content

Commit

Permalink
Use seperate API to query the weather of Opendata
Browse files Browse the repository at this point in the history
  • Loading branch information
tsunglung committed Dec 16, 2024
1 parent 9a3398f commit 5d783c1
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 85 deletions.
16 changes: 8 additions & 8 deletions custom_components/opencwb/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
DEFAULT_NAME,
DOMAIN,
FORECAST_MODES,
FORECAST_MODE_ONECALL_HOURLY,
FORECAST_MODE_ONECALL_DAILY
# FORECAST_MODE_ONECALL_HOURLY,
# FORECAST_MODE_ONECALL_DAILY
)
from .core.weatherapi12.uris import ONE_CALL_URI

Expand Down Expand Up @@ -60,9 +60,9 @@ async def async_step_user(self, user_input=None):
location_name) + "-" + user_input[CONF_MODE])
self._abort_if_unique_id_configured()

if (location_id != ONE_CALL_URI and
user_input[CONF_MODE] == FORECAST_MODE_ONECALL_DAILY):
user_input[CONF_MODE] = FORECAST_MODE_ONECALL_HOURLY
#if (location_id != ONE_CALL_URI and
# user_input[CONF_MODE] == FORECAST_MODE_ONECALL_DAILY):
# user_input[CONF_MODE] = FORECAST_MODE_ONECALL_HOURLY

try:
api_online = await _is_ocwb_api_online(
Expand Down Expand Up @@ -121,9 +121,9 @@ async def async_step_init(self, user_input=None):
location_id = _is_supported_city(
self.config_entry.data.get(CONF_API_KEY),
self.config_entry.data.get(CONF_LOCATION_NAME))
if (location_id != ONE_CALL_URI and
user_input[CONF_MODE] == FORECAST_MODE_ONECALL_DAILY):
user_input[CONF_MODE] = FORECAST_MODE_ONECALL_HOURLY
#if (location_id != ONE_CALL_URI and
# user_input[CONF_MODE] == FORECAST_MODE_ONECALL_DAILY):
# user_input[CONF_MODE] = FORECAST_MODE_ONECALL_HOURLY

return self.async_create_entry(title="", data=user_input)

Expand Down
20 changes: 10 additions & 10 deletions custom_components/opencwb/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
# ATTR_FORECAST_PRESSURE,
ATTR_FORECAST_TEMP,
# ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME,
ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
Expand Down Expand Up @@ -93,7 +93,7 @@
FORECAST_MODE_HOURLY,
FORECAST_MODE_DAILY,
FORECAST_MODE_ONECALL_HOURLY,
# FORECAST_MODE_ONECALL_DAILY,
FORECAST_MODE_ONECALL_DAILY,
]
DEFAULT_FORECAST_MODE = FORECAST_MODE_ONECALL_DAILY

Expand All @@ -106,7 +106,7 @@
ATTR_API_WIND_BEARING,
ATTR_API_HUMIDITY,
# ATTR_API_PRESSURE,
ATTR_API_CLOUDS,
# ATTR_API_CLOUDS,
# ATTR_API_RAIN,
# ATTR_API_SNOW,
# ATTR_API_PRECIPITATION_KIND,
Expand All @@ -120,7 +120,7 @@
ATTR_FORECAST_PRECIPITATION_PROBABILITY,
# ATTR_FORECAST_PRESSURE,
ATTR_FORECAST_TEMP,
# ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TEMP_LOW,
ATTR_FORECAST_TIME,
ATTR_FORECAST_WIND_BEARING,
ATTR_FORECAST_WIND_SPEED,
Expand Down Expand Up @@ -182,7 +182,7 @@
# SENSOR_UNIT: PRESSURE_HPA,
# SENSOR_DEVICE_CLASS: SensorDeviceClass.PRESSURE,
# },
ATTR_API_CLOUDS: {SENSOR_NAME: "Cloud coverage", SENSOR_UNIT: PERCENTAGE},
# ATTR_API_CLOUDS: {SENSOR_NAME: "Cloud coverage", SENSOR_UNIT: PERCENTAGE},
# ATTR_API_RAIN: {SENSOR_NAME: "Rain", SENSOR_UNIT: UnitOfLength.MILLIMETERS},
# ATTR_API_SNOW: {SENSOR_NAME: "Snow", SENSOR_UNIT: UnitOfLength.MILLIMETERS},
# ATTR_API_PRECIPITATION_KIND: {SENSOR_NAME: "Precipitation kind"},
Expand Down Expand Up @@ -213,11 +213,11 @@
SENSOR_UNIT: UnitOfTemperature.CELSIUS,
SENSOR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE,
},
# ATTR_FORECAST_TEMP_LOW: {
# SENSOR_NAME: "Temperature Low",
# SENSOR_UNIT: UnitOfTemperature.CELSIUS,
# SENSOR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE,
# },
ATTR_FORECAST_TEMP_LOW: {
SENSOR_NAME: "Temperature Low",
SENSOR_UNIT: UnitOfTemperature.CELSIUS,
SENSOR_DEVICE_CLASS: SensorDeviceClass.TEMPERATURE,
},
ATTR_FORECAST_TIME: {
SENSOR_NAME: "Time",
SENSOR_DEVICE_CLASS: SensorDeviceClass.TIMESTAMP,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/opencwb/core/constants.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

OCWB_VERSION = (1, 0, 0)
OCWB_VERSION = (1, 2, 0)
AGRO_API_VERSION = (1, 0, 0)
AIRPOLLUTION_API_VERSION = (1, 0, 0)
ALERT_API_VERSION = (1, 0, 0)
Expand Down
34 changes: 17 additions & 17 deletions custom_components/opencwb/core/utils/opendata_cwb.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ def _get_weather(the_dict, index, wx_index, last_pop, mode):
if element_value is None:
continue

if "WeatherDescription" == i[elementname] or "\u5929\u6c23\u9810\u5831\u7d9c\u5408\u63cf\u8ff0" == i[elementname]:
if i[elementname] in ["WeatherDescription", "\u5929\u6c23\u9810\u5831\u7d9c\u5408\u63cf\u8ff0"]:
if value_str in element_value[0]:
value["weather"][0]["description"] = element_value[0][value_str]
value["weather"][0]["icon"] = ""
else:
value["weather"][0]["description"] = list(element_value[0].values())[0]
value["weather"][0]["icon"] = ""
elif "Wx" == i[elementname] or "\u5929\u6c23\u73fe\u8c61" == i[elementname]:
elif i[elementname] in ["Wx", "\u5929\u6c23\u73fe\u8c61"]:
if value_str in element_value[0]:
value["weather"][0]["main"] = element_value[0][value_str]
value["weather"][0]["id"] = int(element_value[1][value_str])
Expand All @@ -75,68 +75,68 @@ def _get_weather(the_dict, index, wx_index, last_pop, mode):
pop = "0"
value["pop"] = float(int(pop)/100)
break
elif "AT" == i[elementname] or "\u9ad4\u611f\u6eab\u5ea6" == i[elementname]:
elif i[elementname]in ["AT", "\u9ad4\u611f\u6eab\u5ea6"]:
if value_str in element_value[0]:
value["main"]["feels_like"] = int(element_value[0][value_str])
else:
value["main"]["feels_like"] = int(list(element_value[0].values())[0])
elif "MaxAT" == i[elementname] or "\u6700\u9ad8\u9ad4\u611f\u6eab\u5ea6" == i[elementname]:
elif i[elementname] in ["MaxAT", "\u6700\u9ad8\u9ad4\u611f\u6eab\u5ea6"]:
if value_str in element_value[0]:
value["main"]["feels_like"] = int(element_value[0][value_str])
value["feels_like"]["max"] = int(element_value[0][value_str])
else:
value["main"]["feels_like"] = int(list(element_value[0].vaules())[0])
value["main"]["feels_like"] = int(list(element_value[0].values())[0])
value["feels_like"]["max"] = int(list(element_value[0].values())[0])
elif "MinAT" == i[elementname] or "\u6700\u4f4e\u9ad4\u611f\u6eab\u5ea6" == i[elementname]:
elif i[elementname] in ["MinAT", "\u6700\u4f4e\u9ad4\u611f\u6eab\u5ea6"]:
if value_str in element_value[0]:
value["feels_like"]["min"] = int(element_value[0][value_str])
else:
value["feels_like"]["min"] = int(list(element_value[0].values())[0])
elif "UVI" == i[elementname] or "\u7d2b\u5916\u7dda\u6307\u6578" == i[elementname]:
elif i[elementname] in ["UVI", "\u7d2b\u5916\u7dda\u6307\u6578"]:
value["uvi"] = 0
for j in i["time"]:
for j in i[time_str]:
if start_time == j[starttime]:
if value_str in element_value[0]:
value["uvi"] = int(j[elementvalue][0][value_str])
else:
value["uvi"] = int(list(j[elementvalue][0].values())[0])
break
elif "T" == i[elementname] or "\u6eab\u5ea6" == i[elementname]:
elif i[elementname] in ["T", "\u6eab\u5ea6", "\u5e73\u5747\u6eab\u5ea6"]:
if value_str in element_value[0]:
value["main"]["temp"] = int(element_value[0][value_str])
else:
value["main"]["temp"] = int(list(element_value[0].values())[0])
elif "MaxT" == i[elementname] or "\u6700\u9ad8\u6eab\u5ea6" == i[elementname]:
elif i[elementname] in ["MaxT", "\u6700\u9ad8\u6eab\u5ea6"]:
if value_str in element_value[0]:
value["main"]["temp_max"] = int(element_value[0][value_str])
else:
value["main"]["temp_max"] = int(list(element_value[0].values())[0])
elif "MinT" == i[elementname] or "\u6700\u4f4e\u6eab\u5ea6" == i[elementname]:
elif i[elementname] in ["MinT", "\u6700\u4f4e\u6eab\u5ea6"]:
if value_str in element_value[0]:
value["main"]["temp_min"] = int(element_value[0][value_str])
else:
value["main"]["temp_min"] = int(list(element_value[0].values())[0])
elif "Td" == i[elementname] or "\u9732\u9ede\u6eab\u5ea6" == i[elementname]:
elif i[elementname] in ["Td", "\u9732\u9ede\u6eab\u5ea6", "\u5e73\u5747\u9732\u9ede\u6eab\u5ea6"]:
if value_str in element_value[0]:
value["calc"]["dewpoint"] = int(element_value[0][value_str]) * 100
else:
value["calc"]["dewpoint"] = int(list(element_value[0].values())[0]) * 100
elif "RH" == i[elementname] or "\u76f8\u5c0d\u6fd5\u5ea6" == i[elementname]:
elif i[elementname] in ["RH", "\u76f8\u5c0d\u6fd5\u5ea6"]:
if value_str in element_value[0]:
value["humidity"] = int(element_value[0][value_str])
else:
value["humidity"] = int(list(element_value[0].values())[0])
elif "MinCI" == i[elementname] or "\u8212\u9069\u5ea6\u6307\u6578" == i[elementname]:
elif i[elementname] in ["MinCI", "\u8212\u9069\u5ea6\u6307\u6578"]:
if value_str in element_value[0]:
value["calc"]["humidex"] = int(element_value[0][value_str])
else:
value["calc"]["humidex"] = int(list(element_value[0].values())[0])
elif "MaxCI" == i[elementname] or "\u8212\u9069\u5ea6\u6307\u6578" == i[elementname]:
elif i[elementname] in ["MaxCI", "\u8212\u9069\u5ea6\u6307\u6578"]:
if value_str in element_value[0]:
value["calc"]["heatindex"] = int(element_value[0][value_str])
else:
value["calc"]["heatindex"] = int(list(element_value[0].values())[0])
elif "WS" == i[elementname] or "\u98a8\u901f" == i[elementname]:
elif i[elementname] in ["WS", "\u98a8\u901f"]:
if value_str in element_value[0]:
value["wind_speed"] = int(''.join(
k for k in element_value[0][value_str] if k.isdigit()))
Expand All @@ -147,7 +147,7 @@ def _get_weather(the_dict, index, wx_index, last_pop, mode):
k for k in list(element_value[0].values())[0] if k.isdigit()))
value["wind_gust"] = int(''.join(
c for c in list(element_value[0].values())[1] if c.isdigit()))
elif "WD" == i[elementname] or "\u98a8\u5411" == i[elementname]:
elif i[elementname] in ["WD", "\u98a8\u5411"]:
if value_str in element_value[0]:
value["wind_deg"] = element_value[0][value_str]
else:
Expand Down
37 changes: 19 additions & 18 deletions custom_components/opencwb/core/weatherapi12/weather_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ def weather_at_place(self, name, interval):
:param name: the location's toponym
:type name: str
:param interval: the granularity of the forecast, among `3h` and 'daily'
:type interval: str among `3h` and 'daily'
:param interval: the granularity of the forecast, among `hourly` and 'daily'
:type interval: str among `hourly` and 'daily'
:returns: an *Observation* instance or ``None`` if no weather data is
available
:raises: *ParseResponseException* when OCWB Weather API responses' data
Expand All @@ -78,7 +78,7 @@ def weather_at_place(self, name, interval):
if loc_id is None:
raise ValueError("%s is not support location".format(name))
params = {'locationName': name}
if interval == '3h':
if interval == 'hourly':
uri = loc_id
elif interval == 'daily':
uri = loc_id[0:loc_id.rindex("-") + 1] + str(
Expand Down Expand Up @@ -294,8 +294,8 @@ def forecast_at_place(self, name, interval, limit=None):
:param name: the location's toponym
:type name: str
:param interval: the granularity of the forecast, among `3h` and 'daily'
:type interval: str among `3h` and 'daily'
:param interval: the granularity of the forecast, among `hourly` and 'daily'
:type interval: str among `hourly` and 'daily'
:param limit: the maximum number of *Weather* items to be retrieved
(default is ``None``, which stands for any number of items)
:type limit: int or ``None``
Expand All @@ -317,7 +317,7 @@ def forecast_at_place(self, name, interval, limit=None):
params = {'locationName': urllib.parse.quote_plus(name)}
if limit is not None:
params['cnt'] = limit
if interval == '3h':
if interval == 'hourly':
uri = loc_id
elif interval == 'daily':
uri = loc_id[0:loc_id.rindex("-") + 1] + str(
Expand All @@ -343,8 +343,8 @@ def forecast_at_coords(self, lat, lon, interval, limit=None):
:type lat: int/float
:param lon: location's longitude, must be between -180.0 and 180.0
:type lon: int/float
:param interval: the granularity of the forecast, among `3h` and 'daily'
:type interval: str among `3h` and 'daily'
:param interval: the granularity of the forecast, among `hourly` and 'daily'
:type interval: str among `hourly` and 'daily'
:param limit: the maximum number of *Weather* items to be retrieved
(default is ``None``, which stands for any number of items)
:type limit: int or ``None``
Expand All @@ -364,7 +364,7 @@ def forecast_at_coords(self, lat, lon, interval, limit=None):
params = {'lon': lon, 'lat': lat}
if limit is not None:
params['cnt'] = limit
if interval == '3h':
if interval == 'hourly':
uri = THREE_HOURS_FORECAST_URI
elif interval == 'daily':
uri = DAILY_FORECAST_URI
Expand All @@ -387,8 +387,8 @@ def forecast_at_id(self, id, interval, limit=None):
:param id: the location's city ID
:type id: int
:param interval: the granularity of the forecast, among `3h` and 'daily'
:type interval: str among `3h` and 'daily'
:param interval: the granularity of the forecast, among `hourly` and 'daily'
:type interval: str among `hourly` and 'daily'
:param limit: the maximum number of *Weather* items to be retrieved
(default is ``None``, which stands for any number of items)
:type limit: int or ``None``
Expand All @@ -409,7 +409,7 @@ def forecast_at_id(self, id, interval, limit=None):
params = {'id': id}
if limit is not None:
params['cnt'] = limit
if interval == '3h':
if interval == 'hourly':
uri = THREE_HOURS_FORECAST_URI
elif interval == 'daily':
uri = DAILY_FORECAST_URI
Expand Down Expand Up @@ -536,7 +536,7 @@ def _retrieve_station_history(self, station_ID, limit, interval):
return sh

def one_call(
self, lat: Union[int, float], lon: Union[int, float], loc: Union[str, None], **kwargs
self, lat: Union[int, float], lon: Union[int, float], loc: Union[str, None], intvl: Union[str, None], **kwargs
) -> one_call.OneCall:
"""
Queries the OCWB Weather API with one call for current weather information and forecast for the
Expand All @@ -554,7 +554,7 @@ def one_call(
:type lon: int/float
:param lon: location's name
:type lon: str or None
:param intvl: internal
:param intvl: interval
:type intrl: str or None
:returns: a *OneCall* instance or ``None`` if the data is not
available for the specified location
Expand All @@ -567,15 +567,16 @@ def one_call(

loc_id = self.supported_city(loc)
loc = self.remove_city_name(loc)
uri = ONE_CALL_URI
if loc_id != ONE_CALL_URI:
uri = loc_id
if intvl == "daily":
uri = uri[0:uri.rindex("-") + 1] + str(
int(uri[uri.rindex("-") + 1:]) + 2).zfill(3)
params = {
'lon': lon,
'lat': lat,
'locationId': loc_id,
'locationName': loc}
# 'locationId': loc_id,
'locationName': loc,
'interval': intvl}
for key , value in kwargs.items():
if key == 'exclude':
params['exclude'] = value
Expand Down
2 changes: 1 addition & 1 deletion custom_components/opencwb/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"domain": "opencwb",
"name": "OpenCWA",
"config_flow": true,
"version": "1.1.0",
"version": "1.2.0",
"documentation": "https://opendata.cwa.gov.tw/devManual/insrtuction",
"issue_tracker": "https://github.com/tsunglung/OpenCWB/issues",
"requirements": ["geojson"],
Expand Down
Loading

0 comments on commit 5d783c1

Please sign in to comment.