Skip to content

Commit

Permalink
added option to ignore system state for charge values
Browse files Browse the repository at this point in the history
only use own aiohttp client session for WebAPI
  • Loading branch information
marq24 authored and marq24 committed Dec 13, 2023
1 parent ce0e085 commit 16bab0d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 122 deletions.
21 changes: 14 additions & 7 deletions custom_components/senec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from homeassistant.core import HomeAssistant, Event
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import EntityDescription, Entity
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers import entity_registry, event
Expand Down Expand Up @@ -51,6 +52,7 @@
CONF_SYSTYPE_INVERTER,
CONF_SYSTYPE_WEB,
CONF_DEV_MASTER_NUM,
CONF_IGNORE_SYSTEM_STATE,

MAIN_SENSOR_TYPES,
MAIN_BIN_SENSOR_TYPES,
Expand All @@ -59,6 +61,7 @@
QUERY_WALLBOX_KEY,
QUERY_SPARE_CAPACITY_KEY,
QUERY_PEAK_SHAVING_KEY,
IGNORE_SYSTEM_STATE_KEY,

SERVICE_SET_PEAKSHAVING,
)
Expand All @@ -85,9 +88,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
DEFAULT_SCAN_INTERVAL_SENECV2)))

_LOGGER.info("Starting " + str(config_entry.data.get(CONF_NAME)) + " with interval: " + str(SCAN_INTERVAL))
session = async_create_clientsession(hass)

coordinator = SenecDataUpdateCoordinator(hass, session, config_entry)
coordinator = SenecDataUpdateCoordinator(hass, config_entry)
await coordinator.async_refresh()
if not coordinator.last_update_success:
raise ConfigEntryNotReady
Expand Down Expand Up @@ -136,13 +138,13 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
class SenecDataUpdateCoordinator(DataUpdateCoordinator):
"""Define an object to hold Senec data."""

def __init__(self, hass: HomeAssistant, session, config_entry):
def __init__(self, hass: HomeAssistant, config_entry):
"""Initialize."""
# Build-In INVERTER
if CONF_TYPE in config_entry.data and config_entry.data[CONF_TYPE] == CONF_SYSTYPE_INVERTER:
# host can be changed in the options...
self._host = config_entry.options.get(CONF_HOST, config_entry.data[CONF_HOST])
self.senec = Inverter(self._host, websession=session)
self.senec = Inverter(self._host, web_session=async_get_clientsession(hass))

# WEB-API Version...
elif CONF_TYPE in config_entry.data and config_entry.data[CONF_TYPE] == CONF_SYSTYPE_WEB:
Expand Down Expand Up @@ -192,7 +194,7 @@ def __init__(self, hass: HomeAssistant, session, config_entry):
_LOGGER.info("***** QUERY_PEAK_SHAVING! ********")
opt[QUERY_PEAK_SHAVING_KEY] = True

self.senec = MySenecWebPortal(user=user, pwd=pwd, websession=session,
self.senec = MySenecWebPortal(user=user, pwd=pwd, web_session=async_create_clientsession(hass),
master_plant_number=a_master_plant_number,
options=opt)
# lala.cgi Version...
Expand All @@ -204,9 +206,14 @@ def __init__(self, hass: HomeAssistant, session, config_entry):
else:
self._use_https = False

opt = {
IGNORE_SYSTEM_STATE_KEY: config_entry.options.get(CONF_IGNORE_SYSTEM_STATE, False),
QUERY_WALLBOX_KEY: False,
QUERY_BMS_KEY: False,
QUERY_FANDATA_KEY: False
}
# check if any of the wallbox-sensors is enabled... and only THEN
# we will include the 'WALLBOX' in our POST to the lala.cgi
opt = {QUERY_WALLBOX_KEY: False, QUERY_BMS_KEY: False, QUERY_FANDATA_KEY: False}
if hass is not None and config_entry.title is not None:
registry = entity_registry.async_get(hass)
if registry is not None:
Expand Down Expand Up @@ -250,7 +257,7 @@ def __init__(self, hass: HomeAssistant, session, config_entry):
_LOGGER.info("***** QUERY_FANSPEED-DATA ********")
opt[QUERY_FANDATA_KEY] = True

self.senec = Senec(host=self._host, use_https=self._use_https, websession=session,
self.senec = Senec(host=self._host, use_https=self._use_https, web_session=async_get_clientsession(hass),
lang=hass.config.language.lower(), options=opt)

self.name = config_entry.title
Expand Down
22 changes: 11 additions & 11 deletions custom_components/senec/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
CONF_SYSTYPE_INVERTER,
CONF_SYSTYPE_WEB,
CONF_DEV_MASTER_NUM,
CONF_IGNORE_SYSTEM_STATE,
)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -111,9 +112,9 @@ def _host_in_configuration_exists(self, host) -> bool:
async def _test_connection_senec(self, host, use_https):
"""Check if we can connect to the Senec device."""
self._errors = {}
websession = self.hass.helpers.aiohttp_client.async_create_clientsession(auto_cleanup=False)
web_session = self.hass.helpers.aiohttp_client.async_get_clientsession()
try:
senec_client = Senec(host=host, use_https=use_https, websession=websession)
senec_client = Senec(host=host, use_https=use_https, web_session=web_session)
await senec_client.update_version()
self._use_https = use_https
self._device_type_internal = senec_client.device_type_internal
Expand All @@ -137,16 +138,14 @@ async def _test_connection_senec(self, host, use_https):
"Could not connect to SENEC.Home (using https? %s) at %s, check host ip address",
use_https, host,
)
finally:
websession.detach()
return False

