Skip to content

Commit

Permalink
refactor: Config/Options flow
Browse files Browse the repository at this point in the history
- Removed Disable Templating item
- Removed Name item from Options flow
  • Loading branch information
davidrapan committed Jul 14, 2024
1 parent 2dc54b3 commit bdb8705
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 31 deletions.
16 changes: 7 additions & 9 deletions custom_components/solarman/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ async def async_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None

async def step_user_data_process(discovery):
_LOGGER.debug(f"step_user_data_process: discovery: {discovery}")
return { CONF_NAME: DEFAULT_NAME, CONF_INVERTER_DISCOVERY: DEFAULT_DISCOVERY, CONF_INVERTER_HOST: await discovery.get_ip(), CONF_INVERTER_SERIAL: await discovery.get_serial(), CONF_INVERTER_PORT: DEFAULT_PORT_INVERTER, CONF_INVERTER_MB_SLAVE_ID: DEFAULT_INVERTER_MB_SLAVE_ID, CONF_LOOKUP_FILE: DEFAULT_LOOKUP_FILE, CONF_BATTERY_NOMINAL_VOLTAGE: DEFAULT_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING: DEFAULT_BATTERY_LIFE_CYCLE_RATING, CONF_DISABLE_TEMPLATING: DEFAULT_DISABLE_TEMPLATING }
return { CONF_NAME: DEFAULT_NAME, CONF_INVERTER_DISCOVERY: DEFAULT_DISCOVERY, CONF_INVERTER_HOST: await discovery.get_ip(), CONF_INVERTER_SERIAL: await discovery.get_serial(), CONF_INVERTER_PORT: DEFAULT_PORT_INVERTER, CONF_INVERTER_MB_SLAVE_ID: DEFAULT_INVERTER_MB_SLAVE_ID, CONF_LOOKUP_FILE: DEFAULT_LOOKUP_FILE, CONF_BATTERY_NOMINAL_VOLTAGE: DEFAULT_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING: DEFAULT_BATTERY_LIFE_CYCLE_RATING }

