Skip to content

Commit

Permalink
- updated docu
Browse files Browse the repository at this point in the history
- deleting orphan DeviceRegistryEntries (#14)
  • Loading branch information
marq24 committed Aug 20, 2024
1 parent 4ccc3d9 commit 794eead
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Once you have enabled the automation, you also need to:

in the setting of your go-eCharger - this can be done via the integration!

__Please note: in order to be able to enable 'Use PV surplus' in the go-eCharger Application you must also configure the "Flexibler Energietarif" [specify "Preisgrenze", "Country", "Anbieter", "Tarif" and so on] even though the "Flexibler Energietarif" switch is "OFF"__ Probably a bug in the go-echarger software?

### Example automation

Please note that this example is for a for SENEC.Home System - if you are using 'my' SENEC.Home Integration you can use the code below 1:1 - in any other case: __You must adjust/replace the sensor identifiers!!!__. So if you are not a SENEC.Home user please replace the following:
Expand Down Expand Up @@ -137,7 +139,7 @@ action:

When you have more than one go-eCharger in your HA installation, you must provide an additional attribute `configid` in order to let the service know which charger should be used! This configid is the ConfigEntryId of the Integration for your multiple chargers and can look like this `01J4GR20JPFQ7M888Q4C9YAR31`.

The simples way to find the corresponding ConfigEntryId's of your multiple configured go-eCharger integrations is by using the GUI of the Service, activate the optional selection field, select the charger and then switch (from GUI) to YAML-Mode mode - this will show you the configid you must use.
The simples way to find the corresponding ConfigEntryId's of your multiple configured go-eCharger integrations is by using the GUI of the Service, activate the optional selection field, select the charger and then switch (from GUI) to YAML-Mode mode - this will show you the configid you must use. [[See this image for details](https://raw.githubusercontent.com/marq24/ha-goecharger-api2/main/res/configid.png)]

```
action:
Expand Down
44 changes: 35 additions & 9 deletions custom_components/goecharger_api2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from homeassistant.core import Config, Event, SupportsResponse
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as config_val, entity_registry as entity_reg
from homeassistant.helpers import config_validation as config_val, entity_registry as entity_reg, device_registry as device_reg
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import Entity, EntityDescription
from homeassistant.helpers.typing import UNDEFINED, UndefinedType
Expand Down Expand Up @@ -73,6 +73,8 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry):
if coordinator.check_for_max_of_16a:
asyncio.create_task(coordinator.check_for_16a_limit(hass, config_entry.entry_id))

asyncio.create_task(coordinator.cleanup_device_registry(hass))

# ok we are done...
return True

Expand Down Expand Up @@ -165,20 +167,37 @@ async def check_and_write_to_16a(hass: HomeAssistant, config_entry_id: str, brid
except Exception as e:
_LOGGER.error(f"Error while forcing 16A settings:", e)

@staticmethod
async def check_device_registry(hass: HomeAssistant):
_LOGGER.info(f"check device registry...")
if hass is not None:
a_device_reg = device_reg.async_get(hass)
if a_device_reg is not None:
key_list = []
for a_device_entry in list(a_device_reg.devices.values()):
if hasattr(a_device_entry, "identifiers"):
ident_value = a_device_entry.identifiers
if f"{ident_value}".__contains__(DOMAIN) and len(next(iter(ident_value))) != 4:
_LOGGER.debug(f"found a OLD {DOMAIN} DeviceEntry: {a_device_entry}")
key_list.append(a_device_entry.id)

if len(key_list) > 0:
_LOGGER.info(f"NEED TO DELETE old {DOMAIN} DeviceEntries: {key_list}")
for a_device_entry_id in key_list:
a_device_reg.async_remove_device(device_id=a_device_entry_id)

class GoeChargerDataUpdateCoordinator(DataUpdateCoordinator):
def __init__(self, hass: HomeAssistant, config_entry):
lang = hass.config.language.lower()
self.name = config_entry.title
if CONF_MODE in config_entry.data and config_entry.data.get(CONF_MODE) == WAN:
self.mode = WAN
self.bridge = GoeChargerApiV2Bridge(host=None,
serial=config_entry.options.get(CONF_ID,
config_entry.data.get(CONF_ID)),
token=config_entry.options.get(CONF_TOKEN,
config_entry.data.get(CONF_TOKEN)),
web_session=async_get_clientsession(hass),
lang=lang)
self.bridge = GoeChargerApiV2Bridge(
host=None,
serial=config_entry.options.get(CONF_ID, config_entry.data.get(CONF_ID)),
token=config_entry.options.get(CONF_TOKEN, config_entry.data.get(CONF_TOKEN)),
web_session=async_get_clientsession(hass),
lang=lang)
else:
self.mode = LAN
self.bridge = GoeChargerApiV2Bridge(
Expand Down Expand Up @@ -266,6 +285,7 @@ async def read_versions(self):
self._device_info_dict = {
"identifiers": {(
DOMAIN,
self._serial,
self._config_entry.data.get(CONF_HOST),
self._config_entry.title)},
"manufacturer": MANUFACTURER,
Expand All @@ -279,6 +299,7 @@ async def read_versions(self):
self._device_info_dict = {
"identifiers": {(
DOMAIN,
self._serial,
self._config_entry.data.get(CONF_TOKEN),
self._config_entry.title)},
"manufacturer": MANUFACTURER,
Expand Down Expand Up @@ -312,10 +333,15 @@ async def read_versions(self):
async def check_for_16a_limit(self, hass, entry_id):
_LOGGER.debug(f"check relevant entities for 16A limit... in 15sec")
await asyncio.sleep(15)

_LOGGER.debug(f"check relevant entities for 16A limit NOW!")
await check_and_write_to_16a(hass=hass, config_entry_id=entry_id, bridge=self.bridge)

async def cleanup_device_registry(self, hass: HomeAssistant):
_LOGGER.debug(f"check device registry for orphan {DOMAIN} entries... in 20sec")
await asyncio.sleep(20)
_LOGGER.debug(f"check device registry for orphan {DOMAIN} entries NOW!")
await check_device_registry(hass=hass)


class GoeChargerBaseEntity(Entity):
_attr_should_poll = False
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.8.0"
"version": "2024.8.1"
}
Binary file added res/configid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 794eead

Please sign in to comment.