Skip to content

Commit

Permalink
Customizable entry config
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-dcs committed Feb 8, 2025
1 parent 5e8b7b4 commit cc321ff
Show file tree
Hide file tree
Showing 13 changed files with 238 additions and 64 deletions.
17 changes: 14 additions & 3 deletions custom_components/ithodaalderop/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant

from .const import CONF_ADDON_TYPE, CONF_ENTITIES_CREATION_MODE, CONF_NONCVE_MODEL
from .const import (
CONF_ADDON_TYPE,
CONF_ADVANCED_CONFIG,
CONF_ENTITIES_CREATION_MODE,
CONF_NONCVE_MODEL,
)

PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up the Itho Wifi add-on integration."""

# Migrate noncve config < v2.2.0 to 2.2.0+
# Migrate noncve config from < v2.2.0
# < 2.2.0 Only 1 HRU was supported. This was the HRU ECO 350
# So, if no hru_device is setup, this has to be the hru_eco_350
if (
Expand All @@ -23,12 +28,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
new_data[CONF_NONCVE_MODEL] = "hru_eco_350"
hass.config_entries.async_update_entry(entry, data=new_data)

# Migrate entities_creation_mode config < v2.2.0 to 2.2.0+
# Migrate entities_creation_mode config from < v2.2.0
if entry.data.get(CONF_ENTITIES_CREATION_MODE) is None:
new_data = {**entry.data}
new_data[CONF_ENTITIES_CREATION_MODE] = "only_selected"
hass.config_entries.async_update_entry(entry, data=new_data)

# Migrate advanced_config config from < 2.5.0
if entry.data.get(CONF_ADVANCED_CONFIG) is None:
new_data = {**entry.data}
new_data[CONF_ADVANCED_CONFIG] = False
hass.config_entries.async_update_entry(entry, data=new_data)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
return True

Expand Down
101 changes: 86 additions & 15 deletions custom_components/ithodaalderop/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .const import (
ADDON_TYPES,
CONF_ADDON_TYPE,
CONF_ADVANCED_CONFIG,
CONF_AUTOTEMP_ROOM1,
CONF_AUTOTEMP_ROOM2,
CONF_AUTOTEMP_ROOM3,
Expand All @@ -23,6 +24,9 @@
CONF_AUTOTEMP_ROOM6,
CONF_AUTOTEMP_ROOM7,
CONF_AUTOTEMP_ROOM8,
CONF_CUSTOM_BASETOPIC,
CONF_CUSTOM_DEVICE_NAME,
CONF_CUSTOM_ENTITY_PREFIX,
CONF_ENTITIES_CREATION_MODE,
CONF_NONCVE_MODEL,
CONF_REMOTE_1,
Expand All @@ -34,6 +38,12 @@
ENTITIES_CREATION_MODES,
NONCVE_DEVICES,
)
from .sensors.base import (
get_default_entity_prefix,
get_default_mqtt_base_topic,
get_device_model,
get_entity_prefix,
)


class IthoConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
Expand All @@ -52,6 +62,33 @@ def _get_reconfigure_value(self, param, default):
return self.config[param]
return default

async def _set_unique_id(self):
unique_id = f"itho_wifi_addon_{get_entity_prefix(self.config)}"
await self.async_set_unique_id(unique_id)

def get_entry_title(self):
"""Generete title for the entry."""

addon_type = self.config[CONF_ADDON_TYPE]
advanced_config = self.config[CONF_ADVANCED_CONFIG]

title = "Itho WiFi Add-on for "
if addon_type == "noncve":
title = title + ADDON_TYPES[addon_type] + " - "

if advanced_config:
title = title + self.config[CONF_CUSTOM_DEVICE_NAME]
else:
title = title + NONCVE_DEVICES[self.config[CONF_NONCVE_MODEL]]

elif advanced_config:
title = title + self.config[CONF_CUSTOM_DEVICE_NAME]

else:
title = title + ADDON_TYPES[addon_type]

return title

async def async_step_user(self, user_input: Mapping[str, Any] | None = None):
"""Configure main step."""
if user_input is not None:
Expand All @@ -62,10 +99,10 @@ async def async_step_user(self, user_input: Mapping[str, Any] | None = None):
return await self.async_step_remotes()
if user_input[CONF_ADDON_TYPE] == "noncve":
return await self.async_step_noncve_model()
if user_input.get(CONF_ADVANCED_CONFIG, False):
return await self.async_step_advanced_config()

await self.async_set_unique_id(
f"itho_wifi_addon_{self.config[CONF_ADDON_TYPE]}"
)
await self._set_unique_id()
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=f"Itho WiFi Add-on for {ADDON_TYPES[self.config[CONF_ADDON_TYPE]]}",
Expand All @@ -92,6 +129,7 @@ async def async_step_user(self, user_input: Mapping[str, Any] | None = None):
}
}
),
vol.Required(CONF_ADVANCED_CONFIG): selector({"boolean": {}}),
}
)

Expand All @@ -101,12 +139,14 @@ async def async_step_rooms(self, user_input: Mapping[str, Any] | None = None):
"""Configure rooms for autotemp."""
if user_input is not None:
self.config.update(user_input)
await self.async_set_unique_id(
f"itho_wifi_addon_{self.config[CONF_ADDON_TYPE]}"
)
if self.config[CONF_ADVANCED_CONFIG]:
return await self.async_step_advanced_config()

await self._set_unique_id()
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=f"Itho WiFi Add-on for {ADDON_TYPES[self.config[CONF_ADDON_TYPE]]}",
title=self.get_entry_title(),
data=self.config,
)

Expand Down Expand Up @@ -154,17 +194,14 @@ async def async_step_remotes(self, user_input: Mapping[str, Any] | None = None):
"""Configure up to 5 remotes."""
if user_input is not None:
self.config.update(user_input)
await self.async_set_unique_id(
f"itho_wifi_addon_{self.config[CONF_ADDON_TYPE]}"
)
self._abort_if_unique_id_configured()
if self.config[CONF_ADVANCED_CONFIG]:
return await self.async_step_advanced_config()

title = "Itho WiFi Add-on for " + ADDON_TYPES[self.config[CONF_ADDON_TYPE]]
if self.config[CONF_ADDON_TYPE] == "noncve":
title = title + " - " + NONCVE_DEVICES[self.config[CONF_NONCVE_MODEL]]
await self._set_unique_id()
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=title,
title=self.get_entry_title(),
data=self.config,
)

Expand All @@ -181,6 +218,40 @@ async def async_step_remotes(self, user_input: Mapping[str, Any] | None = None):
step_id="remotes", data_schema=itho_schema, last_step=True
)

async def async_step_advanced_config(
self, user_input: Mapping[str, Any] | None = None
):
"""Advanced configuration for multiple devices that would use the same MQTT basetopic."""
if user_input is not None:
self.config.update(user_input)
await self._set_unique_id()
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=self.get_entry_title(),
data=self.config,
)

itho_schema = vol.Schema(
{
vol.Required(
CONF_CUSTOM_BASETOPIC,
default=get_default_mqtt_base_topic(self.config),
): str,
vol.Required(
CONF_CUSTOM_ENTITY_PREFIX,
default=get_default_entity_prefix(self.config),
): str,
vol.Required(
CONF_CUSTOM_DEVICE_NAME,
default=get_device_model(self.config),
): str,
}
)
return self.async_show_form(
step_id="advanced_config", data_schema=itho_schema, last_step=True
)

async def async_step_reconfigure(self, user_input: Mapping[str, Any] | None = None):
"""Reconfigure config flow."""
entry = self.hass.config_entries.async_get_entry(self.context["entry_id"])
Expand Down
6 changes: 5 additions & 1 deletion custom_components/ithodaalderop/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"rpm": "mdi:speedometer",
}

MQTT_BASETOPIC = {
MQTT_DEFAULT_BASETOPIC = {
"autotemp": "ithotemp",
"cve": "ithocve",
"noncve": "ithohru",
Expand All @@ -49,6 +49,10 @@

CONF_ADDON_TYPE = "addontype"
CONF_ENTITIES_CREATION_MODE = "entities_creation_mode"
CONF_ADVANCED_CONFIG = "advanced_config"
CONF_CUSTOM_BASETOPIC = "custom_basetopic"
CONF_CUSTOM_DEVICE_NAME = "custom_device_name"
CONF_CUSTOM_ENTITY_PREFIX = "custom_entity_prefix"
CONF_NONCVE_MODEL = "noncve_model"

CONF_AUTOTEMP_ROOM1 = "room1"
Expand Down
1 change: 1 addition & 0 deletions custom_components/ithodaalderop/definitions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class IthoSensorEntityDescription(SensorEntityDescription):
json_field: str | None = None
topic: str | None = None
icon: str | None = None
room: str | None = None
unique_id_template: str | None = None
unique_id: str | None = None
entity_category: EntityCategory | None = None
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ithodaalderop/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"mqtt": ["ithohru/#"],
"requirements": [
],
"version": "2.4.0"
"version": "2.5.0-b1"
}
22 changes: 16 additions & 6 deletions custom_components/ithodaalderop/sensors/autotemp.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
AUTOTEMP_ERROR,
AUTOTEMP_MODE,
CONF_ADDON_TYPE,
CONF_ADVANCED_CONFIG,
CONF_CUSTOM_DEVICE_NAME,
DOMAIN,
MANUFACTURER,
MQTT_BASETOPIC,
MQTT_STATETOPIC,
)
from ..definitions.autotemp import (
Expand All @@ -28,13 +29,18 @@
AUTOTEMP_VALVE_SENSOR_TEMPLATE,
)
from ..definitions.base import IthoSensorEntityDescription
from ..sensors.base import IthoBaseSensor, IthoBinarySensor
from .base import (
IthoBaseSensor,
IthoBinarySensor,
get_entity_prefix,
get_mqtt_base_topic,
)


def get_autotemp_binary_sensors(config_entry: ConfigEntry):
"""Create binary sensors for Autotemp."""
sensors = []
topic = f"{MQTT_BASETOPIC["autotemp"]}/{MQTT_STATETOPIC["autotemp"]}"
topic = f"{get_mqtt_base_topic(config_entry.data)}/{MQTT_STATETOPIC["autotemp"]}"
for description in AUTOTEMP_BINARYSENSORS:
description.topic = topic
sensors.append(IthoBinarySensor(description, config_entry))
Expand All @@ -44,7 +50,7 @@ def get_autotemp_binary_sensors(config_entry: ConfigEntry):
def get_autotemp_sensors(config_entry: ConfigEntry):
"""Create sensors for Autotemp."""
sensors = []
topic = f"{MQTT_BASETOPIC["autotemp"]}/{MQTT_STATETOPIC["autotemp"]}"
topic = f"{get_mqtt_base_topic(config_entry.data)}/{MQTT_STATETOPIC["autotemp"]}"
for i in range(1, 13):
letter = chr(i + 64)
description = copy.deepcopy(AUTOTEMP_COMM_SPACE_SENSOR_TEMPLATE)
Expand Down Expand Up @@ -162,16 +168,20 @@ def __init__(
) -> None:
"""Construct sensor for Autotemp."""

name = f"Spider {description.room.capitalize()}"
if config_entry.data[CONF_ADVANCED_CONFIG]:
name = config_entry.data[CONF_CUSTOM_DEVICE_NAME] + " - " + name

self._attr_device_info = DeviceInfo(
identifiers={
(
DOMAIN,
f"{config_entry.data[CONF_ADDON_TYPE]}_room_{description.room.lower()}",
f"itho_wifi_addon_{get_entity_prefix(config_entry.data)}_room_{description.room.lower()}",
)
},
manufacturer=MANUFACTURER,
model=f"{ADDON_TYPES[config_entry.data[CONF_ADDON_TYPE]]} Spider",
name=f"Spider {description.room.capitalize()}",
name=name,
via_device=(DOMAIN, config_entry.data[CONF_ADDON_TYPE]),
)

Expand Down
Loading

0 comments on commit cc321ff

Please sign in to comment.