Skip to content

Commit

Permalink
SOCKETS integration
Browse files Browse the repository at this point in the history
v3.1.5
  • Loading branch information
marq24 committed Dec 23, 2023
1 parent cf6861a commit e6397bf
Show file tree
Hide file tree
Showing 12 changed files with 1,290 additions and 439 deletions.
81 changes: 42 additions & 39 deletions custom_components/senec/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SENEC_SECTION_PM1OBJ2,
SENEC_SECTION_PV1,
SENEC_SECTION_PWR_UNIT,
SENEC_SECTION_SOCKETS,
SENEC_SECTION_TEMPMEASURE,
SENEC_SECTION_WALLBOX
)
Expand Down Expand Up @@ -56,9 +57,12 @@

MAIN_SENSOR_TYPES,
MAIN_BIN_SENSOR_TYPES,
MAIN_SWITCH_TYPES,
MAIN_NUMBER_SENSOR_TYPES,
QUERY_BMS_KEY,
QUERY_FANDATA_KEY,
QUERY_WALLBOX_KEY,
QUERY_SOCKETS_KEY,
QUERY_SPARE_CAPACITY_KEY,
QUERY_PEAK_SHAVING_KEY,
IGNORE_SYSTEM_STATE_KEY,
Expand Down Expand Up @@ -134,6 +138,31 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):

return True

def check_for_options(registry, sluged_title:str, opt:dict, sensor_type:str, entity_description_list:list) -> dict:
for description in entity_description_list:
if not opt[QUERY_WALLBOX_KEY] and SENEC_SECTION_WALLBOX == description.senec_lala_section:
a_sensor_id = f"{sensor_type}.{sluged_title}_{description.key}".lower()
a_entity = registry.async_get(a_sensor_id)
if a_entity is not None and a_entity.disabled_by is None:
_LOGGER.info("***** QUERY_WALLBOX-DATA ********")
opt[QUERY_WALLBOX_KEY] = True

if not opt[QUERY_FANDATA_KEY] and SENEC_SECTION_FAN_SPEED == description.senec_lala_section:
a_sensor_id = f"{sensor_type}.{sluged_title}_{description.key}".lower()
a_entity = registry.async_get(a_sensor_id)
if a_entity is not None and a_entity.disabled_by is None:
_LOGGER.info("***** QUERY_FANSPEED-DATA ********")
opt[QUERY_FANDATA_KEY] = True

if not opt[QUERY_SOCKETS_KEY] and SENEC_SECTION_SOCKETS == description.senec_lala_section:
a_sensor_id = f"{sensor_type}.{sluged_title}_{description.key}".lower()
a_entity = registry.async_get(a_sensor_id)
if a_entity is not None and a_entity.disabled_by is None:
_LOGGER.info("***** QUERY_SOCKETS-DATA ********")
opt[QUERY_SOCKETS_KEY] = True

return opt


class SenecDataUpdateCoordinator(DataUpdateCoordinator):
"""Define an object to hold Senec data."""
Expand Down Expand Up @@ -210,52 +239,19 @@ def __init__(self, hass: HomeAssistant, config_entry):
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
QUERY_FANDATA_KEY: False,
QUERY_SOCKETS_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
if hass is not None and config_entry.title is not None:
registry = entity_registry.async_get(hass)
if registry is not None:
sluged_title = slugify(config_entry.title)
for description in MAIN_SENSOR_TYPES:
if not opt[QUERY_WALLBOX_KEY] and SENEC_SECTION_WALLBOX == description.senec_lala_section:
a_sensor_id = f"sensor.{sluged_title}_{description.key}".lower()
a_entity = registry.async_get(a_sensor_id)
if a_entity is not None and a_entity.disabled_by is None:
_LOGGER.info("***** QUERY_WALLBOX-DATA ********")
opt[QUERY_WALLBOX_KEY] = True

if not opt[QUERY_BMS_KEY] and SENEC_SECTION_BMS == description.senec_lala_section:
a_sensor_id = f"sensor.{sluged_title}_{description.key}".lower()
a_entity = registry.async_get(a_sensor_id)
if a_entity is not None and a_entity.disabled_by is None:
_LOGGER.info("***** QUERY_BMS-DATA ********")
opt[QUERY_BMS_KEY] = True

# yes - currently only the 'MAIN_BIN_SENSOR's will contain the SENEC_SECTION_FAN_SPEED but
# I want to have here the complete code/overview 'what should be checked'
if not opt[QUERY_FANDATA_KEY] and SENEC_SECTION_FAN_SPEED == description.senec_lala_section:
a_sensor_id = f"sensor.{sluged_title}_{description.key}".lower()
a_entity = registry.async_get(a_sensor_id)
if a_entity is not None and a_entity.disabled_by is None:
_LOGGER.info("***** QUERY_FANSPEED-DATA ********")
opt[QUERY_FANDATA_KEY] = True

for description in MAIN_BIN_SENSOR_TYPES:
if not opt[QUERY_WALLBOX_KEY] and SENEC_SECTION_WALLBOX == description.senec_lala_section:
a_sensor_id = f"binary_sensor.{sluged_title}_{description.key}".lower()
a_entity = registry.async_get(a_sensor_id)
if a_entity is not None and a_entity.disabled_by is None:
_LOGGER.info("***** QUERY_WALLBOX-DATA ********")
opt[QUERY_WALLBOX_KEY] = True

if not opt[QUERY_FANDATA_KEY] and SENEC_SECTION_FAN_SPEED == description.senec_lala_section:
a_sensor_id = f"binary_sensor.{sluged_title}_{description.key}".lower()
a_entity = registry.async_get(a_sensor_id)
if a_entity is not None and a_entity.disabled_by is None:
_LOGGER.info("***** QUERY_FANSPEED-DATA ********")
opt[QUERY_FANDATA_KEY] = True
opt = check_for_options(registry, sluged_title, opt, "sensor", MAIN_SENSOR_TYPES)
opt = check_for_options(registry, sluged_title, opt, "binary_sensor", MAIN_BIN_SENSOR_TYPES)
opt = check_for_options(registry, sluged_title, opt, "switch", MAIN_SWITCH_TYPES)
opt = check_for_options(registry, sluged_title, opt, "number", MAIN_NUMBER_SENSOR_TYPES)

self.senec = Senec(host=self._host, use_https=self._use_https, web_session=async_get_clientsession(hass),
lang=hass.config.language.lower(), options=opt)
Expand Down Expand Up @@ -290,6 +286,13 @@ async def _async_switch_to_state(self, switch_key, state):
except UpdateFailed as exception:
raise UpdateFailed() from exception

async def _async_switch_array_to_state(self, switch_array_key, array_pos, state):
try:
await self.senec.switch_array(switch_array_key, array_pos, state)
return self.senec
except UpdateFailed as exception:
raise UpdateFailed() from exception


async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry):
"""Unload Senec config entry."""
Expand Down
5 changes: 4 additions & 1 deletion custom_components/senec/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ def is_on(self) -> bool | None:
"""Return true if the binary_sensor is on."""
# return self.coordinator.data.get("title", "") == "foo"
try:
value = getattr(self.coordinator.senec, self.entity_description.key)
if self.entity_description.array_key is not None:
value = getattr(self.coordinator.senec, self.entity_description.array_key)[self.entity_description.array_pos] == 1
else:
value = getattr(self.coordinator.senec, self.entity_description.key)
if value is None or value == "":
value = None
else:
Expand Down
Loading

0 comments on commit e6397bf

Please sign in to comment.