Skip to content

Commit

Permalink
Tests updated to suit data schema 6
Browse files Browse the repository at this point in the history
  • Loading branch information
autoSteve committed Jan 29, 2025
1 parent 01f6d41 commit 6d641d3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
48 changes: 30 additions & 18 deletions custom_components/solcast_solar/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,6 @@ async def __check_forecast_fetch(self, *args) -> None:
self.__fetch,
interval,
)
"""
self.tasks[task_name] = async_call_later(self.hass, update_in, self.__fetch)
"""
if interval < _from:
pop_expired.append(index)
# Remove expired intervals if any have been missed
Expand Down Expand Up @@ -349,15 +346,18 @@ async def service_event_update(self, **kwargs) -> None:
ServiceValidationError: Notify Home Assistant that an error has occurred.
"""
if self.solcast.reauth_required:
raise ConfigEntryAuthFailed(translation_domain=DOMAIN, translation_key="init_key_invalid")

if self.solcast.options.auto_update > 0 and "ignore_auto_enabled" not in kwargs:
raise ServiceValidationError(translation_domain=DOMAIN, translation_key="auto_use_force")
task = asyncio.create_task(
self.__forecast_update(completion="Completed task update" if not kwargs.get("completion") else kwargs["completion"])
)
self.tasks["forecast_update"] = task.cancel
if self.tasks.get("forecast_update") is None:
if self.solcast.reauth_required:
raise ConfigEntryAuthFailed(translation_domain=DOMAIN, translation_key="init_key_invalid")

if self.solcast.options.auto_update > 0 and "ignore_auto_enabled" not in kwargs:
raise ServiceValidationError(translation_domain=DOMAIN, translation_key="auto_use_force")
task = asyncio.create_task(
self.__forecast_update(completion="Completed task update" if not kwargs.get("completion") else kwargs["completion"])
)
self.tasks["forecast_update"] = task.cancel
else:
_LOGGER.warning("Forecast update already requested, ignoring")

async def service_event_force_update(self) -> None:
"""Force the update of forecast data when requested by a service call. Ignores API usage/limit counts.
Expand All @@ -366,17 +366,21 @@ async def service_event_force_update(self) -> None:
ServiceValidationError: Notify Home Assistant that an error has occurred.
"""
if self.solcast.reauth_required:
raise ConfigEntryAuthFailed(translation_domain=DOMAIN, translation_key="init_key_invalid")
if self.tasks.get("forecast_update") is None:
if self.solcast.reauth_required:
raise ConfigEntryAuthFailed(translation_domain=DOMAIN, translation_key="init_key_invalid")

if self.solcast.options.auto_update == 0:
raise ServiceValidationError(translation_domain=DOMAIN, translation_key="auto_use_normal")
task = asyncio.create_task(self.__forecast_update(force=True, completion="Completed task force_update"))
self.tasks["forecast_update"] = task.cancel
if self.solcast.options.auto_update == 0:
raise ServiceValidationError(translation_domain=DOMAIN, translation_key="auto_use_normal")
task = asyncio.create_task(self.__forecast_update(force=True, completion="Completed task force_update"))
self.tasks["forecast_update"] = task.cancel
else:
_LOGGER.warning("Forecast update already requested, ignoring")

async def service_event_delete_old_solcast_json_file(self) -> None:
"""Delete the solcast.json file when requested by a service call."""
await self.solcast.tasks_cancel()
await self.tasks_cancel_specific("forecast_update")
await self.hass.async_block_till_done()
await self.solcast.delete_solcast_file()
self._data_updated = True
Expand Down Expand Up @@ -504,3 +508,11 @@ async def tasks_cancel(self) -> None:
_LOGGER.debug("Cancelling coordinator task %s", task)
cancel()
self.tasks = {}

async def tasks_cancel_specific(self, task: str) -> None:
"""Cancel a specific task."""
cancel = self.tasks.get(task)
if cancel is not None:
_LOGGER.debug("Cancelling coordinator task %s", task)
cancel()
self.tasks.pop(task)
7 changes: 5 additions & 2 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ async def _wait_for_update(caplog: any) -> None:
and "aborting forecast update" not in caplog.text
and "pausing" not in caplog.text
and "Completed task update" not in caplog.text
and "Completed task force_update" not in caplog.text
and "ConfigEntryAuthFailed" not in caplog.text
): # Wait for task to complete
await asyncio.sleep(0.01)
Expand All @@ -176,7 +177,9 @@ async def _wait_for_abort(caplog: any) -> None:
"""Wait for forecast update completion."""

async with asyncio.timeout(5):
while "Forecast update aborted" not in caplog.text: # Wait for task to abort
while (
"Forecast update aborted" not in caplog.text and "Forecast update already requested, ignoring" not in caplog.text
): # Wait for task to abort
await asyncio.sleep(0.01)


Expand Down Expand Up @@ -1007,7 +1010,7 @@ def alter_last_updated():
data = json.loads(data_file.read_text(encoding="utf-8"))
data["last_updated"] = (dt.now(datetime.UTC) - timedelta(days=5)).isoformat()
data["last_attempt"] = data["last_updated"]
data["auto_updated"] = True
data["auto_updated"] = 10
# Remove forecasts today up to "now"
for site in data["siteinfo"].values():
site["forecasts"] = [f for f in site["forecasts"] if f["period_start"] > dt.now(datetime.UTC).isoformat()]
Expand Down

0 comments on commit 6d641d3

Please sign in to comment.