Skip to content

Commit

Permalink
Adjustment to changed API
Browse files Browse the repository at this point in the history
  • Loading branch information
ledermann committed Aug 18, 2023
1 parent caebb2d commit 78bb5ea
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
9 changes: 5 additions & 4 deletions lib/senec/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ module Senec
# For a full list of available vars, see http://[IP-of-your-SENEC]/vars.html
# Comments taken from https://gist.github.com/smashnet/82ad0b9d7f0ba2e5098e6649ba08f88a
BASIC_REQUEST = {
STATISTIC: {
CURRENT_STATE: '', # Current state of the system (int, see SYSTEM_STATE_NAME)
MEASURE_TIME: '' # Unix timestamp for above values (ms)
},
ENERGY: {
STAT_STATE: '', # Current state of the system (int, see SYSTEM_STATE_NAME)
GUI_BAT_DATA_CURRENT: '', # Battery charge current: negative if discharging, positiv if charging (A)
GUI_BAT_DATA_FUEL_CHARGE: '', # Remaining battery (percent)
GUI_BAT_DATA_POWER: '', # Battery charge power: negative if discharging, positiv if charging (W)
Expand All @@ -16,6 +13,10 @@ module Senec
GUI_INVERTER_POWER: '', # PV production (W)
STAT_HOURS_OF_OPERATION: '' # Appliance hours of operation
},
RTC: {
UTC_OFFSET: '',
WEB_TIME: ''
},
PV1: {
MPP_POWER: '' # List: MPP power (W)
},
Expand Down
7 changes: 5 additions & 2 deletions lib/senec/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def case_temp
end

def current_state
get('STATISTIC', 'CURRENT_STATE')
get('ENERGY', 'STAT_STATE')
end

def current_state_name
Expand All @@ -63,7 +63,10 @@ def current_state_name
end

def measure_time
get('STATISTIC', 'MEASURE_TIME')
web_time = get('RTC', 'WEB_TIME')
utc_offset = get('RTC', 'UTC_OFFSET')

web_time - (utc_offset * 60)
end

private
Expand Down
20 changes: 13 additions & 7 deletions lib/senec/value.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
module Senec
class Value
def initialize(data)
@data = data
def initialize(raw)
@raw = raw
@prefix, @value = raw&.split('_')
end

def decoded
parts = @data.split('_')
prefix = parts[0]
value = parts[1]
attr_reader :prefix, :value, :raw

def valid?
prefix && PREFIXES.include?(prefix)
end

def decoded
case prefix
when 'fl'
decoded_float(value)
Expand All @@ -18,7 +21,7 @@ def decoded
value
# TODO: There are some more prefixes to handle
else
raise Senec::DecodingError, "Unknown value '#{@data}'"
raise Senec::DecodingError, "Unknown value '#{@raw}'"
end
end

Expand All @@ -28,6 +31,9 @@ def decoded

private

PREFIXES = %w[fl i3 u1 u3 u6 u8 st].freeze
private_constant :PREFIXES

def decoded_float(hex)
["0x#{hex}".to_i(16)].pack('L').unpack1('F').round(1)
end
Expand Down
26 changes: 13 additions & 13 deletions spec/lib/senec/request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
RSpec.describe Senec::Request do
subject(:request) { described_class.new(host: host, state_names: state_names) }

let(:state_names) { { 54 => 'LADESCHLUSSPHASE' } }
let(:state_names) { { 56 => 'PEAK-SHAVING: WARTEN' } }

context 'with a valid host', vcr: { cassette_name: 'request' } do
let(:host) { 'senec' }

describe '#house_power' do
subject { request.house_power }

it { is_expected.to eq(382.0) }
it { is_expected.to eq(433.9) }
end

describe '#inverter_power' do
subject { request.inverter_power }

it { is_expected.to eq(1265.0) }
it { is_expected.to eq(1404.4) }
end

describe '#mpp_power' do
subject { request.mpp_power }

it { is_expected.to eq([624.4, 0.0, 640.7]) }
it { is_expected.to eq([705.9, 0.0, 698.5]) }
end

describe '#bat_power' do
subject { request.bat_power }

it { is_expected.to eq(262.4) }
it { is_expected.to eq(89.1) }
end

describe '#bat_fuel_charge' do
subject { request.bat_fuel_charge }

it { is_expected.to eq(100.0) }
it { is_expected.to eq(60.6) }
end

describe '#bat_charge_current' do
subject { request.bat_charge_current }

it { is_expected.to eq(4.6) }
it { is_expected.to eq(1.7) }
end

describe '#bat_voltage' do
subject { request.bat_voltage }

it { is_expected.to eq(57.5) }
it { is_expected.to eq(53.0) }
end

describe '#grid_power' do
subject { request.grid_power }

it { is_expected.to eq(-620.7) }
it { is_expected.to eq(-881.3) }
end

describe '#wallbox_charge_power' do
Expand All @@ -63,25 +63,25 @@
describe '#case_temp' do
subject { request.case_temp }

it { is_expected.to eq(34.5) }
it { is_expected.to eq(34.0) }
end

describe '#current_state' do
subject { request.current_state }

it { is_expected.to eq(54) }
it { is_expected.to eq(56) }
end

describe '#current_state_name' do
subject { request.current_state_name }

it { is_expected.to eq('LADESCHLUSSPHASE') }
it { is_expected.to eq('PEAK-SHAVING: WARTEN') }
end

describe '#measure_time' do
subject { Time.at(request.measure_time) }

it { is_expected.to eq(Time.parse('2023-08-17 15:23:51.000000000 +0200')) }
it { is_expected.to eq(Time.parse('2023-08-18 09:52:57.000000000 +0200')) }
end
end

Expand Down
7 changes: 7 additions & 0 deletions spec/lib/senec/value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
it { is_expected.to eq(254.5) }
end

context 'when i3' do
let(:data) { 'i3_00000078' }

it { is_expected.to be_a(Integer) }
it { is_expected.to eq(120) }
end

context 'when u1' do
let(:data) { 'u1_FFFF' }

Expand Down
6 changes: 3 additions & 3 deletions spec/support/cassettes/request.yml

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

0 comments on commit 78bb5ea

Please sign in to comment.