From 2a7f63bfe6b5d0dea24534c37be69b057d669c86 Mon Sep 17 00:00:00 2001 From: William Brockhus Date: Thu, 9 Feb 2023 09:25:33 +1100 Subject: [PATCH 1/2] feat: store/expose lookup_file on the Inverter --- custom_components/solarman/solarman.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/custom_components/solarman/solarman.py b/custom_components/solarman/solarman.py index 9416e05..95c895a 100644 --- a/custom_components/solarman/solarman.py +++ b/custom_components/solarman/solarman.py @@ -26,14 +26,12 @@ 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' - - - with open(self.path + lookup_file) as f: - self.parameter_definition = yaml.full_load(f) + 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 + self.lookup_file) as f: + self.parameter_definition = yaml.full_load(f) def modbus(self, data): POLY = 0xA001 From af265dba40430087c2bed2add02c3e346c81696a Mon Sep 17 00:00:00 2001 From: William Brockhus Date: Thu, 9 Feb 2023 09:26:49 +1100 Subject: [PATCH 2/2] feat: expose device_info on sensors this provides Home Assistant the information it needs to group sensors into a single device --- custom_components/solarman/sensor.py | 40 +++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/custom_components/solarman/sensor.py b/custom_components/solarman/sensor.py index 4bf724b..b189db1 100644 --- a/custom_components/solarman/sensor.py +++ b/custom_components/solarman/sensor.py @@ -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 @@ -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 @@ -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)