diff --git a/custom_components/solvis_control/__init__.py b/custom_components/solvis_control/__init__.py index 407e5b3..edec174 100644 --- a/custom_components/solvis_control/__init__.py +++ b/custom_components/solvis_control/__init__.py @@ -3,3 +3,38 @@ Version: 0.1.1-alpha """ +"""Solvis integration.""" + +from homeassistant.config_entries import ConfigEntry +from homeassistant.const import CONF_IP_ADDRESS, Platform +from homeassistant.core import HomeAssistant + +from .const import ( + DATA_COORDINATOR, + DOMAIN, +) +from .coordinator import SolvisModbusCoordinator + +PLATFORMS: [Platform] = [Platform.SENSOR] + +async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + """Set up Solvis device from a config entry.""" + + address = entry.data.get(CONF_IP_ADDRESS) + + if address is None: + return False + + # Create data structure + hass.data.setdefault(DOMAIN, {}) + hass.data[DOMAIN].setdefault(entry.entry_id, {}) + + # Create coordinator for polling + coordinator = SolvisModbusCoordinator(hass, address) + await coordinator.async_config_entry_first_refresh() + hass.data[DOMAIN][entry.entry_id].setdefault(DATA_COORDINATOR, coordinator) + + # Setup platforms + await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) + + return True diff --git a/custom_components/solvis_control/config_flow.py b/custom_components/solvis_control/config_flow.py index e029e51..401ee0f 100644 --- a/custom_components/solvis_control/config_flow.py +++ b/custom_components/solvis_control/config_flow.py @@ -41,6 +41,7 @@ class SolvisConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): def __init__(self) -> None: """Init the ConfigFlow.""" + _LOGGER.info("Initialize config flow for %s", DOMAIN) self.data: ConfigType = {} self.client = None @@ -62,71 +63,81 @@ async def async_step_user( step_id="user", data_schema=get_host_schema_config(self.data) ) self.data = user_input - errors = {} - try: - self.client = ModbusClient.AsyncModbusTcpClient( - user_input[CONF_HOST], user_input[CONF_PORT] + await self.async_set_unique_id(self.data[CONF_HOST], raise_on_progress=False) + self._abort_if_unique_id_configured() + + return self.async_create_entry( + title=self.data[CONF_NAME], data=self.data) + @staticmethod + @callback + def async_get_options_flow(config_entry: config_entries.ConfigEntry,) -> config_entries.OptionsFlow: + """Create the options flow.""" + return SolvisOptionsFlow(config_entry) + + # TODO: add check for valid data + # errors = {} + # try: + # self.client = ModbusClient.AsyncModbusTcpClient( + # user_input[CONF_HOST], user_input[CONF_PORT] + # ) + # await self.client.connect() + # except ConnectionException: + # errors["base"] = "Es konnte keine Verbinung aufgebaut werden" + # else: + # try: + # await self.client.read_coils(32770, 3, slave=1) + # except ModbusException as exc: + # _LOGGER.debug(f"Received ModbusException({exc}) from library") + # else: + # await self.client.close() + + # errors["base"] = "cannot_connect" + + # return self.async_show_form( + # step_id="user", data_schema=get_host_schema_config(self.data), errors=errors + # ) + + +class SolvisOptionsFlow(config_entries.OptionsFlow, domain=DOMAIN): + # The schema version of the entries that it creates + # Home Assistant will call your migrate method if the version changes + VERSION = 1 + MINOR_VERSION = 1 + + def __init__(self, config) -> None: + """Init the ConfigFlow.""" + self.data: ConfigType = config + self.client = None + + async def async_step_init( + self, user_input: dict[str, int] | None = None + ) -> FlowResult: + """Handle the initial step.""" + if user_input is None: + return self.async_show_form( + step_id="user", data_schema=get_host_schema_options(self.data) ) - await self.client.connect() - except ConnectionException: - errors["base"] = "Es konnte keine Verbinung aufgebaut werden" - else: - try: - await self.client.read_coils(32770, 3, slave=1) - except ModbusException as exc: - _LOGGER.debug(f"Received ModbusException({exc}) from library") - else: - await self.client.close() - return self.async_create_entry( + self.data = user_input + return self.async_create_entry( title=self.data[CONF_NAME], data=self.data ) - errors["base"] = "cannot_connect" - - return self.async_show_form( - step_id="user", data_schema=get_host_schema_config(self.data), errors=errors - ) - - -# class SolvisOptionsFlow(config_entries.OptionsFlow, domain=DOMAIN): -# # The schema version of the entries that it creates -# # Home Assistant will call your migrate method if the version changes -# VERSION = 1 -# MINOR_VERSION = 1 - -# def __init__(self, config) -> None: -# """Init the ConfigFlow.""" -# self.data: ConfigType = config -# self.client = None - -# async def async_step_init( -# self, user_input: dict[str, int] | None = None -# ) -> FlowResult: -# """Handle the initial step.""" -# if user_input is None: -# return self.async_show_form( -# step_id="user", data_schema=get_host_schema_options(self.data) -# ) -# self.data = user_input -# errors = {} -# try: -# self.client = ModbusClient.AsyncModbusTcpClient( -# user_input[CONF_HOST], user_input[CONF_PORT] -# ) -# await self.client.connect() -# except ConnectionException: -# errors["base"] = "Es konnte keine Verbinung aufgebaut werden" -# else: -# try: -# rr = await self.client.read_coils(32770, 3, slave=1) -# except ModbusException as exc: -# print(f"Received ModbusException({exc}) from library") -# finally: -# await self.client.close() -# return self.async_create_entry( -# title=self.data[CONF_NAME], data=self.data -# ) -# errors["base"] = "cannot_connect" - -# return self.async_show_form( -# step_id="init", data_schema=self.data_schema, errors=errors -# ) + # errors = {} + # try: + # self.client = ModbusClient.AsyncModbusTcpClient( + # user_input[CONF_HOST], user_input[CONF_PORT] + # ) + # await self.client.connect() + # except ConnectionException: + # errors["base"] = "Es konnte keine Verbinung aufgebaut werden" + # else: + # try: + # rr = await self.client.read_coils(32770, 3, slave=1) + # except ModbusException as exc: + # print(f"Received ModbusException({exc}) from library") + # finally: + # await self.client.close() + # errors["base"] = "cannot_connect" + + # return self.async_show_form( + # step_id="init", data_schema=self.data_schema, errors=errors + # ) diff --git a/custom_components/solvis_control/const.py b/custom_components/solvis_control/const.py index a79d91d..2dc6ffc 100644 --- a/custom_components/solvis_control/const.py +++ b/custom_components/solvis_control/const.py @@ -3,3 +3,5 @@ CONF_NAME = "name" CONF_HOST = "host" CONF_PORT = "port" + +DATA_COORDINATOR = "coordinator"