From 735a892c851f77b551d9d8d80f259b70dce7b7f3 Mon Sep 17 00:00:00 2001 From: Neil Bacon <2782001+neilbacon@users.noreply.github.com> Date: Tue, 29 Aug 2023 18:04:20 +1000 Subject: [PATCH] custom component --- .gitignore | 4 +-- adapters/export/mosquitto/mqtt.go | 25 +++++++-------- .../sofar_g3_lsw3_logger_reader/README.md | 32 +++++++++++++++++++ .../sofar_g3_lsw3_logger_reader/__init__.py | 20 ++++++++++++ .../sofar_g3_lsw3_logger_reader/manifest.json | 9 ++++++ 5 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 custom_components/sofar_g3_lsw3_logger_reader/README.md create mode 100644 custom_components/sofar_g3_lsw3_logger_reader/__init__.py create mode 100644 custom_components/sofar_g3_lsw3_logger_reader/manifest.json diff --git a/.gitignore b/.gitignore index 4c8c71c..5ae1223 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,4 @@ -/sofar -sofar_g3_lsw3_logger_reader config.yaml sofar sofar-arm -.idea \ No newline at end of file +.idea diff --git a/adapters/export/mosquitto/mqtt.go b/adapters/export/mosquitto/mqtt.go index 85fba50..a775990 100644 --- a/adapters/export/mosquitto/mqtt.go +++ b/adapters/export/mosquitto/mqtt.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "log" - "strings" "time" mqtt "github.com/eclipse/paho.mqtt.golang" @@ -67,15 +66,15 @@ 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 { @@ -83,9 +82,9 @@ func (conn *Connection) InsertDiscoveryRecord(discovery string, state string, fi 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, diff --git a/custom_components/sofar_g3_lsw3_logger_reader/README.md b/custom_components/sofar_g3_lsw3_logger_reader/README.md new file mode 100644 index 0000000..36c8e2e --- /dev/null +++ b/custom_components/sofar_g3_lsw3_logger_reader/README.md @@ -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" diff --git a/custom_components/sofar_g3_lsw3_logger_reader/__init__.py b/custom_components/sofar_g3_lsw3_logger_reader/__init__.py new file mode 100644 index 0000000..d414774 --- /dev/null +++ b/custom_components/sofar_g3_lsw3_logger_reader/__init__.py @@ -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 diff --git a/custom_components/sofar_g3_lsw3_logger_reader/manifest.json b/custom_components/sofar_g3_lsw3_logger_reader/manifest.json new file mode 100644 index 0000000..ee83873 --- /dev/null +++ b/custom_components/sofar_g3_lsw3_logger_reader/manifest.json @@ -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" +}