Skip to content

Commit

Permalink
Merge pull request #208 from YodaDaCoda/feat/device-group-sensors
Browse files Browse the repository at this point in the history
Group Sensors into a Device
  • Loading branch information
StephanJoubert committed Feb 11, 2023
2 parents 3028093 + 673b052 commit 3788e7a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
40 changes: 36 additions & 4 deletions custom_components/solarman/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,48 @@ async def async_setup_platform(hass: HomeAssistant, config, async_add_entities :
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback):
_LOGGER.debug(f'sensor.py:async_setup_entry: {entry.options}')
_do_setup_platform(hass, entry.options, async_add_entities)




#############################################################################################################
# This is the Device seen by Home Assistant.
# It provides device_info to Home Assistant which allows grouping all the Entities under a single Device.
#############################################################################################################

class SolarmanSensor():
"""Solarman Device class."""

def __init__(self, id: str = None, device_name: str = None, model: str = None, manufacturer: str = None):
self.id = id
self.device_name = device_name
self.model = model
self.manufacturer = manufacturer

@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self.id)},
"name": self.device_name,
"model": self.model,
"manufacturer": self.manufacturer,
}

@property
def extra_state_attributes(self):
"""Return the extra state attributes."""
return {
"id": self.id,
"integration": DOMAIN,
}


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

class SolarmanStatus(Entity):
class SolarmanStatus(SolarmanSensor, Entity):
def __init__(self, inverter_name, inverter, field_name, sn):
super().__init__(sn, inverter_name, inverter.lookup_file)
self._inverter_name = inverter_name
self.inverter = inverter
self._field_name = field_name
Expand Down Expand Up @@ -118,6 +150,7 @@ def state(self):
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
Expand Down Expand Up @@ -152,7 +185,6 @@ def update(self):
# Overrides the Text sensor and supply the device class, last_reset and unit of measurement
#############################################################################################################


class SolarmanSensor(SolarmanSensorText):
def __init__(self, inverter_name, inverter, sensor, sn):
SolarmanSensorText.__init__(self, inverter_name, inverter, sensor, sn)
Expand Down
10 changes: 4 additions & 6 deletions custom_components/solarman/solarman.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ def __init__(self, path, serial, host, port, mb_slaveid, lookup_file):
self._current_val = None
self.status_connection = "Disconnected"
self.status_lastUpdate = "N/A"
if not lookup_file:
lookup_file = 'deye_hybrid.yaml'
elif lookup_file == 'parameters.yaml':
lookup_file = 'deye_hybrid.yaml'
self.lookup_file = lookup_file
if not self.lookup_file or lookup_file == 'parameters.yaml':
self.lookup_file = 'deye_hybrid.yaml'


with open(self.path + lookup_file) as f:
with open(self.path + self.lookup_file) as f:
self.parameter_definition = yaml.full_load(f)

def modbus(self, data):
Expand Down

0 comments on commit 3788e7a

Please sign in to comment.