Skip to content

Commit

Permalink
add serialno to unique entity id
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarv committed Mar 29, 2024
1 parent 52cbacb commit 4a466f3
Show file tree
Hide file tree
Showing 4 changed files with 372 additions and 82 deletions.
27 changes: 25 additions & 2 deletions custom_components/lg_ess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
from pyess.aio_ess import ESS, ESSException

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.const import CONF_HOST, CONF_PASSWORD, Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.entity_registry import async_migrate_entries

from .const import DOMAIN

Expand Down Expand Up @@ -39,3 +40,25 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await api.destruct()

return unload_ok


async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate old entry."""
_LOGGER.debug("Migrating from version %s", entry.version)

# Add serialno to unique id in order to allow multiple devices
if entry.version == 1:
ess = await ESS.create(None, entry.data[CONF_PASSWORD], entry.data[CONF_HOST])
serialno = (await ess.get_systeminfo())["pms"]["serialno"]

@callback
def update_unique_id(entity_entry):
"""Update unique ID of entity entry."""
return {"new_unique_id": serialno + "_" + entity_entry.unique_id}

await async_migrate_entries(hass, entry.entry_id, update_unique_id)
hass.config_entries.async_update_entry(entry, version=2)

_LOGGER.info("Migration to version %s successful", entry.version)

return True
42 changes: 38 additions & 4 deletions custom_components/lg_ess/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PASSWORD
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult

from .const import DOMAIN

Expand All @@ -20,6 +20,11 @@
vol.Required(CONF_PASSWORD): str,
}
)
STEP_DISCOVERED_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_PASSWORD): str,
}
)


async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
Expand All @@ -34,19 +39,24 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,
return {"title": "LG ESS"}


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
class EssConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for LG ESS."""

VERSION = 1
def __init__(self) -> None:
"""Initialize the ESS config flow."""
self.discovery_schema: vol.Schema | None = None

VERSION = 2

async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> FlowResult:
) -> ConfigFlowResult:
"""Handle the initial step."""
errors: dict[str, str] = {}
if user_input is not None:
try:
info = await validate_input(self.hass, user_input)
# user_input['serialno'] = info['serialno']
return self.async_create_entry(title=info["title"], data=user_input)
except ESSAuthException:
_LOGGER.exception("Wrong password")
Expand All @@ -61,3 +71,27 @@ async def async_step_user(
return self.async_show_form(
step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors
)


# async def async_step_zeroconf(
# self, discovery_info: zeroconf.ZeroconfServiceInfo
# ) -> ConfigFlowResult:
# """Handle the zeroconf discovery."""
# host = discovery_info.host
# properties = discovery_info.properties
# _LOGGER.info("Discovered device %s with %s", host, discovery_info)
# host = discovery_info.host
# id = {CONF_HOST: host}
# await self.async_set_unique_id(host)
# self._abort_if_unique_id_configured(updates={CONF_HOST: host})
#
# self._async_abort_entries_match({CONF_HOST: host})
#
# self.discovery_schema = vol.Schema(
# {
# vol.Required(CONF_HOST, default=host): str,
# }
# )
#
# return await self.async_step_user()
#
7 changes: 5 additions & 2 deletions custom_components/lg_ess/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
"config_flow": true,
"dependencies": [],
"documentation": "https://github.com/dkarv/hacs-lg-ess/blob/main/README.md",
"version": "0.1.0",
"version": "0.2.0",
"homekit": {},
"iot_class": "local_polling",
"issue_tracker": "https://github.com/dkarv/hacs-lg-ess/issues",
"documentation": "https://www.home-assistant.io/integrations/lg_ess",
"homekit": {},
"iot_class": "local_polling",
"requirements": ["pyess==0.1.15"],
"ssdp": [],
"zeroconf": []
"zeroconf": ["_pmsctrl._tcp.local."]
}
Loading

0 comments on commit 4a466f3

Please sign in to comment.