Skip to content

Commit

Permalink
Merge pull request #25 from autoSteve/Utilise-async_get_time_zone
Browse files Browse the repository at this point in the history
Add support for async_get_time_zone()
  • Loading branch information
BJReplay authored Jun 13, 2024
2 parents 38315cd + 5af3948 commit f0fd7d2
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions custom_components/solcast_solar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
from homeassistant.util import dt as dt_util

from .const import (
DOMAIN,
SERVICE_CLEAR_DATA,
SERVICE_UPDATE,
SERVICE_QUERY_FORECAST_DATA,
SERVICE_SET_DAMPENING,
DOMAIN,
SERVICE_CLEAR_DATA,
SERVICE_UPDATE,
SERVICE_QUERY_FORECAST_DATA,
SERVICE_SET_DAMPENING,
SERVICE_SET_HARD_LIMIT,
SERVICE_REMOVE_HARD_LIMIT,
SOLCAST_URL,
Expand Down Expand Up @@ -87,37 +87,47 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
for a in range(0,24):
optdamp[str(a)] = 1.0

# Introduced in 2024.6.0: async_get_time_zone
try:
dt_util.async_get_time_zone
asynctz = True
except:
asynctz = False
if asynctz:
tz = await dt_util.async_get_time_zone(hass.config.time_zone)
else:
tz = dt_util.get_time_zone(hass.config.time_zone)
options = ConnectionOptions(
entry.options[CONF_API_KEY],
SOLCAST_URL,
hass.config.path('solcast.json'),
dt_util.get_time_zone(hass.config.time_zone),
tz,
optdamp,
entry.options[CUSTOM_HOUR_SENSOR],
entry.options.get(KEY_ESTIMATE,"estimate"),
(entry.options.get(HARD_LIMIT,100000)/1000),
)

solcast = SolcastApi(aiohttp_client.async_get_clientsession(hass), options)

try:
await solcast.sites_data()
await solcast.sites_usage()
except Exception as ex:
raise ConfigEntryNotReady(f"Getting sites data failed: {ex}") from ex

await solcast.load_saved_data()

_VERSION = ""
try:
integration = await loader.async_get_integration(hass, DOMAIN)
_VERSION = str(integration.version)
_LOGGER.info(f"Solcast Integration version number: {_VERSION}")
except loader.IntegrationNotFound:
pass

coordinator = SolcastUpdateCoordinator(hass, solcast, _VERSION)

await coordinator.setup()

await coordinator.async_config_entry_first_refresh()
Expand Down Expand Up @@ -159,7 +169,7 @@ async def handle_service_get_solcast_data(call: ServiceCall) -> ServiceResponse:
return {"data": d}

return None

async def handle_service_set_dampening(call: ServiceCall):
"""Handle service call"""
try:
Expand Down Expand Up @@ -211,7 +221,6 @@ async def handle_service_set_hard_limit(call: ServiceCall):
if val < 0: # if not a positive int print message and ask for input again
raise HomeAssistantError(f"Error processing {SERVICE_SET_HARD_LIMIT}: Hard limit value not a positive number")


opt = {**entry.options}
opt[HARD_LIMIT] = val
# solcast._hardlimit = val
Expand All @@ -221,7 +230,7 @@ async def handle_service_set_hard_limit(call: ServiceCall):
raise HomeAssistantError(f"Error processing {SERVICE_SET_HARD_LIMIT}: Hard limit value not a positive number")
except intent.IntentHandleError as err:
raise HomeAssistantError(f"Error processing {SERVICE_SET_DAMPENING}: {err}") from err

async def handle_service_remove_hard_limit(call: ServiceCall):
"""Handle service call"""
try:
Expand Down Expand Up @@ -272,7 +281,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
hass.services.async_remove(DOMAIN, SERVICE_SET_DAMPENING)
hass.services.async_remove(DOMAIN, SERVICE_SET_HARD_LIMIT)
hass.services.async_remove(DOMAIN, SERVICE_REMOVE_HARD_LIMIT)


return unload_ok

Expand Down Expand Up @@ -305,8 +313,6 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->

hass.config_entries.async_update_entry(config_entry, options=new)



#new 4.0.15
#custom sensor for 'next x hours'
if config_entry.version == 5:
Expand All @@ -316,8 +322,6 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->

hass.config_entries.async_update_entry(config_entry, options=new)



#new 4.0.16
#which estimate value to use for data calcs est,est10,est90
if config_entry.version == 6:
Expand Down

0 comments on commit f0fd7d2

Please sign in to comment.