Skip to content

Commit

Permalink
custom component
Browse files Browse the repository at this point in the history
  • Loading branch information
neilbacon committed Aug 29, 2023
1 parent d25f93d commit 735a892
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 16 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/sofar
sofar_g3_lsw3_logger_reader
config.yaml
sofar
sofar-arm
.idea
.idea
25 changes: 12 additions & 13 deletions adapters/export/mosquitto/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"
"log"
"strings"
"time"

mqtt "github.com/eclipse/paho.mqtt.golang"
Expand Down Expand Up @@ -67,25 +66,25 @@ func (conn *Connection) publish(topic string, msg string, retain bool) {
}

// return "power" for kW etc., "energy" for kWh etc.
func unit2DeviceClass(unit string) string {
if strings.HasSuffix(unit, "Wh") {
return "energy"
} else if strings.HasSuffix(unit, "W") {
return "power"
} else {
return ""
}
}
// func unit2DeviceClass(unit string) string {
// if strings.HasSuffix(unit, "Wh") {
// return "energy"
// } else if strings.HasSuffix(unit, "W") {
// return "power"
// } else {
// return ""
// }
// }

// MQTT Discovery: https://www.home-assistant.io/integrations/mqtt/#mqtt-discovery
func (conn *Connection) InsertDiscoveryRecord(discovery string, state string, fields []ports.DiscoveryField) error {
uniq := "01ad" // TODO: get from config?
for _, f := range fields {
topic := fmt.Sprintf("%s/%s/config", discovery, f.Name)
json, _ := json.Marshal(map[string]interface{}{
"name": f.Name,
"unique_id": fmt.Sprintf("%s_%s", f.Name, uniq),
"device_class": unit2DeviceClass(f.Unit),
"name": f.Name,
"unique_id": fmt.Sprintf("%s_%s", f.Name, uniq),
// "device_class": unit2DeviceClass(f.Unit), // TODO: not working, always "energy"
// "state_class": "measurement",
"state_topic": state,
"unit_of_measurement": f.Unit,
Expand Down
32 changes: 32 additions & 0 deletions custom_components/sofar_g3_lsw3_logger_reader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Custom Component to run sofar_g3_lsw3_logger_reader

## Introduction

`sofar_g3_lsw3_logger_reader` is a program written in go that:
- loops forever, polling the LSW3 data logger and writing the data to a MQTT topic
- reads its `config.yaml` from the current working directory
- writes logging to stderr
- supports MQTT Discovery

This component runs `sofar_g3_lsw3_logger_reader` with:
- the current working directory set to /config/custom_components/sofar_g3_lsw3_logger_reader
- stdout/err going to files out/err.log (because I haven't succeeded in integrating it with homeassistant logging)

## Installation

1. First get `sofar_g3_lsw3_logger_reader` working on your dev machine, so that you have a working `config.yaml` and `sofar` (x86) and `sofar-arm` (arm) binary executables.
2. Copy this directory `custom_components/sofar_g3_lsw3_logger_reader` to homeassistant's `/config/custom_components`
(creating `/config/custom_components/sofar_g3_lsw3_logger_reader`).
You can use the `Samba share` or `Advanced SSH & Web Terminal` add-ons to do this.
3. Copy the working `config.yaml` (from step 1) to the same directory.
4. Copy the `sofar` (x86) or `sofar-arm` (arm) binary executable (from step 1) to `sofar` in this same directory.
5. Enable the custom component by adding a line `sofar_g3_lsw3_logger_reader:` to homeassistant's `/config/configuration.yaml`.
6. Do a full restart of homeassistant: `Developer Tools` > `YAML` > `CHECK CONFIGURATION` then `RESTART` > `Restart Home Assistant`
7. Check the content of `err.log` in this same directory.
8. Add the `Inverter` device to your dashhboard: `Settings` > `Devices & Services` > `Integration` > `MQTT` > `1 device` > `ADD TO DASHBOARD`

## To Do

1. Add to HACS
2. Get logging going to homeassistant's log.
3. Add support for "config flow"
20 changes: 20 additions & 0 deletions custom_components/sofar_g3_lsw3_logger_reader/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Integration for Sofar Solar PV inverters with LSW-3 WiFi sticks with SN 23XXXXXXX"""

import subprocess
import logging
import os

DOMAIN = "sofar_g3_lsw3_logger_reader"
_LOGGER = logging.getLogger(__name__)

def setup(hass, config):
hass.states.set("sofar_g3_lsw3_logger_reader.status", __name__)
_LOGGER.warning("setup: Working Dir is %s", os.getcwd())
_LOGGER.info("setup: Working Dir is %s", os.getcwd()) # main configuration.yaml should enable info, but not yet working

out = open(f"/config/custom_components/{DOMAIN}/out.log", "w") # empty
err = open(f"/config/custom_components/{DOMAIN}/err.log", "w") # go logging package writes to stderr
proc = subprocess.Popen(f"/config/custom_components/{DOMAIN}/sofar", cwd=f"/config/custom_components/{DOMAIN}", stdout=out, stderr=err)
# the sofar process is left running forever
_LOGGER.warning("setup: end")
return True
9 changes: 9 additions & 0 deletions custom_components/sofar_g3_lsw3_logger_reader/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"domain": "sofar_g3_lsw3_logger_reader",
"name": "Sofar LSW3",
"version": "0.1.0",
"integration_type": "device",
"iot_class": "local_polling",
"dependencies": ["mqtt"],
"documentation": "https://github.com/neilbacon/sofar_g3_lsw3_logger_reader"
}

0 comments on commit 735a892

Please sign in to comment.