hackwerken / solaredge2influx

Python script to send data from the Solaredge API to InfluxDB
MIT License
1 stars 0 forks source link

Does not work with Influx set to HTTPS #1

Open MaKloth opened 3 years ago

MaKloth commented 3 years ago

I use influxdb with ssl (unsafe because of self signed cert). Therefore I get following error if host is set to localhost in config file.

  File "/usr/lib/python3/dist-packages/influxdb/client.py", line 286, in request
    raise InfluxDBClientError(response.content, response.status_code)
influxdb.exceptions.InfluxDBClientError: 400: Client sent an HTTP request to an HTTPS server.

Changing host to https://localhost raises following errors:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 159, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw)
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 57, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/lib/python3.7/socket.py", line 748, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.7/http/client.py", line 1244, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1290, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1239, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.7/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/lib/python3.7/http/client.py", line 966, in send
    self.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 181, in connect
    conn = self._new_conn()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 168, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0xb5e43090>: Failed to establish a new connection: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='https', port=80): Max retries exceeded with url: //localhost:8086/query?q=SELECT+LAST%28power%29+FROM+%22pv%22&db=solaredge (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xb5e43090>: Failed to establish a new connection: [Errno -2] Name or service not known'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 78, in <module>
    main()
  File "main.py", line 70, in main
    send(solaredge_api, influxdb)
  File "main.py", line 22, in send
    last_update = influxdb.get_last_update()
  File "/home/pi/solaredge2influx/influx.py", line 15, in get_last_update
    select = self._influxdb.query('SELECT LAST(power) FROM "pv"')
  File "/usr/lib/python3/dist-packages/influxdb/client.py", line 416, in query
    expected_response_code=expected_response_code
  File "/usr/lib/python3/dist-packages/influxdb/client.py", line 267, in request
    timeout=self._timeout
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='https', port=80): Max retries exceeded with url: //localhost:8086/query?q=SELECT+LAST%28power%29+FROM+%22pv%22&db=solaredge (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0xb5e43090>: Failed to establish a new connection: [Errno -2] Name or service not known'))
hackwerken commented 3 years ago

Hi MaKloth,

Honestly I didn't expect anyone else to start using this script :sweat_smile: . But, of course you are welcome to use it as you see fit.

Regarding your issue: I never tested with TLS because I don't need it myself. However, I had a quick look at the Python InfluxDB client documentation, and it should be easy to implement this.

Everything InfluxDB related is handled in influx.py. You could forward the ssl (and maybe verify_ssl) options from the constructor to the client object (line 12). And then pass the configuration from the main.py file.

Feel free to submit a pull request if you happen to implement these changes.

Regards, Hackwerken

MaKloth commented 3 years ago

It works as you suggested to extend the constructor. Thanks.