Skip to content

Commit

Permalink
Add status fields for the integration (lastUpdate, and connection state)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanJoubert committed Feb 6, 2022
1 parent 5c0dd7a commit e632a64
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 23 deletions.
59 changes: 37 additions & 22 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
################################################################################
# Solarman local interface.
#
# This component can retrieve data solarman using version 5 of the protocol.
# This component can retrieve data from the solarman dongle using version 5
# of the protocol.
#
###############################################################################

Expand Down Expand Up @@ -50,51 +51,64 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
hass_sensors.append(SunsynkSensorText(inverter_name, inverter, sensor, inverter_sn))
else:
hass_sensors.append(SunsynkSensor(inverter_name, inverter, sensor, inverter_sn))

hass_sensors.append(SynsynkStatus(inverter_name, inverter, "status_lastUpdate", inverter_sn))
hass_sensors.append(SynsynkStatus(inverter_name, inverter, "status_connection", inverter_sn))
add_devices(hass_sensors)

#############################################################################################################
# This is the entity seen by Home Assistant.
# It derives from the Entity class in HA and is suited for status values.
#############################################################################################################


class SunsynkSensorText(Entity):
def __init__(self, inverter_name, inverter, sensor, sn):
class SynsynkStatus(Entity):
def __init__(self,inverter_name, inverter, field_name, sn):
self._inverter_name = inverter_name
self.inverter = inverter
self._field_name = sensor['name']
if 'icon' in sensor:
self.p_icon = sensor['icon']
else:
self.p_icon = ''
self.p_name = self._inverter_name
self._field_name = field_name
self.p_state = None
self._sn = sn
return


self.p_icon = 'mdi:magnify'
self._sn = sn
return
@property
def icon(self):
# Return the icon of the sensor. """
return self.p_icon
return self.p_icon

@property
def name(self):
# Return the name of the sensor.
return "{} {}".format(self.p_name, self._field_name)
return "{} {}".format(self._inverter_name, self._field_name)

@property
def unique_id(self):
# Return a unique_id based on the serial number
return "{}_{}_{}".format(self.p_name, self._sn, self._field_name)
return "{}_{}_{}".format(self._inverter_name, self._sn, self._field_name)

@property
def state(self):
# Return the state of the sensor.
return self.p_state
return self.p_state

def update(self):
self.p_state = getattr(self.inverter, self._field_name)

#############################################################################################################
# Entity displaying a text field read from the inverter
# Overrides the Status entity, supply the configured icon, and updates the inverter parameters
#############################################################################################################

class SunsynkSensorText(SynsynkStatus):
def __init__(self, inverter_name, inverter, sensor, sn):
SynsynkStatus.__init__(self,inverter_name, inverter, sensor['name'], sn)
if 'icon' in sensor:
self.p_icon = sensor['icon']
else:
self.p_icon = ''
return


def update(self):
# Update this sensor using the data.
# Get the latest data and use it to update our sensor state.
Expand All @@ -108,10 +122,11 @@ def update(self):


#############################################################################################################
# This is the numeric sensor entity seen by Home Assistant.
# It derives from the SunsynkSensorText class and add the fields required for numeric sensors
# Entity displaying a numeric field read from the inverter
# Overrides the Text sensor and supply the device class, last_reset and unit of measurement
#############################################################################################################


class SunsynkSensor(SunsynkSensorText):
def __init__(self, inverter_name, inverter, sensor, sn):
SunsynkSensorText.__init__(self, inverter_name, inverter, sensor, sn)
Expand Down
7 changes: 6 additions & 1 deletion custom_components/solarman/solarman.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import socket
import yaml
from homeassistant.util import Throttle

from datetime import datetime
from .parser import ParameterParser
from .const import *

Expand All @@ -18,6 +18,8 @@ def __init__(self, path, serial, host, port):
self._host = host
self._port = port
self._current_val = None
self.status_connection = "Disconnected"
self.status_lastUpdate = "N/A"
with open(self.path +'parameters.yaml') as f:
self.parameter_definition = yaml.full_load(f)

Expand Down Expand Up @@ -80,10 +82,13 @@ def send_request (self, params, start, end):
sock.connect((self._host, self._port))
sock.sendall(request) # Request param 0x3B up to 0x71
raw_msg = sock.recv(1024)
self.status_lastUpdate = datetime.now().strftime("%m/%d/%Y, %H:%M:%S")
self.status_connection = "Connected"
params.parse(raw_msg, start, length)
del raw_msg
except:
print ('Could not connect to the inverter on %s:%s', self._host, self._port)
self.status_connection = "Disconnected"
finally:
sock.close()

Expand Down

0 comments on commit e632a64

Please sign in to comment.