Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
- Corrects a small bug about platforms re: Issue #273
- Changes sensor reported times to datetimes re: Issue #275
- Allows for updating locations via hass re: Issue #241
- Allows changing the scan_interval re: Issue #230
  • Loading branch information
alexander0042 committed Aug 15, 2024
1 parent d80f644 commit 55ade00
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
16 changes: 13 additions & 3 deletions custom_components/pirateweather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
forecast_hours = _get_config_value(entry, CONF_HOURLY_FORECAST)
pw_entity_platform = _get_config_value(entry, PW_PLATFORM)
pw_entity_rounding = _get_config_value(entry, PW_ROUND)
pw_scan_Int = entry.data[CONF_SCAN_INTERVAL]
pw_scan_Int = _get_config_value(entry, CONF_SCAN_INTERVAL)

# Extract list of int from forecast days/ hours string if present
# _LOGGER.warning('forecast_days_type: ' + str(type(forecast_days)))
Expand Down Expand Up @@ -109,8 +109,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
CONF_SCAN_INTERVAL: pw_scan_Int,
}

# Setup platforms
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
# Setup platforms
# If both platforms
if (PW_PLATFORMS[0] in pw_entity_platform) and (
PW_PLATFORMS[1] in pw_entity_platform
):
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
# If only sensor
elif PW_PLATFORMS[0] in pw_entity_platform:
await hass.config_entries.async_forward_entry_setups(entry, [PLATFORMS[0]])
# If only weather
elif PW_PLATFORMS[1] in pw_entity_platform:
await hass.config_entries.async_forward_entry_setups(entry, [PLATFORMS[1]])

update_listener = entry.add_update_listener(async_update_options)
hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER] = update_listener
Expand Down
9 changes: 9 additions & 0 deletions custom_components/pirateweather/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ async def async_step_init(self, user_input=None):
),
),
): cv.longitude,
vol.Optional(
CONF_SCAN_INTERVAL,
default=self.config_entry.options.get(
CONF_SCAN_INTERVAL,
self.config_entry.data.get(
CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL
),
),
): int,
vol.Required(
PW_PLATFORM,
default=self.config_entry.options.get(
Expand Down
2 changes: 1 addition & 1 deletion custom_components/pirateweather/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"requirements": [
"python-forecastio==1.4.0"
],
"version": "1.5.4"
"version": "1.5.5"
}
14 changes: 12 additions & 2 deletions custom_components/pirateweather/sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Support for Pirate Weather (Dark Sky Compatable) weather service."""

from datetime import datetim
import logging
from dataclasses import dataclass, field
from typing import Literal, NamedTuple
Expand Down Expand Up @@ -1161,7 +1161,17 @@ def get_state(self, data):
"precip_intensity_max",
]:
outState = round(state, roundingPrecip)


# Convert unix times to datetimes times
elif self.type in [
"temperature_high_time",
"temperature_low_time",
"apparent_temperature_high_time",
"apparent_temperature_low_time",
"sunrise_time",
"sunset_time",
]:
outState = datetime.fromtimestamp(state)
else:
outState = state

Expand Down
9 changes: 5 additions & 4 deletions custom_components/pirateweather/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
"data": {
"api_key": "API Key",
"language": "Language",
"latitude": "Latitude",
"longitude": "Longitude",
"latitude": "Latitude. Set as 0 to use current location",
"longitude": "Longitude. Set as 0 to use current location",
"mode": "Forecast mode for the Weather entity",
"name": "Integration Name",
"units": "Units for sensors. Only used for if sensors are requested.",
Expand All @@ -38,8 +38,9 @@
"data": {
"api_key": "API Key",
"language": "Language",
"latitude": "Latitude",
"longitude": "Longitude",
"latitude": "Latitude. Set as 0 to use current location",
"longitude": "Longitude. Set as 0 to use current location",
"scan_interval": "Seconds to wait between updates. Reducing this below 900 seconds (15 minutes) is not recomended."
"mode": "Forecast mode for the Weather entity",
"name": "Integration Name",
"units": "Units for sensors. Only used for if sensors are requested.",
Expand Down
21 changes: 18 additions & 3 deletions custom_components/pirateweather/weather_update_coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,30 @@ async def _async_update_data(self):
async with async_timeout.timeout(60):
try:
data = await self._get_pw_weather()
_LOGGER.debug("Pirate Weather data update")
except HTTPException as err:
raise UpdateFailed(f"Error communicating with API: {err}") from err
return data

async def _get_pw_weather(self):
"""Poll weather data from PW."""

if self.latitude == 0.0:
requestLatitude = self.hass.config.latitude
else:
requestLatitude = self.latitude

if self.longitude == 0.0:
requestLongitude = self.hass.config.latitude
else:
requestLongitude = self.longitude

forecastString = (
"https://api.pirateweather.net/forecast/"
+ self._api_key
+ "/"
+ str(self.latitude)
+ str(requestLatitude)
+ ","
+ str(self.longitude)
+ str(requestLongitude)
+ "?units="
+ self.requested_units
+ "&extend=hourly"
Expand All @@ -72,4 +82,9 @@ async def _get_pw_weather(self):
headers = resp.headers
status = resp.raise_for_status()

_LOGGER.debug("Pirate Weather data update for "
+ str(requestLatitude)
+ ","
+ str(requestLongitude))

return Forecast(jsonText, status, headers)

0 comments on commit 55ade00

Please sign in to comment.