Skip to content

Commit

Permalink
refactored service_naming
Browse files Browse the repository at this point in the history
  • Loading branch information
marq24 committed Jul 30, 2024
1 parent 00379c1 commit 1d3d43a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
48 changes: 25 additions & 23 deletions custom_components/goecharger_api2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,25 @@

SCAN_INTERVAL = timedelta(seconds=10)
CONFIG_SCHEMA = config_val.removed(DOMAIN, raise_if_present=False)
_SERVICE_SET_PV_DATA = SERVICE_SET_PV_DATA
_SERVICE_STOP_CHARGING = SERVICE_STOP_CHARGING

async def async_setup(hass: HomeAssistant, config: Config): # pylint: disable=unused-argument
"""Set up this integration using YAML is not supported."""
return True


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
global _SERVICE_SET_PV_DATA
global _SERVICE_STOP_CHARGING
def get_service_names(hass, config_entry):
all_integration_configs = hass.config_entries.async_entries(domain=DOMAIN,include_disabled=True, include_ignore=True)
if all_integration_configs is not None and len(all_integration_configs) > 1:
_SERVICE_SET_PV_DATA = f"{SERVICE_SET_PV_DATA}_{config_entry.data.get(CONF_ID)}"
_SERVICE_STOP_CHARGING = f"{SERVICE_STOP_CHARGING}_{config_entry.data.get(CONF_ID)}"
else:
_SERVICE_SET_PV_DATA = SERVICE_SET_PV_DATA
_SERVICE_STOP_CHARGING = SERVICE_STOP_CHARGING

return _SERVICE_SET_PV_DATA, _SERVICE_STOP_CHARGING


async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
if DOMAIN not in hass.data:
value = "UNKOWN"
_LOGGER.info(STARTUP_MESSAGE)
Expand All @@ -68,23 +75,11 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
config_entry.add_update_listener(async_reload_entry)

# initialize our service...
all_integration_configs = hass.config_entries.async_entries(domain=DOMAIN,include_disabled=True, include_ignore=True)
if all_integration_configs is not None:
_LOGGER.debug(f"{len(all_integration_configs)} -> {all_integration_configs}")
else:
_LOGGER.error(f"all_integration_configs is NONE - this could/should not be the case!")

if all_integration_configs is not None and len(all_integration_configs) > 1:
_SERVICE_SET_PV_DATA = f"{SERVICE_SET_PV_DATA}_{config_entry.data.get(CONF_ID)}"
_SERVICE_STOP_CHARGING = f"{SERVICE_STOP_CHARGING}_{config_entry.data.get(CONF_ID)}"
else:
_SERVICE_SET_PV_DATA = SERVICE_SET_PV_DATA
_SERVICE_STOP_CHARGING = SERVICE_STOP_CHARGING

service_names = get_service_names(hass, config_entry)
service = GoeChargerApiV2Service(hass, config_entry, coordinator)
hass.services.async_register(DOMAIN, _SERVICE_SET_PV_DATA, service.set_pv_data,
hass.services.async_register(DOMAIN, service_names[0], service.set_pv_data,
supports_response=SupportsResponse.OPTIONAL)
hass.services.async_register(DOMAIN, _SERVICE_STOP_CHARGING, service.stop_charging,
hass.services.async_register(DOMAIN, service_names[1], service.stop_charging,
supports_response=SupportsResponse.OPTIONAL)

if coordinator.check_for_max_of_16a:
Expand All @@ -98,13 +93,15 @@ async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
unload_ok = await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)

if unload_ok:
service_names = get_service_names(hass, config_entry)

if DOMAIN in hass.data and config_entry.entry_id in hass.data[DOMAIN]:
coordinator = hass.data[DOMAIN][config_entry.entry_id]
coordinator.clear_data()
hass.data[DOMAIN].pop(config_entry.entry_id)

hass.services.async_remove(DOMAIN, _SERVICE_SET_PV_DATA)
hass.services.async_remove(DOMAIN, _SERVICE_STOP_CHARGING)
hass.services.async_remove(DOMAIN, service_names[0])
hass.services.async_remove(DOMAIN, service_names[1])

return unload_ok

Expand Down Expand Up @@ -260,6 +257,11 @@ async def async_write_key(self, key: str, value, entity: Entity = None) -> dict:

async def read_versions(self):
await self.bridge.read_versions()
if Tag.FWV.key in self.bridge._versions:
sw_version = self.bridge._versions[Tag.FWV.key]
else:
sw_version = "UNKNOWN"

if self.mode == LAN:
self._device_info_dict = {
"identifiers": {(
Expand All @@ -270,7 +272,7 @@ async def read_versions(self):
"suggested_area": "Garage",
"name": self._config_entry.title,
"model": self._config_entry.data.get(CONF_TYPE),
"sw_version": self.bridge._versions[Tag.FWV.key]
"sw_version": sw_version
# hw_version
}
else:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/goecharger_api2/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/marq24/ha-goecharger-api2/issues",
"requirements": [],
"version": "2024.7.1"
"version": "2024.7.2"
}

0 comments on commit 1d3d43a

Please sign in to comment.