Skip to content

Commit 3a57957

Browse files
Create new configuration options
Create new configuration options , it is posible to change ip host, prefix, supports heating and cooling
1 parent b99986e commit 3a57957

File tree

7 files changed

+112
-88
lines changed

7 files changed

+112
-88
lines changed

custom_components/uhomeuponor/__init__.py

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,51 @@
33
For more details about this platform, please refer to the documentation at
44
https://github.com/fcastroruiz/uhomeuponor
55
"""
6-
from homeassistant.const import CONF_HOST
76
from logging import getLogger
7+
import asyncio
88
from homeassistant.core import HomeAssistant
99
from homeassistant.const import Platform
1010
from homeassistant.config_entries import ConfigEntry
11+
from homeassistant.helpers import device_registry, entity_registry
1112

1213
_LOGGER = getLogger(__name__)
1314
DOMAIN = "uhomeuponor"
1415
PLATFORMS = [Platform.SENSOR, Platform.CLIMATE]
1516

1617
async def async_setup(hass: HomeAssistant, config: dict):
1718
"""Set up this integration using UI."""
18-
_LOGGER.info("Loading setup")
1919
hass.data.setdefault(DOMAIN, {})
2020
hass.data[DOMAIN]["config"] = config.get(DOMAIN) or {}
2121
return True
2222

2323
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
2424
"""Set up this integration using UI."""
25-
#host = config_entry.data[CONF_HOST]
2625
_LOGGER.info("Loading setup entry")
2726

28-
# hass.async_create_task(hass.config_entries.async_forward_entry_setup(config_entry, "climate"))
29-
# hass.async_create_task(hass.config_entries.async_forward_entry_setup(config_entry, "sensor"))
30-
hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)
27+
# hass.config_entries.async_setup_platforms(config_entry, PLATFORMS)
28+
if config_entry.options:
29+
if config_entry.data != config_entry.options:
30+
dev_reg = device_registry.async_get(hass)
31+
ent_reg = entity_registry.async_get(hass)
32+
dev_reg.async_clear_config_entry(config_entry.entry_id)
33+
ent_reg.async_clear_config_entry(config_entry.entry_id)
34+
hass.config_entries.async_update_entry(config_entry, data=config_entry.options)
35+
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
3136

37+
config_entry.async_on_unload(config_entry.add_update_listener(async_update_options))
38+
3239
return True
3340

41+
async def async_update_options(hass: HomeAssistant, entry: ConfigEntry) -> None:
42+
"""Update options."""
43+
_LOGGER.debug("Update setup entry: %s, data: %s, options: %s", entry.entry_id, entry.data, entry.options)
44+
await hass.config_entries.async_reload(entry.entry_id)
45+
3446
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
3547
"""Unload a config entry."""
36-
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
48+
_LOGGER.debug("Unloading setup entry: %s, data: %s, options: %s", config_entry.entry_id, config_entry.data, config_entry.options)
49+
unload_ok = await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
50+
return unload_ok
51+
#if unload_ok:
52+
# hass.data[DOMAIN].pop(config_entry.entry_id)
53+

custom_components/uhomeuponor/climate.py

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@
3030
CONF_SUPPORTS_HEATING = "supports_heating"
3131
CONF_SUPPORTS_COOLING = "supports_cooling"
3232

33-
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
34-
vol.Required(CONF_HOST): cv.string,
35-
vol.Optional(CONF_PREFIX): cv.string,
36-
vol.Optional(CONF_SUPPORTS_HEATING, default=True): cv.boolean,
37-
vol.Optional(CONF_SUPPORTS_COOLING, default=True): cv.boolean,
38-
})
39-
4033
ATTR_TECHNICAL_ALARM = "technical_alarm"
4134
ATTR_RF_SIGNAL_ALARM = "rf_alarm"
4235
ATTR_BATTERY_ALARM = "battery_alarm"
@@ -45,22 +38,20 @@
4538

4639

4740
async def async_setup_entry(hass, config_entry, async_add_entities):
48-
_LOGGER.info("init setup climate platform for %s", config_entry)
49-
return await async_setup_platform(
50-
hass, config_entry.data, async_add_entities, discovery_info=None
41+
_LOGGER.info("init setup climate platform for id: %s data: %s, options: %s", config_entry.entry_id, config_entry.data, config_entry.options)
42+
config = config_entry.data
43+
return await async_setup_climate(
44+
hass, config, async_add_entities, discovery_info=None
5145
)
5246

53-
async def async_setup_platform(
47+
async def async_setup_climate(
5448
hass, config, async_add_entities, discovery_info=None
5549
) -> bool:
56-
"""Set up the Alexa alarm control panel platform."""
5750
"""Set up climate for device."""
58-
_LOGGER.info("init setup climate platform for %s", config)
59-
6051
host = config[CONF_HOST]
6152
prefix = config[CONF_PREFIX]
62-
supports_heating = True
63-
supports_cooling = True
53+
supports_heating = config[CONF_SUPPORTS_HEATING] or True
54+
supports_cooling = config[CONF_SUPPORTS_COOLING] or True
6455

6556
_LOGGER.info("init setup host %s", host)
6657

custom_components/uhomeuponor/config_flow.py

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
import asyncio
23
from homeassistant import config_entries
34
from homeassistant.config_entries import ConfigEntry
45
from homeassistant.core import callback
@@ -8,6 +9,8 @@
89

910
_LOGGER = logging.getLogger(__name__)
1011
DOMAIN = "uhomeuponor"
12+
CONF_SUPPORTS_HEATING = "supports_heating"
13+
CONF_SUPPORTS_COOLING = "supports_cooling"
1114

1215
class UhomeuponorConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
1316
"""Uponor config flow."""
@@ -16,22 +19,25 @@ class UhomeuponorConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
1619

1720
async def async_step_user(self, user_input=None):
1821
errors = {}
22+
_LOGGER.info("Init config step uhomeuponor")
1923
if self._async_current_entries():
2024
return self.async_abort(reason="single_instance_allowed")
2125
if user_input is not None:
22-
_LOGGER.info ("user_input: %s", user_input)
26+
_LOGGER.debug("user_input: %s", user_input)
2327
# Validate user input
2428
#valid = await is_valid(user_input)
2529
#if valid:
2630
#title = f"{self.info[CONF_HOST]} - {self.device_id}"
2731
title = f"Uhome Uponor"
28-
prefix = user_input.get(CONF_PREFIX) if user_input.get(CONF_PREFIX) else ""
32+
data={
33+
CONF_HOST: user_input[CONF_HOST],
34+
CONF_PREFIX: (user_input.get(CONF_PREFIX) if user_input.get(CONF_PREFIX) else ""),
35+
CONF_SUPPORTS_HEATING: user_input[CONF_SUPPORTS_HEATING],
36+
CONF_SUPPORTS_COOLING: user_input[CONF_SUPPORTS_COOLING]}
2937
return self.async_create_entry(
3038
title=title,
31-
data={
32-
"host": user_input[CONF_HOST],
33-
"prefix": prefix,
34-
},
39+
data=data
40+
# options=data
3541
)
3642

3743
return self.async_show_form(
@@ -40,56 +46,58 @@ async def async_step_user(self, user_input=None):
4046
{
4147
vol.Required(CONF_HOST): str,
4248
vol.Optional(CONF_PREFIX): str,
49+
vol.Optional(CONF_SUPPORTS_HEATING, default=True): bool,
50+
vol.Optional(CONF_SUPPORTS_COOLING, default=True): bool,
4351
}
4452
), errors=errors
4553
)
4654

47-
# @staticmethod
48-
# @callback
49-
# def async_get_options_flow(
50-
# config_entry: ConfigEntry,
51-
# ) -> UhomeuponorOptionsFlowHandler:
52-
# """Options callback for uponor."""
53-
# return UhomeuponorOptionsFlowHandler(config_entry)
55+
@staticmethod
56+
@callback
57+
def async_get_options_flow(entry: config_entries.ConfigEntry):
58+
return OptionsFlowHandler(entry)
5459

60+
class OptionsFlowHandler(config_entries.OptionsFlow):
5561

56-
# class UhomeuponorOptionsFlowHandler(config_entries.OptionsFlow):
57-
# """Config flow options for uponor."""
62+
def __init__(self, config_entry):
63+
"""Initialize options flow."""
64+
self.config_entry = config_entry
5865

59-
# def __init__(self, entry: ConfigEntry) -> None:
60-
# """Initialize AccuWeather options flow."""
61-
# self.config_entry = entry
62-
63-
# async def async_step_init(self, user_input=None):
64-
# """Manage the options."""
65-
# return await self.async_step_user()
66-
67-
# async def async_step_user(self, user_input=None):
68-
# errors = {}
69-
# if user_input is not None:
70-
# _LOGGER.info ("Aqui debemos hacer algo con user_input: %s", user_input)
71-
# # Validate user input
72-
# #valid = await is_valid(user_input)
73-
# #if valid:
74-
# #title = f"{self.info[CONF_HOST]} - {self.device_id}"
75-
# title = f"Uhome Uponor"
76-
# return self.async_create_entry(
77-
# title=title,
78-
# data={
79-
# "host": user_input[CONF_HOST],
80-
# "prefix": user_input[CONF_PREFIX],
81-
# },
82-
# )
66+
async def async_step_init(self, _user_input=None):
67+
"""Manage the options."""
68+
return await self.async_step_user()
69+
70+
async def async_step_user(self, user_input=None):
71+
"""Handle a flow initialized by the user."""
72+
_LOGGER.debug("entra en step user: %s", user_input)
73+
_LOGGER.info("Init Option config step uhomeuponor")
74+
errors = {}
75+
options = self.config_entry.data
76+
if user_input is not None:
77+
data={
78+
CONF_HOST: user_input[CONF_HOST],
79+
CONF_PREFIX: user_input[CONF_PREFIX],
80+
CONF_SUPPORTS_HEATING: user_input[CONF_SUPPORTS_HEATING],
81+
CONF_SUPPORTS_COOLING: user_input[CONF_SUPPORTS_COOLING],
82+
}
83+
_LOGGER.debug("user_input data: %s, id: %s", data, self.config_entry.entry_id)
84+
title = f"Uhome Uponor"
85+
return self.async_create_entry(
86+
title=title,
87+
data=data
88+
)
8389

84-
# return self.async_show_form(
85-
# step_id="user",
86-
# data_schema=vol.Schema(
87-
# {
88-
# vol.Required(CONF_HOST): str,
89-
# vol.Optional(CONF_PREFIX): str,
90-
# }
91-
# ), errors=errors
92-
# )
90+
return self.async_show_form(
91+
step_id="user",
92+
data_schema=vol.Schema(
93+
{
94+
vol.Required(CONF_HOST, default=options.get(CONF_HOST)): str,
95+
vol.Optional(CONF_PREFIX, default=options.get(CONF_PREFIX)): str,
96+
vol.Optional(CONF_SUPPORTS_HEATING, default=options.get(CONF_SUPPORTS_HEATING)): bool,
97+
vol.Optional(CONF_SUPPORTS_COOLING, default=options.get(CONF_SUPPORTS_COOLING)): bool,
98+
}
99+
), errors=errors
100+
)
93101

94102
# class UhomeuponorDicoveryFlow(DiscoveryFlowHandler[Awaitable[bool]], domain=DOMAIN):
95103
# """Discovery flow handler."""

custom_components/uhomeuponor/sensor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,16 @@
3535

3636

3737
async def async_setup_entry(hass, config_entry, async_add_entities):
38-
_LOGGER.info("init setup sensor platform for %s", config_entry)
39-
return await async_setup_platform(
40-
hass, config_entry.data, async_add_entities, discovery_info=None
38+
_LOGGER.info("init setup sensor platform for id: %s data: %s, options: %s", config_entry.entry_id, config_entry.data, config_entry.options)
39+
config = config_entry.data
40+
return await async_setup_sensor(
41+
hass, config, async_add_entities, discovery_info=None
4142
)
4243

43-
async def async_setup_platform(
44+
async def async_setup_sensor(
4445
hass, config, async_add_entities, discovery_info=None
4546
) -> bool:
46-
_LOGGER.info("init setup sensor platform for %s", config)
47-
47+
4848
host = config[CONF_HOST]
4949
prefix = config[CONF_PREFIX]
5050

custom_components/uhomeuponor/translations/en.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"description": "Config Uponor system",
1515
"data": {
1616
"host": "Host or IP address of the Uponor Gateway",
17-
"prefix": "Entities prefix"
17+
"prefix": "Entities prefix",
18+
"supports_heating": "Uponor supports heating",
19+
"supports_cooling": "Uponor supports cooling"
1820
}
1921
}
2022
}
@@ -26,7 +28,9 @@
2628
"description": "Config Uponor system",
2729
"data": {
2830
"host": "Host or IP address of the Uponor Gateway",
29-
"prefix": "Entities prefix"
31+
"prefix": "Entities prefix",
32+
"supports_heating": "Uponor supports heating",
33+
"supports_cooling": "Uponor supports cooling"
3034
}
3135
}
3236
}

custom_components/uhomeuponor/translations/es.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
"description": "Configurar Uponor",
1515
"data": {
1616
"host": "Host o dirección IP del Gateway Uponor",
17-
"prefix": "Prefijo para las entidades"
17+
"prefix": "Prefijo para las entidades",
18+
"supports_heating": "Uponor soporta calentar",
19+
"supports_cooling": "Uponor soporta refrigerar"
1820
}
1921
}
2022
}
@@ -26,7 +28,9 @@
2628
"description": "Configurar Uponor",
2729
"data": {
2830
"host": "Host o dirección IP del Gateway Uponor",
29-
"prefix": "Prefijo para las entidades"
31+
"prefix": "Prefijo para las entidades",
32+
"supports_heating": "Uponor soporta calentar",
33+
"supports_cooling": "Uponor soporta refrigerar"
3034
}
3135
}
3236
}

custom_components/uhomeuponor/uponor_api/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ async def init_controllers(self):
5858
# Controller i is present
5959
self.controllers.append(UponorController(self, i))
6060

61-
_LOGGER.debug("Identified %d controllers", len(self.controllers))
61+
#_LOGGER.debug("Identified %d controllers", len(self.controllers))
6262

6363
# Update all controllers
6464
await self.update_devices(self.controllers)
@@ -81,7 +81,7 @@ async def init_thermostats(self):
8181
# Thermostat i is present
8282
self.thermostats.append(UponorThermostat(self, controller.controller_index, i))
8383

84-
_LOGGER.debug("Identified %d thermostats on %d controllers", len(self.thermostats), len(self.controllers))
84+
#_LOGGER.debug("Identified %d thermostats on %d controllers", len(self.thermostats), len(self.controllers))
8585

8686
# Update all thermostats
8787
await self.update_devices(self.thermostats)
@@ -115,7 +115,7 @@ async def do_rest_call(self, requestObject):
115115

116116
response_data = json.loads(response.text)
117117

118-
_LOGGER.debug("Issued API request type '%s' for %d objects, return code %d", requestObject['method'], len(requestObject['params']['objects']), response.status_code)
118+
#_LOGGER.debug("Issued API request type '%s' for %d objects, return code %d", requestObject['method'], len(requestObject['params']['objects']), response.status_code)
119119

120120
return response_data
121121

@@ -141,7 +141,7 @@ async def update_devices(self, *devices):
141141
for value in allvalues:
142142
allvalue_dict[value.id] = value
143143

144-
_LOGGER.debug("Requested update %d values of %d devices, skipped %d devices", len(values), len(devices_to_update), len(devices) - len(devices_to_update))
144+
#_LOGGER.debug("Requested update %d values of %d devices, skipped %d devices", len(values), len(devices_to_update), len(devices) - len(devices_to_update))
145145

146146
# Update all values, but at most N at a time
147147
for value_list in chunks(values, self.max_values_batch):
@@ -157,7 +157,7 @@ async def update_values(self, allvalue_dict, *values):
157157
if len(values) == 0:
158158
return
159159

160-
_LOGGER.debug("Requested update of %d values", len(values))
160+
#_LOGGER.debug("Requested update of %d values", len(values))
161161

162162
value_dict = {}
163163
for value in values:
@@ -260,7 +260,7 @@ def validate_values(self,response_data,allvalue_dict):
260260
def set_values(self, *value_tuples):
261261
"""Writes values to UHome, accepts tuples of (UponorValue, New Value)"""
262262

263-
_LOGGER.debug("Requested write to %d values", len(value_tuples))
263+
#_LOGGER.debug("Requested write to %d values", len(value_tuples))
264264

265265
req = self.create_request("write")
266266

@@ -312,7 +312,7 @@ def attributes(self):
312312
return attr
313313

314314
async def async_update(self):
315-
_LOGGER.debug("Updating %s, device '%s'", self.__class__.__name__, self.identity_string)
315+
#_LOGGER.debug("Updating %s, device '%s'", self.__class__.__name__, self.identity_string)
316316

317317
await self.uponor_client.update_devices(self)
318318

0 commit comments

Comments
 (0)