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) diff --git a/custom_components/solarman/solarman.py b/custom_components/solarman/solarman.py index 318a0ec..d6f04c6 100644 --- a/custom_components/solarman/solarman.py +++ b/custom_components/solarman/solarman.py @@ -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):