From 157009a6a97741f45f83d7cc780a9882c93fc533 Mon Sep 17 00:00:00 2001 From: David Rapan Date: Fri, 26 Jul 2024 03:37:01 +0200 Subject: [PATCH] refactor: Conscise loading of inverter profiles --- .github/workflows/bump.yaml | 2 +- custom_components/solarman/common.py | 4 ++++ custom_components/solarman/config_flow.py | 4 +--- .../solarman/inverter_definitions/deye_sg04lp3.yaml | 1 - 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/bump.yaml b/.github/workflows/bump.yaml index 1cb9083..7885c38 100644 --- a/.github/workflows/bump.yaml +++ b/.github/workflows/bump.yaml @@ -20,4 +20,4 @@ jobs: git config --global user.name "Continuous Integration" git config --global user.email "david@rapan.cz" git commit -m "chore: bump version" custom_components/solarman/manifest.json - git push \ No newline at end of file + git push diff --git a/custom_components/solarman/common.py b/custom_components/solarman/common.py index 3d7c233..f614721 100644 --- a/custom_components/solarman/common.py +++ b/custom_components/solarman/common.py @@ -1,3 +1,4 @@ +import os import yaml import asyncio import aiofiles @@ -14,6 +15,9 @@ async def async_execute(x): loop = asyncio.get_running_loop() return await loop.run_in_executor(None, x) +async def async_listdir(path, prefix = ""): + return sorted([prefix + f for f in await async_execute(lambda: os.listdir(path)) if os.path.isfile(path + f)]) if os.path.exists(path) else [] + def execute_async(x): loop = asyncio.get_event_loop() return loop.run_until_complete(x) diff --git a/custom_components/solarman/config_flow.py b/custom_components/solarman/config_flow.py index e5f520c..5884dfd 100644 --- a/custom_components/solarman/config_flow.py +++ b/custom_components/solarman/config_flow.py @@ -37,9 +37,7 @@ async def step_user_data_process(discovery): return { CONF_NAME: DEFAULT_NAME, CONF_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_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)]) - if os.path.exists(hass.config.path(LOOKUP_CUSTOM_DIRECTORY_PATH)): - lookup_files = lookup_files + sorted([f"custom/{f}" for f in await async_execute(lambda: os.listdir(hass.config.path(LOOKUP_CUSTOM_DIRECTORY_PATH))) if os.path.isfile(LOOKUP_CUSTOM_DIRECTORY_PATH + f)]) + lookup_files = await async_listdir(hass.config.path(LOOKUP_DIRECTORY_PATH)) + await async_listdir(hass.config.path(LOOKUP_CUSTOM_DIRECTORY_PATH), "custom/") _LOGGER.debug(f"step_user_data_schema: data: {data}, {LOOKUP_DIRECTORY_PATH}: {lookup_files}") #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( diff --git a/custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml b/custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml index 8f7c227..62838aa 100644 --- a/custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml +++ b/custom_components/solarman/inverter_definitions/deye_sg04lp3.yaml @@ -1154,7 +1154,6 @@ parameters: - name: "DC Temperature" update_interval: 30 - disabled: class: "temperature" state_class: "measurement" uom: "°C"