Files

135 lines
4.8 KiB
Python

"""Config flow for Sofar devices."""
import logging
from typing import Any, override
from modbus_connection import ModbusError, ModbusTcpParams
from sofar_modbus.modern.device import SofarInverter
import voluptuous as vol
from homeassistant.components.modbus import async_get_temporary_unit
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.selector import (
NumberSelector,
NumberSelectorConfig,
NumberSelectorMode,
TextSelector,
)
from .const import CONF_UNIT_ID, DEFAULT_NAME, DEFAULT_PORT, DEFAULT_UNIT_ID, DOMAIN
_LOGGER = logging.getLogger(__name__)
STEP_USER_DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_HOST): TextSelector(),
vol.Required(CONF_PORT, default=DEFAULT_PORT): vol.All(
NumberSelector(
NumberSelectorConfig(mode=NumberSelectorMode.BOX, min=1, max=65535)
),
vol.Coerce(int),
),
vol.Required(CONF_UNIT_ID, default=DEFAULT_UNIT_ID): vol.All(
NumberSelector(
NumberSelectorConfig(mode=NumberSelectorMode.BOX, min=1, max=247)
),
vol.Coerce(int),
),
}
)
async def _async_probe(
hass: HomeAssistant, host: str, port: int, unit_id: int
) -> SofarInverter:
"""Connect to the inverter and read its identity, or raise."""
params = ModbusTcpParams(host=host, port=port)
async with async_get_temporary_unit(hass, params, unit_id) as unit:
device = SofarInverter(unit)
await device.async_update()
return device
class SofarConfigFlow(ConfigFlow, domain=DOMAIN):
"""Handle a Sofar config flow."""
VERSION = 1
@override
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the initial connection step."""
errors: dict[str, str] = {}
description_placeholders: dict[str, str] = {}
if user_input is not None:
try:
device = await _async_probe(
self.hass,
user_input[CONF_HOST],
user_input[CONF_PORT],
user_input[CONF_UNIT_ID],
)
except (ModbusError, HomeAssistantError) as err:
errors["base"] = "cannot_connect"
description_placeholders["error"] = str(err)
else:
assert device.serial_number is not None
if not device.inverter_type:
errors["base"] = "unrecognized_inverter"
else:
await self.async_set_unique_id(device.serial_number)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=device.model or DEFAULT_NAME,
data=user_input,
)
return self.async_show_form(
step_id="user",
data_schema=STEP_USER_DATA_SCHEMA,
errors=errors,
description_placeholders=description_placeholders,
)
async def async_step_reconfigure(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle updating an existing entry's connection details."""
reconfigure_entry = self._get_reconfigure_entry()
errors: dict[str, str] = {}
description_placeholders: dict[str, str] = {}
if user_input is not None:
try:
device = await _async_probe(
self.hass,
user_input[CONF_HOST],
user_input[CONF_PORT],
user_input[CONF_UNIT_ID],
)
except (ModbusError, HomeAssistantError) as err:
errors["base"] = "cannot_connect"
description_placeholders["error"] = str(err)
else:
assert device.serial_number is not None
if not device.inverter_type:
errors["base"] = "unrecognized_inverter"
else:
await self.async_set_unique_id(device.serial_number)
self._abort_if_unique_id_mismatch()
return self.async_update_reload_and_abort(
reconfigure_entry, data_updates=user_input
)
return self.async_show_form(
step_id="reconfigure",
data_schema=self.add_suggested_values_to_schema(
STEP_USER_DATA_SCHEMA, user_input or reconfigure_entry.data
),
errors=errors,
description_placeholders=description_placeholders,
)