Skip to content

Commit

Permalink
Merge pull request #13 from hostcc/feature/include_default_parameters
Browse files Browse the repository at this point in the history
Allow including default parameters
  • Loading branch information
hostcc authored Mar 11, 2023
2 parents 74d04ed + 3d263e9 commit a8c6501
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ Configuration file is in YAML format and supports following elements:
# (string) default ``error``: logging level, one of ``critical``,
# ``error``, ``warning``, ``info`` or ``debug``
logging_level:
# (bool) default ``false``: if enabled makes parameters a
# combination of default ones plus those defined in the
# configuration file, no check for duplicates is performed!
include_default_parameters:
# Energy meter parameters
meter:
# (string) Serial port (e.g. /dev/ttyUSB0)
Expand Down
14 changes: 13 additions & 1 deletion src/energomera_hass_mqtt/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ def _get_schema(self):
error='Invalid logging level - should be one of'
f' {", ".join(self._logging_levels.keys())}'
),
Optional(
'include_default_parameters',
default=DEFAULT_CONFIG.general.include_default_parameters
): bool,
}),
'meter': {
'port': str,
Expand Down Expand Up @@ -213,9 +217,17 @@ def __init__(self, config_file=None, content=None):
raise EnergomeraConfigError(
f'Error validating configuration file:\n{str(exc)}'
) from None

# Store the configuration state as `addict.Dict` so access by (possibly
# chained) attributes is avavilable
# chained) attributes is available
self._config = Dict(config)
# If enabled makes parameters a combination of default ones plus those
# defined in the configuration file, no check for duplicates is
# performed!
if self._config.general.include_default_parameters:
self._config.parameters = (
DEFAULT_CONFIG.parameters + self._config.parameters
)
# Make a copy of the original configuration for the `interpolate()`
# method to access initially defined interpolation expressions if any
self._orig_config = deepcopy(self._config)
Expand Down
1 change: 1 addition & 0 deletions src/energomera_hass_mqtt/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
oneshot=False,
intercycle_delay=30,
logging_level='error',
include_default_parameters=False,
),
meter=dict(
timeout=30,
Expand Down
34 changes: 34 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def test_valid_config_file():
'oneshot': False,
'intercycle_delay': 30,
'logging_level': 'error',
'include_default_parameters': False,
},
'meter': {
'port': 'dummy_serial',
Expand Down Expand Up @@ -88,6 +89,39 @@ def test_valid_config_file():
assert config.logging_level == logging.ERROR


def test_valid_config_file_with_default_parameters():
'''
Tests for processing of valid configuration file that allows including
default parameters plus adds some custom ones.
'''
valid_config_yaml = '''
general:
include_default_parameters: true
meter:
port: dummy_serial
password: dummy_password
mqtt:
host: a_mqtt_host
user: a_mqtt_user
password: mqtt_dummy_password
parameters:
- name: dummy_param
address: dummy_addr
device_class: dummy_class
state_class: dummy_state
unit: dummy
'''

with patch('builtins.open', mock_open(read_data=valid_config_yaml)):
config = EnergomeraConfig(config_file='dummy')
assert isinstance(config.of, dict)
# Resulting number of parameters should be combined across default and
# custom ones
assert len(config.of.parameters) == 12
# Verify the last parameter is the custom one
assert config.of.parameters[-1].name == 'dummy_param'


def test_empty_file():
'''
Tests for processing empty configuration file.
Expand Down

0 comments on commit a8c6501

Please sign in to comment.