From 65c03e28d74fc32d7d84e37bfaa1f3d5523c3ac0 Mon Sep 17 00:00:00 2001 From: marq24 Date: Sat, 9 Sep 2023 09:00:26 +0200 Subject: [PATCH] stats available is dynamic now --- custom_components/senec/__init__.py | 8 ++------ custom_components/senec/binary_sensor.py | 2 +- custom_components/senec/config_flow.py | 5 ----- custom_components/senec/const.py | 1 - custom_components/senec/pysenec_ha/__init__.py | 3 ++- custom_components/senec/sensor.py | 5 ++--- custom_components/senec/switch.py | 2 +- 7 files changed, 8 insertions(+), 18 deletions(-) diff --git a/custom_components/senec/__init__.py b/custom_components/senec/__init__.py index 2655cbc..aa9375d 100644 --- a/custom_components/senec/__init__.py +++ b/custom_components/senec/__init__.py @@ -31,7 +31,6 @@ CONF_SYSTYPE_INVERTER, CONF_SYSTYPE_WEB, CONF_DEV_MASTER_NUM, - CONF_SUPPORT_STATS, ) _LOGGER = logging.getLogger(__name__) @@ -63,10 +62,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry): # after the refresh we should know if the lala.cgi return STATISTIC data # or not... if CONF_TYPE not in config_entry.data or config_entry.data[CONF_TYPE] in (CONF_SYSTYPE_SENEC, CONF_SYSTYPE_SENEC_V2): - # make sure, that we have the correct value if STATS are supported (or not) - config_entry.data[CONF_SUPPORT_STATS] = coordinator.senec.grid_total_export is not None - # and also updating the 'coordinator._entry' object... - coordinator._config_entry.data[CONF_SUPPORT_STATS] = config_entry.data[CONF_SUPPORT_STATS] + coordinator._statistics_available = coordinator.senec.grid_total_export is not None hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][config_entry.entry_id] = coordinator @@ -111,7 +107,7 @@ def __init__(self, hass, session, config_entry): self.name = config_entry.title self._config_entry = config_entry - + self._statistics_available = False super().__init__(hass, _LOGGER, name=DOMAIN, update_interval=SCAN_INTERVAL) async def _async_update_data(self): diff --git a/custom_components/senec/binary_sensor.py b/custom_components/senec/binary_sensor.py index 0eda8e6..90f4752 100644 --- a/custom_components/senec/binary_sensor.py +++ b/custom_components/senec/binary_sensor.py @@ -40,7 +40,7 @@ def __init__( else: self._attr_entity_registry_enabled_default = True - title = self.coordinator._entry.title + title = self.coordinator._config_entry.title key = self.entity_description.key name = self.entity_description.name self.entity_id = f"binary_sensor.{slugify(title)}_{key}" diff --git a/custom_components/senec/config_flow.py b/custom_components/senec/config_flow.py index 2078491..44a8f1d 100644 --- a/custom_components/senec/config_flow.py +++ b/custom_components/senec/config_flow.py @@ -41,7 +41,6 @@ CONF_DEV_TYPE, CONF_DEV_TYPE_INT, CONF_USE_HTTPS, - CONF_SUPPORT_STATS, CONF_SUPPORT_BDC, CONF_DEV_NAME, CONF_DEV_SERIAL, @@ -102,9 +101,6 @@ async def _test_connection_senec(self, host, use_https): self._use_https = use_https self._stats_available = senec_client.grid_total_export is not None - # just for local testing... - #self._stats_available = False - _LOGGER.info( "Successfully connect to SENEC.Home (using https? %s) at %s", use_https, host, @@ -283,7 +279,6 @@ async def async_step_system(self, user_input=None): CONF_USE_HTTPS: self._use_https, CONF_SCAN_INTERVAL: scan, CONF_TYPE: CONF_SYSTYPE_SENEC, - CONF_SUPPORT_STATS: self._stats_available, CONF_DEV_TYPE_INT: self._device_type_internal, CONF_DEV_TYPE: self._device_type, CONF_DEV_NAME: self._device_name, diff --git a/custom_components/senec/const.py b/custom_components/senec/const.py index 06f2512..28909c3 100644 --- a/custom_components/senec/const.py +++ b/custom_components/senec/const.py @@ -40,7 +40,6 @@ CONF_DEV_TYPE_INT: Final = "dtype_int" CONF_USE_HTTPS: Final = "use_https" CONF_SUPPORT_BDC: Final = "has_bdc_support" -CONF_SUPPORT_STATS: Final = "has_statistics" CONF_DEV_NAME: Final = "dname" CONF_DEV_SERIAL: Final = "dserial" CONF_DEV_VERSION: Final = "version" diff --git a/custom_components/senec/pysenec_ha/__init__.py b/custom_components/senec/pysenec_ha/__init__.py index 0572309..e2c594d 100644 --- a/custom_components/senec/pysenec_ha/__init__.py +++ b/custom_components/senec/pysenec_ha/__init__.py @@ -248,7 +248,8 @@ def grid_total_export(self) -> float: """ Total energy exported to grid export (kWh) """ - if hasattr(self, '_raw') and "STATISTIC" in self._raw and "LIVE_GRID_EXPORT" in self._raw["STATISTIC"]: + if hasattr(self, '_raw') and "STATISTIC" in self._raw and "LIVE_GRID_EXPORT" in self._raw["STATISTIC"] and \ + self._raw["STATISTIC"]["LIVE_GRID_EXPORT"] != "VARIABLE_NOT_FOUND": return self._raw["STATISTIC"]["LIVE_GRID_EXPORT"] @property diff --git a/custom_components/senec/sensor.py b/custom_components/senec/sensor.py index 7b61e1a..0e6c838 100644 --- a/custom_components/senec/sensor.py +++ b/custom_components/senec/sensor.py @@ -14,7 +14,6 @@ INVERTER_SENSOR_TYPES, WEB_SENSOR_TYPES, CONF_SUPPORT_BDC, - CONF_SUPPORT_STATS, CONF_SYSTYPE_INVERTER, CONF_SYSTYPE_WEB ) @@ -50,7 +49,7 @@ async def async_setup_entry(hass: HomeAssistantType, config_entry: ConfigEntry, addEntity = description.controls is None if not addEntity: if 'require_stats_fields' in description.controls: - if CONF_SUPPORT_STATS not in config_entry.data or config_entry.data[CONF_SUPPORT_STATS]: + if coordinator._statistics_available: addEntity = True else: addEntity = True @@ -77,7 +76,7 @@ def __init__( else: self._attr_entity_registry_enabled_default = True - title = self.coordinator._entry.title + title = self.coordinator._config_entry.title key = self.entity_description.key name = self.entity_description.name self.entity_id = f"sensor.{slugify(title)}_{key}" diff --git a/custom_components/senec/switch.py b/custom_components/senec/switch.py index 24c1be8..96e0d5c 100644 --- a/custom_components/senec/switch.py +++ b/custom_components/senec/switch.py @@ -42,7 +42,7 @@ def __init__( else: self._attr_entity_registry_enabled_default = True - title = self.coordinator._entry.title + title = self.coordinator._config_entry.title key = self.entity_description.key name = self.entity_description.name self.entity_id = f"switch.{slugify(title)}_{key}"