repairs.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """Repairs platform for HACS."""
  2. from __future__ import annotations
  3. from typing import Any
  4. import voluptuous as vol
  5. from homeassistant import data_entry_flow
  6. from homeassistant.components.repairs import RepairsFlow
  7. from homeassistant.core import HomeAssistant
  8. from custom_components.hacs.base import HacsBase
  9. from .const import DOMAIN
  10. class RestartRequiredFixFlow(RepairsFlow):
  11. """Handler for an issue fixing flow."""
  12. def __init__(self, issue_id: str) -> None:
  13. self.issue_id = issue_id
  14. async def async_step_init(
  15. self, user_input: dict[str, str] | None = None
  16. ) -> data_entry_flow.FlowResult:
  17. """Handle the first step of a fix flow."""
  18. return await (self.async_step_confirm_restart())
  19. async def async_step_confirm_restart(
  20. self, user_input: dict[str, str] | None = None
  21. ) -> data_entry_flow.FlowResult:
  22. """Handle the confirm step of a fix flow."""
  23. if user_input is not None:
  24. await self.hass.services.async_call("homeassistant", "restart")
  25. return self.async_create_entry(title="", data={})
  26. hacs: HacsBase = self.hass.data[DOMAIN]
  27. integration = hacs.repositories.get_by_id(self.issue_id.split("_")[2])
  28. return self.async_show_form(
  29. step_id="confirm_restart",
  30. data_schema=vol.Schema({}),
  31. description_placeholders={"name": integration.display_name},
  32. )
  33. async def async_create_fix_flow(
  34. hass: HomeAssistant,
  35. issue_id: str,
  36. data: dict[str, str | int | float | None] | None = None,
  37. *args: Any,
  38. **kwargs: Any,
  39. ) -> RepairsFlow | None:
  40. """Create flow."""
  41. if issue_id.startswith("restart_required"):
  42. return RestartRequiredFixFlow(issue_id)
  43. return None