Replace the ENV variable with a config file
So it's safer to configure, as we don't need to pass env variables to a docker run service. Also it's easier to configure without needing to pass a whole lot of env vars.
This commit is contained in:
5 files changed
+70
-24
No files matched your search
@@ -1,2 +1,3 @@
|
||||
__pycache__
|
||||
*.pyc
|
||||
config.yaml
|
||||
@@ -1,7 +1,42 @@
|
||||
## Usage
|
||||
|
||||
### Installation
|
||||
|
||||
The easiest way is to run the docker container and set it to restart always. This basically makes it a system service that will always run.
|
||||
The easiest way this is to run the docker container and set it to restart always. This basically makes it a system service that will always run.
|
||||
|
||||
To make it safe and avoid passing credentials we're using a configuration file.
|
||||
|
||||
```shell
|
||||
docker run --restart=always -e MODBUS_HOST=192.168.1.1 solaredge_modbus_mqtt:latest
|
||||
mkdir /etc/solaredge_modbus_mqtt
|
||||
chmod 700 /etc/solaredge_modbus_mqtt
|
||||
cp config.yaml-dist /etc/solaredge_modbus_mqtt/config.yaml
|
||||
docker run --name=solaredge_modbus_mqtt --restart=always --volume=/etc/solaredge_modbus_mqtt/config.yaml:/app/config.yaml marijnkoesen/solaredge_modbus_mqtt:latest
|
||||
```
|
||||
|
||||
|
||||
## For developers
|
||||
|
||||
### Building and running the docker image
|
||||
|
||||
```shell
|
||||
docker build -t solaredge_modbus_mqtt:latest .
|
||||
cp config.yaml-dist config.yaml
|
||||
docker run --volume=$PWD/config.yaml:/app/config.yaml solaredge_modbus_mqtt:latest
|
||||
```
|
||||
|
||||
|
||||
### Running without docker
|
||||
|
||||
```shell
|
||||
pip3 install -r requirements.txt
|
||||
cp config.yaml-dist config.yaml
|
||||
python3 solaredge_modbus_mqtt.py
|
||||
```
|
||||
|
||||
|
||||
### Running tests
|
||||
|
||||
```shell
|
||||
pip3 install -r requirements.txt
|
||||
python3 -m unittest
|
||||
```
|
||||
@@ -0,0 +1,13 @@
|
||||
modbus:
|
||||
host: 127.0.0.1
|
||||
port: 1502
|
||||
timeout: 1
|
||||
unit: 1
|
||||
|
||||
mqtt:
|
||||
host: 127.0.0.1
|
||||
port: 1883
|
||||
user: ~
|
||||
pass: ~
|
||||
|
||||
poll_interval: 3
|
||||
+3
-2
@@ -1,2 +1,3 @@
|
||||
paho-mqtt=1.5.0
|
||||
solaredge_modbus=0.2.0
|
||||
paho-mqtt>=1.5.0
|
||||
solaredge_modbus>=0.2.0
|
||||
pyyaml>=5.3
|
||||
+16
-20
@@ -7,38 +7,34 @@ import solaredge_modbus
|
||||
import sys
|
||||
import time
|
||||
import paho.mqtt.client as mqtt
|
||||
import yaml
|
||||
from solaredge_modbus_mqtt import SolarData
|
||||
from pymodbus.exceptions import ConnectionException
|
||||
|
||||
if __name__ == "__main__":
|
||||
argparser = argparse.ArgumentParser()
|
||||
argparser.add_argument("--modbus-host", type=str, default=os.environ.get('MODBUS_HOST', '127.0.0.1'), help="ModbusTCP address")
|
||||
argparser.add_argument("--modbus-port", type=int, default=os.environ.get('MODBUS_PORT', 1502), help="ModbusTCP port")
|
||||
argparser.add_argument("--modbus-timeout", type=int, default=os.environ.get('MODBUS_TIMEOUT', 1), help="Connection timeout")
|
||||
argparser.add_argument("--modbus-unit", type=int, default=os.environ.get('MODBUS_UNIT', 1), help="Modbus unit")
|
||||
|
||||
argparser.add_argument("--mqtt-host", type=str, default=os.environ.get('MQTT_HOST', '127.0.0.1'), help="MQTT Host")
|
||||
argparser.add_argument("--mqtt-port", type=int, default=os.environ.get('MQTT_PORT', 1883), help="MQTT port")
|
||||
argparser.add_argument("--mqtt-user", type=str, default=os.environ.get('MQTT_USER', None), help="MQTT Username")
|
||||
argparser.add_argument("--mqtt-pass", type=str, default=os.environ.get('MQTT_PASS', None), help="MQTT Password")
|
||||
|
||||
argparser.add_argument("--poll-interval", type=int, default=os.environ.get('POLL_INTERVAL', 3), help="Waiting time between each poll")
|
||||
|
||||
argparser.add_argument("--config", type=str, default=os.environ.get('CONFIG_FILE', 'config.yaml'), help="Path to the config.yaml")
|
||||
args = argparser.parse_args()
|
||||
|
||||
with open(args.config, 'r') as stream:
|
||||
try:
|
||||
config = yaml.safe_load(stream)
|
||||
except yaml.YAMLError as exc:
|
||||
print(exc)
|
||||
|
||||
try:
|
||||
mqtt = mqtt.Client(client_id="", clean_session=True, userdata=None, protocol=mqtt.MQTTv311, transport="tcp")
|
||||
mqtt.username_pw_set(args.mqtt_user, args.mqtt_pass)
|
||||
mqtt.connect(args.mqtt_host, args.mqtt_port, 60)
|
||||
mqtt.username_pw_set(config['mqtt']['user'], config['mqtt']['pass'])
|
||||
mqtt.connect(config['mqtt']['host'], config['mqtt']['port'], 60)
|
||||
except OSError as err:
|
||||
print("Cannot connect to MQTT on {0}:{1}, error: {2}".format(args.mqtt_host, args.mqtt_port, err))
|
||||
print("Cannot connect to MQTT on {0}:{1}, error: {2}".format(config['mqtt']['host'], config['mqtt']['port'], err))
|
||||
sys.exit(1)
|
||||
|
||||
inverter = solaredge_modbus.Inverter(
|
||||
host=args.modbus_host,
|
||||
port=args.modbus_port,
|
||||
timeout=args.modbus_timeout,
|
||||
unit=args.modbus_unit
|
||||
host=config['modbus']['host'],
|
||||
port=config['modbus']['port'],
|
||||
timeout=config['modbus']['timeout'],
|
||||
unit=config['modbus']['unit']
|
||||
)
|
||||
|
||||
run = True
|
||||
@@ -47,7 +43,7 @@ if __name__ == "__main__":
|
||||
try:
|
||||
solar_data = SolarData(inverter.read_all())
|
||||
except ConnectionException as err:
|
||||
print("Cannot connect to SolarEdge Modbus server on {0}:{1}, error: {2}".format(args.modbus_host, args.modbus_port, err))
|
||||
print("Cannot connect to SolarEdge Modbus server on {0}:{1}, error: {2}".format(config['modbus']['host'], config['modbus']['port'], err))
|
||||
sys.exit(2)
|
||||
|
||||
mqtt.loop()
|
||||
|
||||
Reference in new issue
Block a user