This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
config_flow.py
115 lines (96 loc) · 4.05 KB
/
config_flow.py
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
114
115
import asyncio
from .triones import discover, TrionesInstance
from typing import Any
from homeassistant import config_entries
from homeassistant.const import CONF_MAC
import voluptuous as vol
from homeassistant.helpers.device_registry import format_mac
from .const import DOMAIN, LOGGER
DATA_SCHEMA = vol.Schema({("host"): str})
MANUAL_MAC = "manual"
class TrionesFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
def __init__(self) -> None:
self.mac = None
self.triones_instance = None
self.name = None
async def async_step_user(self, user_input=None):
"""Handle the initial step."""
if user_input is not None:
if user_input["mac"] == MANUAL_MAC:
return await self.async_step_manual()
self.mac = user_input["mac"]
self.name = user_input["name"]
await self.async_set_unique_id(format_mac(self.mac))
return await self.async_step_validate()
already_configured = self._async_current_ids(False)
devices = await discover()
devices = [device for device in devices if format_mac(device.address) not in already_configured]
if not devices:
return await self.async_step_manual()
return self.async_show_form(
step_id="user", data_schema=vol.Schema(
{
vol.Required("mac"): vol.In(
{
**{device.address: device.name for device in devices},
MANUAL_MAC: "Manually add a MAC address",
}
),
vol.Required("name"): str
}
),
errors={})
async def async_step_validate(self, user_input: "dict[str, Any] | None" = None):
if user_input is not None:
if "flicker" in user_input:
if user_input["flicker"]:
return self.async_create_entry(title=self.name, data={CONF_MAC: self.mac, "name": self.name})
return self.async_abort(reason="cannot_validate")
if "retry" in user_input and not user_input["retry"]:
return self.async_abort(reason="cannot_connect")
error = await self.toggle_light()
if error:
return self.async_show_form(
step_id="validate", data_schema=vol.Schema(
{
vol.Required("retry"): bool
}
), errors={"base": "connect"})
return self.async_show_form(
step_id="validate", data_schema=vol.Schema(
{
vol.Required("flicker"): bool
}
), errors={})
async def async_step_manual(self, user_input: "dict[str, Any] | None" = None):
if user_input is not None:
self.mac = user_input["mac"]
self.name = user_input["name"]
await self.async_set_unique_id(format_mac(self.mac))
return await self.async_step_validate()
return self.async_show_form(
step_id="manual", data_schema=vol.Schema(
{
vol.Required("mac"): str,
vol.Required("name"): str
}
), errors={})
async def toggle_light(self):
if not self.triones_instance:
self.triones_instance = TrionesInstance(self.mac)
try:
await self.triones_instance.update()
if self.triones_instance.is_on:
await self.triones_instance.turn_off()
await asyncio.sleep(2)
await self.triones_instance.turn_on()
else:
await self.triones_instance.turn_on()
await asyncio.sleep(2)
await self.triones_instance.turn_off()
except (Exception) as error:
return error
finally:
await self.triones_instance.disconnect()