Fix from feedback on supervisor issues to repairs (#91680)

* Fix from feedback on supervisor issues to repairs

* Use cls parameter in classmethods
This commit is contained in:
Mike Degatano 2023-04-19 19:02:40 -04:00 committed by GitHub
parent 6342992791
commit 24fe6dfc63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 13 deletions

View File

@ -115,10 +115,10 @@ class Suggestion:
"""Get key for suggestion (combination of context and type)."""
return f"{self.context}_{self.type_}"
@staticmethod
def from_dict(data: SuggestionDataType) -> Suggestion:
@classmethod
def from_dict(cls, data: SuggestionDataType) -> Suggestion:
"""Convert from dictionary representation."""
return Suggestion(
return cls(
uuid=data["uuid"],
type_=data["type"],
context=data["context"],
@ -151,11 +151,11 @@ class Issue:
"""Get key for issue (combination of context and type)."""
return f"issue_{self.context}_{self.type_}"
@staticmethod
def from_dict(data: IssueDataType) -> Issue:
@classmethod
def from_dict(cls, data: IssueDataType) -> Issue:
"""Convert from dictionary representation."""
suggestions: list[SuggestionDataType] = data.get("suggestions", [])
return Issue(
return cls(
uuid=data["uuid"],
type_=data["type"],
context=data["context"],
@ -244,6 +244,9 @@ class SupervisorIssues:
def add_issue(self, issue: Issue) -> None:
"""Add or update an issue in the list. Create or update a repair if necessary."""
if issue.key in ISSUE_KEYS_FOR_REPAIRS:
placeholders: dict[str, str] | None = None
if issue.reference:
placeholders = {PLACEHOLDER_KEY_REFERENCE: issue.reference}
async_create_issue(
self._hass,
DOMAIN,
@ -251,9 +254,7 @@ class SupervisorIssues:
is_fixable=bool(issue.suggestions),
severity=IssueSeverity.WARNING,
translation_key=issue.key,
translation_placeholders={PLACEHOLDER_KEY_REFERENCE: issue.reference}
if issue.reference
else None,
translation_placeholders=placeholders,
)
self._issues[issue.uuid] = issue

View File

@ -59,12 +59,13 @@ class SupervisorIssueRepairFlow(RepairsFlow):
async def async_step_init(self, _: None = None) -> FlowResult:
"""Handle the first step of a fix flow."""
# Got out of sync with supervisor, issue is resolved or isn't fixable. Either way, resolve the repair
# Out of sync with supervisor, issue is resolved or not fixable. Remove it
if not self.issue or not self.issue.suggestions:
return self.async_create_entry(data={})
# All suggestions do the same thing: apply them in supervisor, optionally with a confirmation step.
# Generating the required handler for each allows for shared logic but screens can still be translated per step id.
# All suggestions have the same logic: Apply them in supervisor,
# optionally with a confirmation step. Generating the required handler for each
# allows for shared logic but screens can still be translated per step id.
for suggestion in self.issue.suggestions:
setattr(
self,
@ -79,7 +80,7 @@ class SupervisorIssueRepairFlow(RepairsFlow):
description_placeholders=self.description_placeholders,
)
# Always show a form if there's only one suggestion so we can explain to the user what's happening
# Always show a form for one suggestion to explain to user what's happening
return self._async_form_for_suggestion(self.issue.suggestions[0])
async def _async_step_apply_suggestion(