Skip to content

Commit 5133e27

Browse files
CoMPaTechbouwew
authored andcommitted
Rework from core
1 parent 778cf3e commit 5133e27

File tree

3 files changed

+66
-54
lines changed

3 files changed

+66
-54
lines changed

custom_components/plugwise/config_flow.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
from copy import deepcopy
6-
from types import MappingProxyType
76
from typing import Any, Self
87

98
from plugwise import Smile
@@ -42,7 +41,6 @@
4241
from homeassistant.core import HomeAssistant, callback
4342
from homeassistant.helpers import config_validation as cv
4443
from homeassistant.helpers.aiohttp_client import async_get_clientsession
45-
from homeassistant.helpers.selector import TextSelector
4644

4745
from .const import (
4846
ANNA_WITH_ADAM,
@@ -73,8 +71,15 @@
7371

7472
# Upstream basically the whole file (excluding the pw-beta options)
7573

74+
SMILE_RECONF_SCHEMA = vol.Schema(
75+
{
76+
vol.Required(CONF_HOST): str,
77+
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
78+
}
79+
)
80+
7681

77-
def base_schema(
82+
def SMILE_USER_SCHEMA(
7883
cf_input: ZeroconfServiceInfo | dict[str, Any] | None,
7984
) -> vol.Schema:
8085
"""Generate base schema for gateways."""
@@ -105,23 +110,10 @@ def base_schema(
105110
)
106111

107112

108-
def reconfigure_schema(reconfigure_data: MappingProxyType[str, Any]) -> vol.Schema:
109-
"""Generate reconfigure schema for gateways."""
110-
return vol.Schema(
111-
{
112-
vol.Required(
113-
CONF_HOST, default=reconfigure_data.get(CONF_HOST)
114-
): TextSelector(),
115-
vol.Required(
116-
CONF_PORT, default=reconfigure_data.get(CONF_PORT)
117-
): vol.Coerce(int),
118-
}
119-
)
120-
121113
async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> Smile:
122114
"""Validate whether the user input allows us to connect to the gateway.
123115
124-
Data has the keys from base_schema() with values provided by the user.
116+
Data has the keys from the schema with values provided by the user.
125117
"""
126118
websession = async_get_clientsession(hass, verify_ssl=False)
127119
api = Smile(
@@ -216,7 +208,7 @@ def is_matching(self, other_flow: Self) -> bool:
216208
async def _verify_connection(
217209
self, user_input: dict[str, Any]
218210
) -> tuple[Smile | None, dict[str, str]]:
219-
"""Verify gateway connection helper function user and reconfiguration steps."""
211+
"""Verify and return the gateway connection using helper function."""
220212
errors: dict[str, str] = {}
221213

222214
try:
@@ -234,9 +226,6 @@ async def _verify_connection(
234226
except Exception: # noqa: BLE001
235227
errors[CONF_BASE] = "unknown"
236228
else:
237-
await self.async_set_unique_id(
238-
api.smile_hostname or api.gateway_id, raise_on_progress=False
239-
)
240229
return (api, errors)
241230
return (None, errors)
242231

@@ -255,12 +244,15 @@ async def async_step_user(
255244

256245
api, errors = await self._verify_connection(user_input)
257246
if not errors and api:
247+
await self.async_set_unique_id(
248+
api.smile_hostname or api.gateway_id, raise_on_progress=False
249+
)
258250
self._abort_if_unique_id_configured()
259251
return self.async_create_entry(title=api.smile_name, data=user_input)
260252

261253
return self.async_show_form(
262254
step_id=SOURCE_USER,
263-
data_schema=base_schema(self.discovery_info),
255+
data_schema=SMILE_USER_SCHEMA(self.discovery_info),
264256
errors=errors,
265257
)
266258

@@ -274,27 +266,31 @@ async def async_step_reconfigure(
274266

275267
if user_input:
276268
# Redefine ingest existing username and password
277-
user_input = {
269+
full_input = {
278270
CONF_HOST: user_input.get(CONF_HOST),
279271
CONF_PORT: user_input.get(CONF_PORT),
280272
CONF_USERNAME: reconfigure_entry.data.get(CONF_USERNAME),
281273
CONF_PASSWORD: reconfigure_entry.data.get(CONF_PASSWORD),
282274
}
283275

284-
_, errors = await self._verify_connection(user_input)
285-
if not errors:
276+
api, errors = await self._verify_connection(full_input)
277+
if not errors and api:
278+
await self.async_set_unique_id(
279+
api.smile_hostname or api.gateway_id, raise_on_progress=False
280+
)
286281
self._abort_if_unique_id_mismatch(reason="not_the_same_smile")
287282
return self.async_update_reload_and_abort(
288283
self._get_reconfigure_entry(),
289-
data_updates=user_input,
284+
data_updates=full_input,
290285
)
291286

292287
return self.async_show_form(
293288
step_id="reconfigure",
294-
data_schema=reconfigure_schema(reconfigure_entry.data),
295-
description_placeholders={
296-
"title": reconfigure_entry.title,
297-
},
289+
data_schema=self.add_suggested_values_to_schema(
290+
data_schema=SMILE_RECONF_SCHEMA,
291+
suggested_values=reconfigure_entry.data,
292+
),
293+
description_placeholders={"title": reconfigure_entry.title},
298294
errors=errors,
299295
)
300296

custom_components/plugwise/translations/en.json

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"already_configured": "This device is already configured",
55
"anna_with_adam": "Both Anna and Adam detected. Add your Adam instead of your Anna",
66
"not_the_same_smile": "The configured Smile ID does not match the Smile ID on the requested IP address.",
7-
"reconfigure_successful": "Re-configuration was successful"
7+
"reconfigure_successful": "Reconfiguration successful"
88
},
99
"error": {
1010
"cannot_connect": "Failed to connect",
@@ -20,16 +20,16 @@
2020
"step": {
2121
"reconfigure": {
2222
"data": {
23-
"host": "IP address",
24-
"port": "Port"
23+
"host": "IP-address",
24+
"port": "Port number"
2525
},
2626
"description": "Update configuration for {title}."
2727
},
2828
"user": {
2929
"data": {
30-
"host": "IP address",
30+
"host": "IP-address",
3131
"password": "ID",
32-
"port": "Port",
32+
"port": "Port number",
3333
"username": "Username"
3434
},
3535
"description": "Enter your Plugwise device: (setup can take up to 90s)",
@@ -57,9 +57,6 @@
5757
"heating_state": {
5858
"name": "Heating"
5959
},
60-
"low_battery": {
61-
"name": "Battery state"
62-
},
6360
"plugwise_notification": {
6461
"name": "Plugwise notification"
6562
},
@@ -288,6 +285,26 @@
288285
}
289286
}
290287
},
288+
"exceptions": {
289+
"authentication_failed": {
290+
"message": "Invalid authentication"
291+
},
292+
"data_incomplete_or_missing": {
293+
"message": "Data incomplete or missing."
294+
},
295+
"error_communicating_with_api": {
296+
"message": "Error communicating with API: {error}."
297+
},
298+
"failed_to_connect": {
299+
"message": "Failed to connect"
300+
},
301+
"invalid_xml_data": {
302+
"message": "Invalid XML data, or error indication received from the Plugwise Adam/Smile/Stretch"
303+
},
304+
"unsupported_firmware": {
305+
"message": "Device with unsupported firmware"
306+
}
307+
},
291308
"options": {
292309
"step": {
293310
"init": {

tests/components/plugwise/test_select.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
)
1212
from homeassistant.const import ATTR_ENTITY_ID
1313
from homeassistant.core import HomeAssistant
14-
from homeassistant.exceptions import ServiceValidationError
1514

1615
from tests.common import MockConfigEntry
1716

@@ -89,18 +88,18 @@ async def test_legacy_anna_select_entities(
8988
assert not hass.states.get("select.anna_thermostat_schedule")
9089

9190

92-
async def test_adam_select_unavailable_regulation_mode(
93-
hass: HomeAssistant, mock_smile_anna: MagicMock, init_integration: MockConfigEntry
94-
) -> None:
95-
"""Test a regulation_mode non-available preset."""
96-
97-
with pytest.raises(ServiceValidationError, match="valid options"):
98-
await hass.services.async_call(
99-
SELECT_DOMAIN,
100-
SERVICE_SELECT_OPTION,
101-
{
102-
ATTR_ENTITY_ID: "select.anna_thermostat_schedule",
103-
ATTR_OPTION: "freezing",
104-
},
105-
blocking=True,
106-
)
91+
#async def test_anna_select_unavailable_regulation_mode(
92+
# hass: HomeAssistant, mock_smile_anna: MagicMock, init_integration: MockConfigEntry
93+
#) -> None:
94+
# """Test a regulation_mode non-available preset."""
95+
#
96+
# with pytest.raises(ServiceValidationError, match="valid options"):
97+
# await hass.services.async_call(
98+
# SELECT_DOMAIN,
99+
# SERVICE_SELECT_OPTION,
100+
# {
101+
# ATTR_ENTITY_ID: "select.anna_thermostat_schedule",
102+
# ATTR_OPTION: "freezing",
103+
# },
104+
# blocking=True,
105+
# )

0 commit comments

Comments
 (0)