-
-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Expand file tree
/
Copy pathrepairs.py
More file actions
113 lines (95 loc) · 3.85 KB
/
Copy pathrepairs.py
File metadata and controls
113 lines (95 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""Repairs platform for the Min/Max integration."""
from types import MappingProxyType
from typing import Any, cast
import voluptuous as vol
from homeassistant import data_entry_flow
from homeassistant.components.group import (
CONF_ENTITIES,
CONF_GROUP_TYPE,
CONF_HIDE_MEMBERS,
CONF_IGNORE_NON_NUMERIC,
DOMAIN as GROUP_DOMAIN,
)
from homeassistant.components.repairs import ConfirmRepairFlow, RepairsFlow
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.config_entries import SOURCE_USER, ConfigEntry, ConfigEntryDisabler
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from .const import CONF_ENTITY_IDS, CONF_ROUND_DIGITS, DOMAIN
class MigrateToGroupSensorFlow(RepairsFlow):
"""Handler for an issue fixing flow."""
def __init__(self, entry: ConfigEntry) -> None:
"""Create flow."""
self.entry = entry
super().__init__()
async def async_step_init(
self, user_input: dict[str, str] | None = None
) -> data_entry_flow.FlowResult:
"""Handle the first step of a fix flow."""
return await self.async_step_migrate()
async def async_step_migrate(
self, user_input: dict[str, Any] | None = None
) -> data_entry_flow.FlowResult:
"""Handle the migration step of a fix flow."""
errors: dict[str, str] = {}
entity_reg = er.async_get(self.hass)
old_entity = entity_reg.async_get_entity_id(
SENSOR_DOMAIN, DOMAIN, self.entry.entry_id
)
if not old_entity:
return self.async_abort(reason="entity_not_found")
if user_input is not None:
config = dict(self.entry.options)
config[CONF_ENTITIES] = config.pop(CONF_ENTITY_IDS)
config.pop(CONF_ROUND_DIGITS)
# Set group sensor defaults
config[CONF_HIDE_MEMBERS] = False
config[CONF_IGNORE_NON_NUMERIC] = False
config[CONF_GROUP_TYPE] = SENSOR_DOMAIN
new_config_entry = ConfigEntry(
data={},
discovery_keys=MappingProxyType({}),
domain=GROUP_DOMAIN,
minor_version=1,
options=config,
source=SOURCE_USER,
subentries_data=[],
title=self.entry.title,
unique_id=None,
version=1,
disabled_by=ConfigEntryDisabler.USER,
)
await self.hass.config_entries.async_unload(self.entry.entry_id)
await self.hass.config_entries.async_add(new_config_entry)
entity_reg.async_update_entity_platform(
old_entity,
GROUP_DOMAIN,
new_config_entry_id=new_config_entry.entry_id,
new_unique_id=new_config_entry.entry_id,
)
await self.hass.config_entries.async_set_disabled_by(
entry_id=new_config_entry.entry_id, disabled_by=None
)
await self.hass.config_entries.async_remove(self.entry.entry_id)
return self.async_create_entry(data={})
entity_info = entity_reg.async_get(old_entity)
assert entity_info
title = er.async_get_full_entity_name(self.hass, entity_info)
return self.async_show_form(
step_id="migrate",
data_schema=vol.Schema({}),
errors=errors,
description_placeholders={"title": title},
)
async def async_create_fix_flow(
hass: HomeAssistant,
issue_id: str,
data: dict[str, Any] | None,
) -> RepairsFlow:
"""Create flow."""
if data and (entry_id := data.get("entry_id")):
entry_id = cast(str, entry_id)
entry = hass.config_entries.async_get_entry(entry_id)
assert entry
return MigrateToGroupSensorFlow(entry)
return ConfirmRepairFlow()