mirror of
https://github.com/hacs/integration.git
synced 2025-08-15 23:30:23 +00:00
Add check ratelimit task (#2331)
This commit is contained in:
40
custom_components/hacs/tasks/check_ratelimit.py
Normal file
40
custom_components/hacs/tasks/check_ratelimit.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
""""Hacs base setup task."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
|
from ..base import HacsBase
|
||||||
|
from ..enums import HacsDisabledReason, HacsStage
|
||||||
|
from .base import HacsTask
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_task(hacs: HacsBase, hass: HomeAssistant) -> Task:
|
||||||
|
"""Set up this task."""
|
||||||
|
return Task(hacs=hacs, hass=hass)
|
||||||
|
|
||||||
|
|
||||||
|
class Task(HacsTask):
|
||||||
|
""" "Hacs task base."""
|
||||||
|
|
||||||
|
_can_run_disabled = True
|
||||||
|
schedule = timedelta(minutes=15)
|
||||||
|
|
||||||
|
async def async_execute(self) -> None:
|
||||||
|
"""Execute the task."""
|
||||||
|
if not self.hacs.stage == HacsStage.RUNNING:
|
||||||
|
self.task_logger(self.log.debug, "HACS is not running")
|
||||||
|
return
|
||||||
|
if (
|
||||||
|
not self.hacs.system.disabled
|
||||||
|
or self.hacs.system.disabled_reason != HacsDisabledReason.RATE_LIMIT
|
||||||
|
):
|
||||||
|
self.task_logger(self.log.debug, "HACS is not ratelimited")
|
||||||
|
return
|
||||||
|
|
||||||
|
self.task_logger(self.log.debug, "Checking if ratelimit has lifted")
|
||||||
|
can_update = await self.hacs.async_can_update()
|
||||||
|
self.task_logger(self.log.debug, f"Ratelimit indicate we can update {can_update}")
|
||||||
|
if can_update > 0:
|
||||||
|
self.hacs.enable_hacs()
|
71
tests/tasks/test_check_ratelimit.py
Normal file
71
tests/tasks/test_check_ratelimit.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
# pylint: disable=missing-function-docstring,missing-module-docstring, protected-access
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from custom_components.hacs.base import HacsBase
|
||||||
|
from custom_components.hacs.enums import HacsDisabledReason, HacsStage
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_check_ratelimit_not_running(hacs: HacsBase, caplog: pytest.LogCaptureFixture):
|
||||||
|
hacs.stage = HacsStage.WAITING
|
||||||
|
await hacs.tasks.async_load()
|
||||||
|
task = hacs.tasks.get("check_ratelimit")
|
||||||
|
|
||||||
|
assert task
|
||||||
|
|
||||||
|
await task.execute_task()
|
||||||
|
assert "HacsTask<check_ratelimit> HACS is not running" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_check_ratelimit_not_ratelimited(hacs: HacsBase, caplog: pytest.LogCaptureFixture):
|
||||||
|
hacs.stage = HacsStage.RUNNING
|
||||||
|
await hacs.tasks.async_load()
|
||||||
|
task = hacs.tasks.get("check_ratelimit")
|
||||||
|
|
||||||
|
assert task
|
||||||
|
|
||||||
|
await task.execute_task()
|
||||||
|
assert "HacsTask<check_ratelimit> HACS is not ratelimited" in caplog.text
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_check_ratelimit(hacs: HacsBase, caplog: pytest.LogCaptureFixture):
|
||||||
|
hacs.stage = HacsStage.RUNNING
|
||||||
|
await hacs.tasks.async_load()
|
||||||
|
task = hacs.tasks.get("check_ratelimit")
|
||||||
|
|
||||||
|
assert task
|
||||||
|
|
||||||
|
hacs.disable_hacs(HacsDisabledReason.RATE_LIMIT)
|
||||||
|
|
||||||
|
assert hacs.system.disabled
|
||||||
|
assert hacs.system.disabled_reason == HacsDisabledReason.RATE_LIMIT
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"custom_components.hacs.base.HacsBase.async_can_update",
|
||||||
|
return_value=1,
|
||||||
|
):
|
||||||
|
await task.execute_task()
|
||||||
|
|
||||||
|
assert "HacsTask<check_ratelimit> Ratelimit indicate we can update 1" in caplog.text
|
||||||
|
assert not hacs.system.disabled
|
||||||
|
assert hacs.system.disabled_reason is None
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_check_ratelimit_exception(hacs: HacsBase, caplog: pytest.LogCaptureFixture):
|
||||||
|
hacs.stage = HacsStage.RUNNING
|
||||||
|
await hacs.tasks.async_load()
|
||||||
|
task = hacs.tasks.get("check_ratelimit")
|
||||||
|
|
||||||
|
assert task
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"custom_components.hacs.tasks.check_ratelimit.Task.async_execute",
|
||||||
|
side_effect=Exception("lore_ipsum"),
|
||||||
|
):
|
||||||
|
await task.execute_task()
|
||||||
|
assert "HacsTask<check_ratelimit> failed: lore_ipsum" in caplog.text
|
Reference in New Issue
Block a user