Skip to content

Commit

Permalink
stats available is dynamic now
Browse files Browse the repository at this point in the history
  • Loading branch information
marq24 committed Sep 9, 2023
1 parent 7213ffa commit 65c03e2
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 18 deletions.
8 changes: 2 additions & 6 deletions custom_components/senec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
CONF_SYSTYPE_INVERTER,
CONF_SYSTYPE_WEB,
CONF_DEV_MASTER_NUM,
CONF_SUPPORT_STATS,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/senec/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
5 changes: 0 additions & 5 deletions custom_components/senec/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion custom_components/senec/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion custom_components/senec/pysenec_ha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions custom_components/senec/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
INVERTER_SENSOR_TYPES,
WEB_SENSOR_TYPES,
CONF_SUPPORT_BDC,
CONF_SUPPORT_STATS,
CONF_SYSTYPE_INVERTER,
CONF_SYSTYPE_WEB
)
Expand Down Expand Up @@ -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
Expand All @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion custom_components/senec/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down

0 comments on commit 65c03e2

Please sign in to comment.