Skip to content

Commit

Permalink
Fix Lint Errors & Deprecated Constants
Browse files Browse the repository at this point in the history
  • Loading branch information
cloneofghosts committed Jan 8, 2024
1 parent 086e4fd commit 2fedcbe
Show file tree
Hide file tree
Showing 6 changed files with 530 additions and 596 deletions.
101 changes: 46 additions & 55 deletions custom_components/pirateweather/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@
import logging
from typing import Any

import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_registry import EntityRegistry

from datetime import timedelta
import forecastio

from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.config_entries import ConfigEntry


from homeassistant.const import (
Expand All @@ -27,26 +23,18 @@

from .const import (
CONF_LANGUAGE,
CONFIG_FLOW_VERSION,
DOMAIN,
DEFAULT_FORECAST_MODE,
ENTRY_NAME,
ENTRY_WEATHER_COORDINATOR,
FORECAST_MODES,
PLATFORMS,
UPDATE_LISTENER,
CONF_UNITS,
DEFAULT_UNITS,
DEFAULT_NAME,
FORECASTS_HOURLY,
FORECASTS_DAILY,
PW_PLATFORMS,
PW_PLATFORM,
PW_PREVPLATFORM,
PW_ROUND,
)

#from .weather_update_coordinator import WeatherUpdateCoordinator, DarkSkyData
# from .weather_update_coordinator import WeatherUpdateCoordinator, DarkSkyData
from .weather_update_coordinator import WeatherUpdateCoordinator

CONF_FORECAST = "forecast"
Expand All @@ -63,7 +51,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
latitude = entry.data.get(CONF_LATITUDE, hass.config.latitude)
longitude = entry.data.get(CONF_LONGITUDE, hass.config.longitude)
forecast_mode = _get_config_value(entry, CONF_MODE)
language = _get_config_value(entry, CONF_LANGUAGE)
conditions = _get_config_value(entry, CONF_MONITORED_CONDITIONS)
units = _get_config_value(entry, CONF_UNITS)
forecast_days = _get_config_value(entry, CONF_FORECAST)
Expand All @@ -72,44 +59,44 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
pw_entity_rounding = _get_config_value(entry, PW_ROUND)
pw_scan_Int = entry.data[CONF_SCAN_INTERVAL]


# Extract list of int from forecast days/ hours string if present
#_LOGGER.warning('forecast_days_type: ' + str(type(forecast_days)))

#_LOGGER.warning(forecast_days)
if type(forecast_days) == str:
# If empty, set to none
if forecast_days == "" or forecast_days == "None":
forecast_days = None
else:
if forecast_days[0] == '[':
forecast_days = forecast_days[1:-1].split(",")
# _LOGGER.warning('forecast_days_type: ' + str(type(forecast_days)))

# _LOGGER.warning(forecast_days)
if isinstance(forecast_days, str):
# If empty, set to none
if forecast_days == "" or forecast_days == "None":
forecast_days = None
else:
forecast_days = forecast_days.split(",")
forecast_days = [int(i) for i in forecast_days]

if type(forecast_hours) == str:
# If empty, set to none
if forecast_hours == "" or forecast_hours == "None":
forecast_hours = None
else:
if forecast_hours[0] == '[':
forecast_hours = forecast_hours[1:-1].split(",")
if forecast_days[0] == "[":
forecast_days = forecast_days[1:-1].split(",")
else:
forecast_days = forecast_days.split(",")
forecast_days = [int(i) for i in forecast_days]

if isinstance(forecast_hours, str):
# If empty, set to none
if forecast_hours == "" or forecast_hours == "None":
forecast_hours = None
else:
forecast_hours = forecast_hours.split(",")
forecast_hours = [int(i) for i in forecast_hours]
if forecast_hours[0] == "[":
forecast_hours = forecast_hours[1:-1].split(",")
else:
forecast_hours = forecast_hours.split(",")
forecast_hours = [int(i) for i in forecast_hours]

unique_location = (f"pw-{latitude}-{longitude}")
unique_location = f"pw-{latitude}-{longitude}"

hass.data.setdefault(DOMAIN, {})
# Create and link weather WeatherUpdateCoordinator
weather_coordinator = WeatherUpdateCoordinator(api_key, latitude, longitude, timedelta(seconds=pw_scan_Int), hass)
weather_coordinator = WeatherUpdateCoordinator(
api_key, latitude, longitude, timedelta(seconds=pw_scan_Int), hass
)
hass.data[DOMAIN][unique_location] = weather_coordinator

#await weather_coordinator.async_refresh()
# await weather_coordinator.async_refresh()
await weather_coordinator.async_config_entry_first_refresh()


hass.data[DOMAIN][entry.entry_id] = {
ENTRY_NAME: name,
ENTRY_WEATHER_COORDINATOR: weather_coordinator,
Expand All @@ -127,15 +114,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
}

# 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 (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_setup(entry, PLATFORMS[0])
await hass.config_entries.async_forward_entry_setup(entry, PLATFORMS[0])
# If only weather
elif PW_PLATFORMS[1] in pw_entity_platform:
await hass.config_entries.async_forward_entry_setup(entry, PLATFORMS[1])

await hass.config_entries.async_forward_entry_setup(entry, PLATFORMS[1])

update_listener = entry.add_update_listener(async_update_options)
hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER] = update_listener
Expand All @@ -147,32 +135,35 @@ async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
await hass.config_entries.async_reload(entry.entry_id)



async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""

pw_entity_prevplatform = hass.data[DOMAIN][entry.entry_id][PW_PLATFORM]


# If both
if (PW_PLATFORMS[0] in pw_entity_prevplatform) and (PW_PLATFORMS[1] in pw_entity_prevplatform):
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if (PW_PLATFORMS[0] in pw_entity_prevplatform) and (
PW_PLATFORMS[1] in pw_entity_prevplatform
):
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
# If only sensor
elif PW_PLATFORMS[0] in pw_entity_prevplatform:
unload_ok = await hass.config_entries.async_unload_platforms(entry, [PLATFORMS[0]])
unload_ok = await hass.config_entries.async_unload_platforms(
entry, [PLATFORMS[0]]
)
# If only Weather
elif PW_PLATFORMS[1] in pw_entity_prevplatform:
unload_ok = await hass.config_entries.async_unload_platforms(entry, [PLATFORMS[1]])
unload_ok = await hass.config_entries.async_unload_platforms(
entry, [PLATFORMS[1]]
)

_LOGGER.info('Unloading Pirate Weather')
_LOGGER.info("Unloading Pirate Weather")

if unload_ok:
update_listener = hass.data[DOMAIN][entry.entry_id][UPDATE_LISTENER]
update_listener()

hass.data[DOMAIN].pop(entry.entry_id)


return unload_ok


Expand Down
Loading

0 comments on commit 2fedcbe

Please sign in to comment.