async def _test_connection_inverter(self, host):
"""Check if we can connect to the Senec device."""
self._errors = {}
websession = self.hass.helpers.aiohttp_client.async_create_clientsession(auto_cleanup=False)
web_session = self.hass.helpers.aiohttp_client.async_get_clientsession()
try:
inverter_client = Inverter(host=host, websession=websession)
inverter_client = Inverter(host=host, web_session=web_session)
await inverter_client.update_version()
self._support_bdc = inverter_client.has_bdc

Expand All @@ -167,16 +166,14 @@ async def _test_connection_inverter(self, host):
"Could not connect to build-in Inverter device at %s, check host ip address",
host,
)
finally:
websession.detach()
return False

async def _test_connection_webapi(self, user: str, pwd: str, master_plant:int):
"""Check if we can connect to the Senec WEB."""
self._errors = {}
websession = self.hass.helpers.aiohttp_client.async_create_clientsession(auto_cleanup=False)
web_session = self.hass.helpers.aiohttp_client.async_create_clientsession(auto_cleanup=False)
try:
senec_web_client = MySenecWebPortal(user=user, pwd=pwd, websession=websession, master_plant_number=master_plant)
senec_web_client = MySenecWebPortal(user=user, pwd=pwd, web_session=web_session, master_plant_number=master_plant)
await senec_web_client.authenticate(do_update=False, throw401=True)
if senec_web_client._is_authenticated:
await senec_web_client.update_context()
Expand All @@ -199,7 +196,7 @@ async def _test_connection_webapi(self, user: str, pwd: str, master_plant:int):
self._errors[CONF_PASSWORD] = "login_failed"
_LOGGER.warning(f"Could not connect to mein-senec.de with '{user}', check credentials (exception)")
finally:
websession.detach()
web_session.detach()
return False

async def async_step_user(self, user_input=None):
Expand Down Expand Up @@ -489,6 +486,9 @@ async def async_step_system(self, user_input=None):
vol.Required(
CONF_SCAN_INTERVAL, default=self.options.get(CONF_SCAN_INTERVAL, self.data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL)),
): int, # pylint: disable=line-too-long
vol.Required(
CONF_IGNORE_SYSTEM_STATE, default=self.options.get(CONF_IGNORE_SYSTEM_STATE, self.data.get(CONF_IGNORE_SYSTEM_STATE, False)),
): bool, # pylint: disable=line-too-long
}
)
return self.async_show_form(
Expand Down
2 changes: 2 additions & 0 deletions custom_components/senec/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
CONF_USE_HTTPS: Final = "use_https"
CONF_SUPPORT_BDC: Final = "has_bdc_support"
CONF_DEV_MASTER_NUM: Final = "master_plant_number"
CONF_IGNORE_SYSTEM_STATE: Final = "ignore_system_state"

CONF_DEV_TYPE: Final = "dtype"
CONF_DEV_MODEL: Final = "dname"
Expand Down Expand Up @@ -95,6 +96,7 @@
QUERY_WALLBOX_KEY = "query_wallbox_data"
QUERY_SPARE_CAPACITY_KEY = "query_spare_capacity"
QUERY_PEAK_SHAVING_KEY = "query_peak_shaving"
IGNORE_SYSTEM_STATE_KEY = CONF_IGNORE_SYSTEM_STATE

# Peak Shaving Options
PEAK_SHAVING_OPTIONS = ["deactivated", "manual", "auto"]
Expand Down
Loading

0 comments on commit 16bab0d

Please sign in to comment.