Skip to content

Commit

Permalink
issue#18
Browse files Browse the repository at this point in the history
  • Loading branch information
marq24 committed Sep 12, 2023
1 parent 95f5da7 commit 168a342
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 29 deletions.
35 changes: 25 additions & 10 deletions custom_components/senec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ async def async_setup(hass: HomeAssistant, config: dict):
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
"""Set up senec from a config entry."""
global SCAN_INTERVAL
SCAN_INTERVAL = timedelta(seconds=config_entry.data.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL_SENECV2))

# update_interval can be adjusted in the options (not for WebAPI)
SCAN_INTERVAL = timedelta(seconds=config_entry.options.get(CONF_SCAN_INTERVAL,
config_entry.data.get(CONF_SCAN_INTERVAL,
DEFAULT_SCAN_INTERVAL_SENECV2)))

session = async_get_clientsession(hass)

Expand All @@ -61,7 +65,8 @@ 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):
if CONF_TYPE not in config_entry.data or config_entry.data[CONF_TYPE] in (
CONF_SYSTYPE_SENEC, CONF_SYSTYPE_SENEC_V2):
coordinator._statistics_available = coordinator.senec.grid_total_export is not None

hass.data.setdefault(DOMAIN, {})
Expand All @@ -82,7 +87,8 @@ def __init__(self, hass, session, config_entry):

# Build-In INVERTER
if CONF_TYPE in config_entry.data and config_entry.data[CONF_TYPE] == CONF_SYSTYPE_INVERTER:
self._host = config_entry.data[CONF_HOST]
# 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)

# WEB-API Version...
Expand All @@ -93,11 +99,15 @@ def __init__(self, hass, session, config_entry):
if CONF_DEV_MASTER_NUM in config_entry.data:
a_master_plant_number = config_entry.data[CONF_DEV_MASTER_NUM]

