Skip to content

Commit

Permalink
Merge pull request #53 from bj00rn/feat/support-multiple-instances
Browse files Browse the repository at this point in the history
feat: support multiple instances
  • Loading branch information
bj00rn authored Nov 10, 2024
2 parents 7061f81 + 4611ea5 commit 3dffc9b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
20 changes: 13 additions & 7 deletions custom_components/saleryd_hrv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import async_timeout
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_create_clientsession
Expand All @@ -19,6 +20,7 @@
CONF_WEBSOCKET_IP,
CONF_WEBSOCKET_PORT,
CONFIG_VERSION,
DEFAULT_NAME,
DOMAIN,
LOGGER,
PLATFORMS,
Expand Down Expand Up @@ -59,8 +61,13 @@ async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
LOGGER.info("Upgrading entry to version 2")
hass.config_entries.async_update_entry(entry, data=new_data, version=2)

if entry.version is None or entry.version < 3:
unique_id = entry.data.get(CONF_NAME, DEFAULT_NAME)
LOGGER.info("Upgrading entry to version 3, setting unique_id to %s", unique_id)
hass.config_entries.async_update_entry(
entry, data=new_data, version=CONFIG_VERSION
entry,
version=3,
unique_id=unique_id,
)

return True
Expand All @@ -74,9 +81,8 @@ async def async_setup(hass: HomeAssistant, processed_config):


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""
if hass.data.get(DOMAIN) is None:
hass.data.setdefault(DOMAIN, {})
"""Set up the integration from ConfigEntry."""
hass.data.setdefault(DOMAIN, {})

url = entry.data.get(CONF_WEBSOCKET_IP)
port = entry.data.get(CONF_WEBSOCKET_PORT)
Expand All @@ -92,7 +98,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
else:
coordinator = SalerydLokeDataUpdateCoordinator(hass, client, LOGGER)
await coordinator.async_config_entry_first_refresh()
hass.data[DOMAIN][entry.entry_id] = coordinator
hass.data[DOMAIN][entry.unique_id] = coordinator

# Setup platforms
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
Expand Down Expand Up @@ -183,10 +189,10 @@ async def set_target_temperature_economy(call):


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
"""Handle unload of an entry."""

# disconnect client
coordinator: SalerydLokeDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
coordinator: SalerydLokeDataUpdateCoordinator = hass.data[DOMAIN][entry.unique_id]
coordinator.client.disconnect()

# remove services
Expand Down
8 changes: 6 additions & 2 deletions custom_components/saleryd_hrv/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
RECONFIG_SCHEMA = vol.Schema({**RECONFIG_DATA})


class SalerydLokeFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
@config_entries.HANDLERS.register(DOMAIN)
class SalerydLokeFlowHandler(config_entries.ConfigFlow):
"""Config flow for SalerydLoke."""

VERSION = CONFIG_VERSION
Expand All @@ -53,7 +54,7 @@ async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
self._errors = {}

# Comment the next 2 lines if multiple instances of the integration is allowed:
# Comment the next 2 lines if multiple instances of the integration is allowed
if self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

Expand All @@ -66,6 +67,9 @@ async def async_step_user(self, user_input=None):
except TimeoutError:
self._errors["base"] = "connect"
else:
await self.async_set_unique_id(user_input[CONF_NAME])
self._abort_if_unique_id_configured()

return self.async_create_entry(
title=user_input[CONF_NAME], data=user_input
)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/saleryd_hrv/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ISSUE_URL = "https://github.com/bj00rn/ha-saleryd-ftx/issues"
SUPPORTED_FIRMWARES = ["4.1.5"]
UNSUPPORTED_FIRMWARES = ["4.1.1"]
CONFIG_VERSION = 2
CONFIG_VERSION = 3

# Icons
ICON = "mdi:format-quote-close"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/saleryd_hrv/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ def extra_state_attributes(self) -> dict[str, Any] | None:

async def async_setup_entry(hass, entry, async_add_entities: AddEntitiesCallback):
"""Setup sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = hass.data[DOMAIN][entry.unique_id]

entities = [
sensor.get("klass")(coordinator, entry.entry_id, sensor.get("description"))
Expand Down
5 changes: 3 additions & 2 deletions custom_components/saleryd_hrv/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
"connect": "Could not connect. Verify connection details"
},
"abort": {
"single_instance_allowed": "Only a single instance is allowed.",
"reconfigure_successful": "Reconfiguration successful"
"single_instance_allowed": "Only a single instance is allowed",
"reconfigure_successful": "Reconfiguration successful",
"already_configured": "An instance with the same name is already configured"
}
}
}
2 changes: 1 addition & 1 deletion custom_components/saleryd_hrv/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class SalerydLokeBoostModeBinarySwitch(SalerydLokeVentilationModeBinarySwitch):

async def async_setup_entry(hass, entry, async_add_entities):
"""Setup sensor platform."""
coordinator = hass.data[DOMAIN][entry.entry_id]
coordinator = hass.data[DOMAIN][entry.unique_id]

entities = [
switch.get("klass")(coordinator, entry.entry_id, switch.get("description"))
Expand Down
5 changes: 3 additions & 2 deletions custom_components/saleryd_hrv/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@
"connect": "Could not connect. Verify connection details"
},
"abort": {
"single_instance_allowed": "Only a single instance is allowed.",
"reconfigure_successful": "Reconfiguration successful"
"single_instance_allowed": "Only a single instance is allowed",
"reconfigure_successful": "Reconfiguration successful",
"already_configured": "An instance with the same name is already configured"
}
}
}

0 comments on commit 3dffc9b

Please sign in to comment.