Skip to content

Commit

Permalink
changed to new forecast api
Browse files Browse the repository at this point in the history
  • Loading branch information
Zsolt committed Nov 14, 2023
1 parent 0b73d0d commit 4d5127c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 16 deletions.
27 changes: 22 additions & 5 deletions custom_components/ha_cyprus_weather/cyprus_weather_org.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,36 @@ def get_weather_data(city):

#Hourly forecast data
forecasts_v = cwMain.find_all("div",class_="hour")
forecast_hourly={}
hourly_forecasts=[]
today = datetime.datetime.today()
next_day = False
for forecast_entry_s in forecasts_v:
#pprint(forecast_entry_s)
hourly_forecast_time = re.compile('>\s*(.+?)\s*<').findall(str(forecast_entry_s))[0]
hourly_forecast_time = re.compile('>\s*(.+?)\s*<').findall(str(forecast_entry_s))[0].strip()
h, m = hourly_forecast_time.split(':')
h = int(h)
m = int(m)
forecast_datetime = today.replace(minute=m, hour=h, second=0, microsecond=0)
if h==0 and m==0:
next_day = True
if next_day:
forecast_datetime = forecast_datetime + datetime.timedelta(days=1)
#pprint(hourly_forecast_time)
hourly_forecast_temperature = re.compile('/>\r?\n\s+(-?\d+).+\r?\n').findall(str(forecast_entry_s))[0]
#d = re.compile('>\s*(.+?)\s*<').findall(str(forecast_entry_s))[0]
#pprint(hourly_forecast_temperature)
#forecast_hourly.append()
forecast_hourly[hourly_forecast_time] = hourly_forecast_temperature
#pprint(forecast_hourly)
#forecast_hourly[hourly_forecast_time] = hourly_forecast_temperature

hourly_forecast = {
"Date":forecast_datetime,
"Temp":hourly_forecast_temperature
}
hourly_forecasts.append(hourly_forecast)

#pprint(hourly_forecasts)

weatherData["Forecast.Hourly"] = forecast_hourly
weatherData["Forecast.Hourly"] = hourly_forecasts

#Today forecast
#<div class="day-forecast">
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ha_cyprus_weather/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "ha_cyprus_weather",
"name": "Cyprus weather",
"version": "2.0.0",
"version": "2.0.1",
"documentation": "https://github.com/xumxum/ha_cyprus_weather",
"requirements": [],
"dependencies": [],
Expand Down
41 changes: 31 additions & 10 deletions custom_components/ha_cyprus_weather/weather.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class CyprusWeather(WeatherEntity):
_attr_native_pressure_unit = PRESSURE_HPA
_attr_native_wind_speed_unit = SPEED_METERS_PER_SECOND

_attr_supported_features = WeatherEntityFeature.FORECAST_DAILY
#_attr_supported_features = WeatherEntityFeature.FORECAST_HOURLY

def __init__(self, hass, name, city, coordinator, entry_id):
"""Initialize Cyprus weather."""
_LOGGER.debug("Creating instance of CyprusWeather, using parameters")
Expand Down Expand Up @@ -164,23 +167,41 @@ def native_visibility(self):
except:
return None

@property
def forecast(self):
"""Return the forecast array."""

async def async_forecast_daily(self) -> list[Forecast] | None:
"""Return the daily forecast in native units.
"""
rez = []
forecast_d = self._coordinator.get_weather_value("Forecast")
if forecast_d:
for k in forecast_d:
forecast_entry = {
ATTR_FORECAST_TIME: forecast_d[k]["Date"],
ATTR_FORECAST_NATIVE_TEMP: int(forecast_d[k]["Day.TempHigh"]),
ATTR_FORECAST_NATIVE_TEMP_LOW: int(forecast_d[k]["Night.TempLow"]),
ATTR_FORECAST_TIME: forecast_d[k]["Date"].isoformat(),
ATTR_FORECAST_TEMP: int(forecast_d[k]["Day.TempHigh"]),
ATTR_FORECAST_TEMP_LOW: int(forecast_d[k]["Night.TempLow"]),

ATTR_FORECAST_CONDITION: forecast_d[k]["Day.Condition"] # we show daytime forecast condition not night?!!
}
rez.append(forecast_entry)

return rez


async def async_forecast_hourly(self) -> list[Forecast] | None:
"""Return the hourly forecast in native units.
"""
rez = []
forecast_v = self._coordinator.get_weather_value("Forecast.Hourly")
if forecast_v:
for hourly_forecast in forecast_v:
forecast_entry = {
ATTR_FORECAST_TIME: hourly_forecast["Date"].isoformat(),
ATTR_FORECAST_TEMP: int(hourly_forecast["Temp"]),
#ATTR_FORECAST_CONDITION: forecast_d[k]["Day.Condition"] # we show daytime forecast condition not night?!!
}
rez.append(forecast_entry)

return rez


@property
def state_attributes(self):
Expand Down Expand Up @@ -219,9 +240,9 @@ def state_attributes(self):
if attribution is not None:
data[ATTR_WEATHER_ATTRIBUTION] = attribution

forecast = self.forecast
if forecast is not None:
data[ATTR_FORECAST] = forecast
# forecast = self.forecast
# if forecast is not None:
# data[ATTR_FORECAST] = forecast

# #add our own custom stuff
forecast_temp_high = self._coordinator.get_weather_value("Forecast.Today.TempHigh")
Expand Down

0 comments on commit 4d5127c

Please sign in to comment.