async def step_user_data_schema(hass: HomeAssistant, data: dict[str, Any] = { CONF_NAME: DEFAULT_NAME, CONF_INVERTER_DISCOVERY: DEFAULT_DISCOVERY, CONF_INVERTER_PORT: DEFAULT_PORT_INVERTER, CONF_INVERTER_MB_SLAVE_ID: DEFAULT_INVERTER_MB_SLAVE_ID, CONF_LOOKUP_FILE: DEFAULT_LOOKUP_FILE, CONF_BATTERY_NOMINAL_VOLTAGE: DEFAULT_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING: DEFAULT_BATTERY_LIFE_CYCLE_RATING, CONF_DISABLE_TEMPLATING: DEFAULT_DISABLE_TEMPLATING }) -> Schema:
async def step_user_data_schema(hass: HomeAssistant, data: dict[str, Any] = { CONF_NAME: DEFAULT_NAME, CONF_INVERTER_DISCOVERY: DEFAULT_DISCOVERY, CONF_INVERTER_PORT: DEFAULT_PORT_INVERTER, CONF_INVERTER_MB_SLAVE_ID: DEFAULT_INVERTER_MB_SLAVE_ID, CONF_LOOKUP_FILE: DEFAULT_LOOKUP_FILE, CONF_BATTERY_NOMINAL_VOLTAGE: DEFAULT_BATTERY_NOMINAL_VOLTAGE, CONF_BATTERY_LIFE_CYCLE_RATING: DEFAULT_BATTERY_LIFE_CYCLE_RATING }, wname: bool = True) -> vol.Schema:
lookup_files = sorted([f for f in await async_execute(lambda: os.listdir(hass.config.path(LOOKUP_DIRECTORY_PATH))) if os.path.isfile(LOOKUP_DIRECTORY_PATH + f)])
_LOGGER.debug(f"step_user_data_schema: data: {data}, {LOOKUP_DIRECTORY_PATH}: {lookup_files}")
STEP_USER_DATA_SCHEMA = vol.Schema(
STEP_USER_DATA_SCHEMA = vol.Schema({ vol.Required(CONF_NAME, default = data.get(CONF_NAME)): str }, extra = vol.PREVENT_EXTRA) if wname else vol.Schema({}, extra = vol.PREVENT_EXTRA)
STEP_USER_DATA_SCHEMA = STEP_USER_DATA_SCHEMA.extend(
{
vol.Required(CONF_NAME, default = data.get(CONF_NAME)): str,
vol.Required(CONF_INVERTER_DISCOVERY, default = data.get(CONF_INVERTER_DISCOVERY)): bool,
vol.Required(CONF_INVERTER_HOST, default = data.get(CONF_INVERTER_HOST)): str,
vol.Required(CONF_INVERTER_SERIAL, default = data.get(CONF_INVERTER_SERIAL)): int,
Expand All @@ -46,9 +46,7 @@ async def step_user_data_schema(hass: HomeAssistant, data: dict[str, Any] = { CO
vol.Optional(CONF_LOOKUP_FILE, default = data.get(CONF_LOOKUP_FILE)): vol.In(lookup_files),
vol.Optional(CONF_BATTERY_NOMINAL_VOLTAGE, default = data.get(CONF_BATTERY_NOMINAL_VOLTAGE)): int,
vol.Optional(CONF_BATTERY_LIFE_CYCLE_RATING, default = data.get(CONF_BATTERY_LIFE_CYCLE_RATING)): int,
vol.Optional(CONF_DISABLE_TEMPLATING, default = data.get(CONF_DISABLE_TEMPLATING)): bool,
},
extra = vol.PREVENT_EXTRA
}
)
_LOGGER.debug(f"step_user_data_schema: STEP_USER_DATA_SCHEMA: {STEP_USER_DATA_SCHEMA}")
return STEP_USER_DATA_SCHEMA
Expand Down Expand Up @@ -134,7 +132,7 @@ async def async_step_init(self, user_input: dict[str, Any] | None = None) -> Con
"""Handle options flow."""
_LOGGER.debug(f"OptionsFlowHandler.async_step_init: {user_input}")
if user_input is None:
return self.async_show_form(step_id = "init", data_schema = await step_user_data_schema(self.hass, self.entry.options))
return self.async_show_form(step_id = "init", data_schema = await step_user_data_schema(self.hass, self.entry.options, False))

errors = {}

Expand All @@ -150,7 +148,7 @@ async def async_step_init(self, user_input: dict[str, Any] | None = None) -> Con
else:
return self.async_create_entry(title = info["title"], data = user_input)

return self.async_show_form(step_id = "init", data_schema = await step_user_data_schema(self.hass, user_input), errors = errors)
return self.async_show_form(step_id = "init", data_schema = await step_user_data_schema(self.hass, user_input, False), errors = errors)

class InvalidHost(HomeAssistantError):
"""Error to indicate there is invalid hostname or IP address."""
Expand Down
2 changes: 0 additions & 2 deletions custom_components/solarman/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
CONF_LOOKUP_FILE = "lookup_file"
CONF_BATTERY_NOMINAL_VOLTAGE = "battery_nominal_voltage"
CONF_BATTERY_LIFE_CYCLE_RATING = "battery_life_cycle_rating"
CONF_DISABLE_TEMPLATING = "disable_templating"

DEFAULT_NAME = "Inverter"
DEFAULT_DISCOVERY = True
Expand All @@ -36,7 +35,6 @@
DEFAULT_LOOKUP_FILE = "deye_hybrid.yaml"
DEFAULT_BATTERY_NOMINAL_VOLTAGE = 48
DEFAULT_BATTERY_LIFE_CYCLE_RATING = 6000
DEFAULT_DISABLE_TEMPLATING = False

ACTION_RETRY_ATTEMPTS = 5

Expand Down
6 changes: 2 additions & 4 deletions custom_components/solarman/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"mb_slave_id": "Modbus-Slave-ID (normalerweise 1)",
"lookup_file": "YAML-Datei mit der Parameter-Definition",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand All @@ -47,8 +46,7 @@
"mb_slave_id": "Modbus-Slave-ID (normalerweise 1)",
"lookup_file": "YAML-Datei mit der Parameter-Definition",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand Down
6 changes: 2 additions & 4 deletions custom_components/solarman/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"mb_slave_id": "Modbus Slave ID (usually 1)",
"lookup_file": "The yaml file containing inverter parameter definitions",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand All @@ -48,8 +47,7 @@
"mb_slave_id": "Modbus Slave ID (usually 1)",
"lookup_file": "The yaml file containing inverter parameter definitions",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand Down
6 changes: 2 additions & 4 deletions custom_components/solarman/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"mb_slave_id": "Slave ID di Modbus (solitamente 1)",
"lookup_file": "File YAML contenente la definizione Inverter",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand All @@ -48,8 +47,7 @@
"mb_slave_id": "Slave ID di Modbus (solitamente 1)",
"lookup_file": "File YAML contenente la definizione Inverter",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand Down
6 changes: 2 additions & 4 deletions custom_components/solarman/translations/pl.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"mb_slave_id": "Modbus Slave ID (zwykle 1)",
"lookup_file": "Plik yaml u\u017cywany do definiowania parametr\u00f3w",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand All @@ -48,8 +47,7 @@
"mb_slave_id": "Modbus Slave ID (zwykle 1)",
"lookup_file": "Plik yaml u\u017cywany do definiowania parametr\u00f3w",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand Down
6 changes: 2 additions & 4 deletions custom_components/solarman/translations/pt-BR.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
"mb_slave_id": "Modbus Slave ID (geralmente 1)",
"lookup_file": "O arquivo yaml a ser usado para definição de parâmetro",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand All @@ -48,8 +47,7 @@
"mb_slave_id": "Modbus Slave ID (geralmente 1)",
"lookup_file": "O arquivo yaml a ser usado para definição de parâmetro",
"battery_nominal_voltage": "Lithium-ion battery nominal voltage",
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating",
"disable_templating": "Disable template sensors"
"battery_life_cycle_rating": "Lithium-ion battery expected life cycle rating"
},
"data_description": {
"inverter_host": "The hostname or IP address of the device to connect to.",
Expand Down

0 comments on commit bdb8705

Please sign in to comment.