Skip to content

Commit

Permalink
Add support for quoted environment variables
Browse files Browse the repository at this point in the history
Environment variables with spaces in them must be quoted, otherwise
`make run` will report `command not found`:

```
$ grep DEYE_HA_PLUGIN_INVERTER_MODEL config.env
DEYE_HA_PLUGIN_INVERTER_MODEL=SUN300G3 EU 230

$ make run
config.env: line 49: EU: command not found
2024-06-10 21:13:44,180 - DeyeDaemon - INFO - Please help me build the list of compatible inverters. kbialek#41
2024-06-10 21:13:44,190 - DeyePluginLoader - INFO - Loading plugin: 'deye_plugin_ha_discovery'
2024-06-10 21:13:44,191 - DeyeConnectorFactory - INFO - Creating Modbus/TCP Logger connector
[...]
```

With the quotation marks, the environment variables in Docker are
slightly different:

**Setup:**

```
$ grep DEYE_HA_PLUGIN_INVERTER_MODEL config.env
DEYE_HA_PLUGIN_INVERTER_MODEL="SUN300G3 EU 230"
```

**Shell and local Python**

```
$ strings /proc/<pid>/environ | grep DEYE_HA_PLUGIN_INVERTER_MODEL
DEYE_HA_PLUGIN_INVERTER_MODEL=SUN300G3 EU 230
```

**Docker**

```
$ strings /proc/<pid>/environ | grep DEYE_HA_PLUGIN_INVERTER_MODEL
DEYE_HA_PLUGIN_INVERTER_MODEL="SUN300G3 EU 230"
```

This change addresses the different behavior of Docker compared to a
Unix shell and removes double or single quotes only if they are the
first and last character of the string.

The functionality was added to `DeyeEnv.integer()` and
`DeyeEnv.boolean()` for the sake of convenience to avoid problems with
quotes that may occur later.
  • Loading branch information
CarstenGrohmann committed Jun 10, 2024
1 parent bc41f2a commit b5397e9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/deye_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,19 @@ def process(self, msg, kwargs):


class DeyeEnv:
@staticmethod
def unquote(value: str) -> str:
"""Remove single and double quotes at the beginning and the end"""
if value and value[0] == '"' and value[-1] == '"':
value = value[1:-1]
elif value and value[0] == "'" and value[-1] == "'":
value = value[1:-1]
return value

@staticmethod
def integer(env_var_name: str, default_value: int = None) -> int:
value = os.getenv(env_var_name)
value = DeyeEnv.unquote(value)
if value:
try:
return int(value)
Expand All @@ -51,6 +61,7 @@ def integer(env_var_name: str, default_value: int = None) -> int:
@staticmethod
def boolean(env_var_name: str, default_value: bool = None) -> bool:
value = os.getenv(env_var_name)
value = DeyeEnv.unquote(value)
if value and value == "true":
return True
elif value and value == "false":
Expand All @@ -67,6 +78,7 @@ def boolean(env_var_name: str, default_value: bool = None) -> bool:
@staticmethod
def string(env_var_name: str, default_value: str = None) -> str | None:
value = os.getenv(env_var_name)
value = DeyeEnv.unquote(value)
if value:
return value
elif default_value is not None:
Expand Down

0 comments on commit b5397e9

Please sign in to comment.