Skip to content

Commit

Permalink
De-dup repair tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
amitfin committed Apr 30, 2024
1 parent d340b71 commit 107c61e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
10 changes: 6 additions & 4 deletions custom_components/retry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import asyncio
import datetime
import functools
import logging
import uuid
import voluptuous as vol

from homeassistant.components.hassio.const import ATTR_DATA
Expand Down Expand Up @@ -288,6 +288,8 @@ def _check_validation(self) -> bool:
)
)

@property
@functools.cache
def _service_call_str(self) -> str:
"""Return a string with the service call parameters."""
service_call = (
Expand Down Expand Up @@ -322,7 +324,7 @@ def _log(self, level: int, prefix: str, stack_info: bool = False) -> None:
prefix,
self._attempt,
self._params.retry_data[ATTR_RETRIES],
self._service_call_str(),
self._service_call_str,
exc_info=stack_info,
)

Expand All @@ -331,13 +333,13 @@ def _repair(self) -> None:
ir.async_create_issue(
self._hass,
DOMAIN,
f"retry_{uuid.uuid4()}",
self._service_call_str,
is_fixable=False,
learn_more_url="https://github.com/amitfin/retry#retrycall",
severity=ir.IssueSeverity.ERROR,
translation_key="failure",
translation_placeholders={
"service": self._service_call_str(),
"service": self._service_call_str,
"retries": self._params.retry_data[ATTR_RETRIES],
},
)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ async def test_failure(
assert len(repairs) == 1
assert repairs[0].data["action"] == "create"
assert repairs[0].data["domain"] == DOMAIN
assert repairs[0].data["issue_id"] == f"{DOMAIN}.{TEST_SERVICE}()"


async def test_entity_unavailable(
Expand Down Expand Up @@ -517,6 +518,19 @@ async def test_disable_repair(
assert not len(repairs)


async def test_identical_repair(
hass: HomeAssistant,
freezer: FrozenDateTimeFactory,
) -> None:
"""Test de-dup of identical repair tickets."""
repairs = async_capture_events(hass, ir.EVENT_REPAIRS_ISSUE_REGISTRY_UPDATED)
await async_setup(hass)
for _ in range(2):
await async_call(hass)
await async_shutdown(hass, freezer)
assert len(repairs) == 1


async def test_unload(hass: HomeAssistant) -> None:
"""Test we abort if already setup."""
config_entry = MockConfigEntry(domain=DOMAIN)
Expand Down

0 comments on commit 107c61e

Please sign in to comment.