Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Nov 4, 2024
2 parents 37966f0 + 6bd2fc2 commit 2c440f1
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 57 deletions.
28 changes: 14 additions & 14 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ GEM
concurrent-ruby
diff-lcs (1.5.1)
docile (1.4.1)
dotenv (3.1.2)
dotenv (3.1.4)
ffi (1.17.0)
formatador (1.1.0)
guard (2.18.1)
guard (2.19.0)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (>= 1.0.12, < 2.0)
Expand All @@ -37,7 +37,7 @@ GEM
rspec (>= 2.99.0, < 4.0)
hashdiff (1.1.1)
influxdb-client (3.1.0)
json (2.7.2)
json (2.7.6)
jsonpath (1.1.5)
multi_json
language_server-protocol (3.17.0.3)
Expand All @@ -55,7 +55,7 @@ GEM
shellany (~> 0.0)
ostruct (0.6.0)
parallel (1.26.3)
parser (3.3.5.0)
parser (3.3.5.1)
ast (~> 2.4.1)
racc
pry (0.14.2)
Expand All @@ -69,21 +69,21 @@ GEM
rb-inotify (0.11.1)
ffi (~> 1.0)
regexp_parser (2.9.2)
rexml (3.3.7)
rexml (3.3.9)
rspec (3.13.0)
rspec-core (~> 3.13.0)
rspec-expectations (~> 3.13.0)
rspec-mocks (~> 3.13.0)
rspec-core (3.13.1)
rspec-core (3.13.2)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-mocks (3.13.1)
rspec-mocks (3.13.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.13.0)
rspec-support (3.13.1)
rubocop (1.66.1)
rubocop (1.68.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand All @@ -93,14 +93,14 @@ GEM
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
rubocop-ast (1.33.1)
parser (>= 3.3.1.0)
rubocop-performance (1.21.1)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (3.0.5)
rubocop-rspec (3.2.0)
rubocop (~> 1.61)
ruby-progressbar (1.13.0)
shellany (0.0.1)
Expand All @@ -111,10 +111,10 @@ GEM
simplecov-html (0.13.1)
simplecov_json_formatter (0.1.4)
thor (1.3.2)
unicode-display_width (2.5.0)
unicode-display_width (2.6.0)
vcr (6.3.1)
base64
webmock (3.23.1)
webmock (3.24.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
Expand Down Expand Up @@ -145,4 +145,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
2.5.18
2.5.22
69 changes: 48 additions & 21 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
'MQTT_TOPIC_HEATPUMP_POWER' => %w[heatpump_power integer],
}.freeze

class ConfigError < StandardError
def backtrace
[]
end
end

class Config
attr_accessor :mqtt_host,
:mqtt_port,
Expand Down Expand Up @@ -86,9 +92,12 @@ def mappings_from(env)

mapping_groups
.transform_values do |values|
values.to_h.transform_keys do |key|
key.match(MAPPING_REGEX)[2].downcase.to_sym
end
mapping_group = values.first[0].match(MAPPING_REGEX)[1]

values
.to_h
.transform_keys { |key| key.match(MAPPING_REGEX)[2].downcase.to_sym }
.merge(mapping_group:)
end
.values
end
Expand Down Expand Up @@ -175,27 +184,45 @@ def validate_url!(url)
end

def validate_mappings!
mappings.each do |value|
# Ensure all required keys are present
unless (value.keys & %i[topic measurement field type]).size == 4 ||
(
value.keys &
%i[
topic
measurement_positive
measurement_negative
field_positive
field_negative
type
]
).size == 6
raise ArgumentError, "Missing required keys: #{value.keys}"
mappings.each_with_index do |mapping, index|
validate_mapping!(index, :topic)
validate_mapping!(index, :type, allow_list: MAPPING_TYPES)

if mapping[:field_positive] || mapping[:field_negative]
validate_mapping!(index, :field_positive)
validate_mapping!(index, :field_negative)
validate_mapping!(index, :measurement_positive)
validate_mapping!(index, :measurement_negative)

validate_mapping!(index, :field, present: false)
validate_mapping!(index, :measurement, present: false)
else
validate_mapping!(index, :field)
validate_mapping!(index, :measurement)

validate_mapping!(index, :field_negative, present: false)
validate_mapping!(index, :field_positive, present: false)
validate_mapping!(index, :measurement_positive, present: false)
validate_mapping!(index, :measurement_negative, present: false)
end
end
end

def validate_mapping!(index, key, present: true, allow_list: nil)
mapping = mappings[index]
var = "MAPPING_#{mapping[:mapping_group]}_#{key.upcase}"

if present
if mapping[key].nil? || mapping[key].strip == ''
raise ConfigError, "Missing variable: #{var}"
end

# Ensure type is valid
unless MAPPING_TYPES.include?(value[:type])
raise ArgumentError, "Invalid type: #{value[:type]}"
if allow_list && !allow_list.include?(mapping[key])
raise ConfigError,
"Variable #{var} is invalid: #{mapping[key]}. Must be one of: #{allow_list.join(', ')}"
end
elsif mapping[key]
raise ConfigError, "Unexpected variable: #{var}"
end
end
end
12 changes: 6 additions & 6 deletions spec/cassettes/influx_success.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion spec/lib/config_mapping_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
measurement: 'PV',
field: 'inverter_power',
type: 'integer',
mapping_group: '0',
},
{
topic: 'senec/0/ENERGY/GUI_HOUSE_POW',
measurement: 'PV',
field: 'house_power',
type: 'integer',
mapping_group: '1',
},
{
topic: 'senec/0/ENERGY/GUI_GRID_POW',
Expand All @@ -66,6 +68,7 @@
field_positive: 'grid_import_power',
field_negative: 'grid_export_power',
type: 'integer',
mapping_group: '2',
},
],
)
Expand All @@ -86,7 +89,7 @@
let(:env) { other_env.merge(hash) }

it 'raises an error' do
expect { config }.to raise_error(ArgumentError)
expect { config }.to raise_error(ConfigError)
end
end
end
Expand Down
Loading

0 comments on commit 2c440f1

Please sign in to comment.