self.senec = MySenecWebPortal(user=config_entry.data[CONF_USERNAME], pwd=config_entry.data[CONF_PASSWORD],
websession=session, master_plant_number=a_master_plant_number)
# user & pwd can be changed via the options...
user = config_entry.options.get(CONF_USERNAME, config_entry.data[CONF_USERNAME])
pwd = config_entry.options.get(CONF_PASSWORD, config_entry.data[CONF_PASSWORD])
self.senec = MySenecWebPortal(user=user, pwd=pwd, websession=session,
master_plant_number=a_master_plant_number)
# lala.cgi Version...
else:
self._host = config_entry.data[CONF_HOST]
# host can be changed in the options...
self._host = config_entry.options.get(CONF_HOST, config_entry.data[CONF_HOST])
if CONF_USE_HTTPS in config_entry.data:
self._use_https = config_entry.data[CONF_USE_HTTPS]
else:
Expand Down Expand Up @@ -163,16 +173,21 @@ def __init__(
def device_info(self) -> dict:
"""Return info for device registry."""
# Setup Device
dtype = self.coordinator._config_entry.options.get(CONF_DEV_TYPE, self.coordinator._config_entry.data.get(CONF_DEV_TYPE))
dserial = self.coordinator._config_entry.options.get(CONF_DEV_SERIAL, self.coordinator._config_entry.data.get(CONF_DEV_SERIAL))
dmodel = self.coordinator._config_entry.options.get(CONF_DEV_NAME, self.coordinator._config_entry.data.get(CONF_DEV_NAME))
dtype = self.coordinator._config_entry.options.get(CONF_DEV_TYPE,
self.coordinator._config_entry.data.get(CONF_DEV_TYPE))
dserial = self.coordinator._config_entry.options.get(CONF_DEV_SERIAL,
self.coordinator._config_entry.data.get(CONF_DEV_SERIAL))
dmodel = self.coordinator._config_entry.options.get(CONF_DEV_NAME,
self.coordinator._config_entry.data.get(CONF_DEV_NAME))
device = self._name
# "hw_version": self.coordinator._config_entry.options.get(CONF_DEV_NAME, self.coordinator._config_entry.data.get(CONF_DEV_NAME)),
return {
"identifiers": {(DOMAIN, self.coordinator._host, device)},
"name": f"{dtype}: {device}",
"model": f"{dmodel} [Serial: {dserial}]",
"sw_version": self.coordinator._config_entry.options.get(CONF_DEV_VERSION, self.coordinator._config_entry.data.get(CONF_DEV_VERSION)),
"sw_version": self.coordinator._config_entry.options.get(CONF_DEV_VERSION,
self.coordinator._config_entry.data.get(
CONF_DEV_VERSION)),
"manufacturer": MANUFACTURE,
}

Expand Down
30 changes: 15 additions & 15 deletions custom_components/senec/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,59 +429,59 @@ class SenecOptionsFlowHandler(config_entries.OptionsFlow):

def __init__(self, config_entry):
"""Initialize HACS options flow."""
self.config_entry = config_entry
self.data = dict(config_entry.data);
if len(dict(config_entry.options)) == 0:
self.options = dict(config_entry.data)
self.options = {}
else:
self.options = dict(config_entry.options)

async def async_step_init(self, user_input=None): # pylint: disable=unused-argument
"""Manage the options."""
if CONF_TYPE in self.options and self.options[CONF_TYPE] == CONF_SYSTYPE_WEB:
if CONF_TYPE in self.data and self.data[CONF_TYPE] == CONF_SYSTYPE_WEB:
return await self.async_step_websetup()
else:
return await self.async_step_user()
return await self.async_step_system()

async def async_step_user(self, user_input=None):
async def async_step_system(self, user_input=None):
"""Handle a flow initialized by the user."""
if user_input is not None:
self.options.update(user_input)
return await self._update_options()
return self._update_options()

dataSchema = vol.Schema(
{
vol.Required(
CONF_NAME, default=self.options.get(CONF_NAME, DEFAULT_NAME),
CONF_NAME, default=self.options.get(CONF_NAME, self.data.get(CONF_NAME, DEFAULT_NAME)),
): str,
vol.Required(
CONF_HOST, default=self.options.get(CONF_HOST, DEFAULT_HOST),
CONF_HOST, default=self.options.get(CONF_HOST, self.data.get(CONF_HOST, DEFAULT_HOST)),
): str, # pylint: disable=line-too-long
vol.Required(
CONF_SCAN_INTERVAL, default=self.options.get(CONF_SCAN_INTERVAL, DEFAULT_SCAN_INTERVAL),
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
}
)
return self.async_show_form(
step_id="user",
step_id="system",
data_schema=dataSchema,
)

async def async_step_websetup(self, user_input=None):
"""Handle a flow initialized by the user."""
if user_input is not None:
self.options.update(user_input)
return await self._update_options()
return self._update_options()

dataSchema = vol.Schema(
{
vol.Required(
CONF_NAME, default=self.options.get(CONF_NAME, DEFAULT_NAME_WEB)
CONF_NAME, default=self.options.get(CONF_NAME, self.data.get(CONF_NAME, DEFAULT_NAME_WEB))
): str,
vol.Required(
CONF_USERNAME, default=self.options.get(CONF_USERNAME, DEFAULT_USERNAME)
CONF_USERNAME, default=self.options.get(CONF_USERNAME, self.data.get(CONF_USERNAME, DEFAULT_USERNAME))
): str,
vol.Required(
CONF_PASSWORD, default=self.options.get(CONF_PASSWORD, "")
CONF_PASSWORD, default=self.options.get(CONF_PASSWORD, self.data.get(CONF_PASSWORD, ""))
): str
}
)
Expand All @@ -490,6 +490,6 @@ async def async_step_websetup(self, user_input=None):
data_schema=dataSchema,
)

async def _update_options(self):
def _update_options(self):
"""Update config entry options."""
return self.async_create_entry(data=self.options)
10 changes: 6 additions & 4 deletions custom_components/senec/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
"step": {
"user": {
"data": {
"system": "[%key:common::config_flow::data::system%]"
"stype": "[%key:common::config_flow::data::stype%]"
}
},
"mode": {
"system": {
"data": {
"mode": "[%key:common::config_flow::data::mode%]"
"name": "[%key:common::config_flow::data::name%]",
"host": "[%key:common::config_flow::data::host%]",
"scan_interval": "[%key:common::config_flow::data::scan_interval%]"
}
},
"system": {
"websetup": {
"data": {
"host": "[%key:common::config_flow::data::host%]",
"username": "[%key:common::config_flow::data::username%]",
Expand Down

0 comments on commit 168a342

Please sign in to comment.