-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MQTT Discovery, filtering, custom component #13
Conversation
Lovely, I'll try to review this soon :) |
I really like those changes but my original assumption was to create a standalone data collector for SOFAR inverters. It's not written to be used only with Home Assistant, Your changes change this approach. Also, check build there are some problems detected by linter with error check :) |
If you just want to close this, it's fine by me. More changes I want to do in my fork:
|
Sorry for long time to respond :) By separating changes I mean only home assistant plugin changes so whole Of course, I think we should link repo with Your HA plugin here in README :) Hope it's more clear right now, resuming:
If You agree with that please clean this PR from unwanted changes, than I'l go with CR :) PS. I have idea for integrating HA with this tool using HA add-on https://www.home-assistant.io/addons/. Then whole integration (with build binaries) will be updated using HA interface, no extra config, file coping or compiling is needed. I'm using such an approach and it's working brilliant, but whole config is drafted and I never finish it for publishing :( |
|
|
Hi Jakub, I'm still interested in having the inverter data in HA, but it's become less of an issue for me after installing a Shelly EM:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I leave some comments. If You don't want to fix it let me know I'll fix it myself :)
I was wondering about the external energy meter too because I want to monitor energy consumption for the whole house. BTW how accurate are readings from the inverter vs external energy monitor?
Big thanks for Your contribution once again
build-arm: | ||
env GOOS=linux GOARCH=arm GOARM=5 go build -o sofar-arm | ||
env GOOS=linux GOARCH=arm GOARM=5 go build -o custom_components/sofar_g3_lsw3_logger_reader/sofar-arm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
env GOOS=linux GOARCH=arm GOARM=5 go build -o custom_components/sofar_g3_lsw3_logger_reader/sofar-arm | |
env GOOS=linux GOARCH=arm GOARM=5 go build -o bin/sofar-arm |
build-x86: | ||
go build -o custom_components/sofar_g3_lsw3_logger_reader/sofar-x86 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go build -o custom_components/sofar_g3_lsw3_logger_reader/sofar-x86 | |
go build -o bin/sofar-x86 |
.vscode | ||
.idea |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.idea | |
.idea | |
bin |
if len(s.attrWhiteList) > 0 { | ||
_, ok := s.attrWhiteList[k] | ||
return ok | ||
} else { | ||
for _, re := range s.attrBlackList { | ||
if re.MatchString(k) { | ||
return false | ||
} | ||
} | ||
} | ||
return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's bug. if there is no whitelist elements but there is blacklist element it should ignore elements from blacklist and pass others:
if len(s.attrWhiteList) > 0 { | |
_, ok := s.attrWhiteList[k] | |
return ok | |
} else { | |
for _, re := range s.attrBlackList { | |
if re.MatchString(k) { | |
return false | |
} | |
} | |
} | |
return true | |
if len(s.attrWhiteList) > 0 { | |
if _, ok := s.attrWhiteList[k]; ok { | |
return true | |
} | |
} | |
for _, re := range s.attrBlackList { | |
if re.MatchString(k) { | |
return false | |
} | |
} | |
return true |
Here's unit test for it (fully generated by chat gpt :D )
func TestNameFilter(t *testing.T) {
// Create a Logger instance with the desired attributes
logger := &Logger{
attrWhiteList: map[string]struct{}{
"whitelisted": {},
},
attrBlackList: []*regexp.Regexp{
regexp.MustCompile("^blacklisted"),
},
}
// Test case 1: Key in the white list
result := logger.nameFilter("whitelisted")
if result != true {
t.Errorf("Expected: true, Got: %v", result)
}
// Test case 2: Key not in the white list, but not matching any black list regex
result = logger.nameFilter("notblacklisted")
if result != true {
t.Errorf("Expected: true, Got: %v", result)
}
// Test case 3: Key in the black list
result = logger.nameFilter("blacklisted-key")
if result != false {
t.Errorf("Expected: false, Got: %v", result)
}
// Test case 4: Key not in the white list and matches a black list regex
result = logger.nameFilter("blacklisted")
if result != false {
t.Errorf("Expected: false, Got: %v", result)
}
// Test case 5: No white or black list
logger = &Logger{} // Reset the logger
result = logger.nameFilter("anykey")
if result != true {
t.Errorf("Expected: true, Got: %v", result)
}
}
@@ -0,0 +1,4 @@ | |||
{ | |||
"name": "Sofar LSW3", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we remove this file as this repo doesn't support hacs?
} | ||
|
||
// MQTT Discovery: https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery | ||
func (conn *Connection) InsertDiscoveryRecord(discovery string, state string, expireAfter int, fields []ports.DiscoveryField) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expireAfter is unused
@@ -0,0 +1,29 @@ | |||
# Integration for Sofar Solar PV Inverters with LSW-3 WiFi data logger with SN 23XXXXXXX |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should remove this info as we removed custom_components
|
||
measurements, err := device.Query() | ||
if hasMQTT { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should not be triggered if discovery is empty
User string `yaml:"user"` | ||
Password string `yaml:"password"` | ||
Discovery string `yaml:"discovery"` | ||
State string `yaml:"state"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to stay with a prefix
for the sake of backward compatibility
url: 1.2.3.4:1883 # MQTT broker URL (e.g. 1.2.3.4:1883) | ||
user: # MQTT username (leave empty when not needed) | ||
password: # MQTT password (leave empty when not needed) | ||
discovery: homeassistant/sensor # topic prefix for MQTT Discovery |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because it's home assistant-specific setting I would rename it to ha_discovery_prefix
or smth
Yes, please take/change whatever you want from this (doing it yourself will be quicker than chatting to me about it). My thinking on the white/black list (filtering of attributes) was:
But feel free to make it work some other way if you prefer. The Shelly EM provides 1W resolution, whereas the sofar datalogger provides 10W resolution. My Shelly EM has a 100A current transformer clamp on the grid and a 50A clamp on all household loads. At night these should read the same (assuming there are no other loads - like maybe the inverter uses a little power?), but mine differ by 5W. So I guess the accuracy is something around that, but it could be worse, I don't really know. |
Ok I'll finish it on my branch :) Thanks for contributing once again |
Not sure how much of this you might want: