Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prework for multiple chargers per cs #1480

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
116 changes: 108 additions & 8 deletions custom_components/ocpp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,47 @@
from .const import (
CONF_AUTH_LIST,
CONF_AUTH_STATUS,
CONF_CPID,
CONF_CSID,
CONF_CPIDS,
CONF_DEFAULT_AUTH_STATUS,
CONF_ID_TAG,
CONF_NAME,
CONF_CPID,
CONF_IDLE_INTERVAL,
CONF_MAX_CURRENT,
CONF_METER_INTERVAL,
CONF_MONITORED_VARIABLES,
CONF_MONITORED_VARIABLES_AUTOCONFIG,
CONF_SKIP_SCHEMA_VALIDATION,
CONF_FORCE_SMART_CHARGING,
CONF_HOST,
CONF_PORT,
CONF_CSID,
CONF_SSL,
CONF_SSL_CERTFILE_PATH,
CONF_SSL_KEYFILE_PATH,
CONF_WEBSOCKET_CLOSE_TIMEOUT,
CONF_WEBSOCKET_PING_TRIES,
CONF_WEBSOCKET_PING_INTERVAL,
CONF_WEBSOCKET_PING_TIMEOUT,
CONFIG,
DEFAULT_CPID,
DEFAULT_IDLE_INTERVAL,
DEFAULT_MAX_CURRENT,
DEFAULT_METER_INTERVAL,
DEFAULT_MONITORED_VARIABLES,
DEFAULT_MONITORED_VARIABLES_AUTOCONFIG,
DEFAULT_SKIP_SCHEMA_VALIDATION,
DEFAULT_FORCE_SMART_CHARGING,
DEFAULT_HOST,
DEFAULT_PORT,
DEFAULT_CSID,
DEFAULT_SSL,
DEFAULT_SSL_CERTFILE_PATH,
DEFAULT_SSL_KEYFILE_PATH,
DEFAULT_WEBSOCKET_CLOSE_TIMEOUT,
DEFAULT_WEBSOCKET_PING_TRIES,
DEFAULT_WEBSOCKET_PING_INTERVAL,
DEFAULT_WEBSOCKET_PING_TIMEOUT,
DOMAIN,
PLATFORMS,
)
Expand Down Expand Up @@ -75,18 +108,19 @@
""" Create Central System Device """
dr.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, entry.data.get(CONF_CSID, DEFAULT_CSID))},
name=entry.data.get(CONF_CSID, DEFAULT_CSID),
identifiers={(DOMAIN, central_sys.id)},
name=central_sys.id,
model="OCPP Central System",
)

""" Create Charge Point Device """
""" Create first Charge Point Device """
cpid = list(entry.data[CONF_CPIDS][0].keys())[0]
dr.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, entry.data.get(CONF_CPID, DEFAULT_CPID))},
name=entry.data.get(CONF_CPID, DEFAULT_CPID),
identifiers={(DOMAIN, cpid)},
name=cpid,
model="Unknown",
via_device=(DOMAIN, entry.data.get(CONF_CSID, DEFAULT_CSID)),
via_device=(DOMAIN, central_sys.id),
)

hass.data[DOMAIN][entry.entry_id] = central_sys
Expand All @@ -97,17 +131,83 @@
return True


async def async_migrate_entry(hass, config_entry: ConfigEntry):
"""Migrate old entry."""
_LOGGER.debug(
"Migrating configuration from version %s.%s",
config_entry.version,
config_entry.minor_version,
)

if config_entry.version > 1:
# This means the user has downgraded from a future version
return False

Check warning on line 144 in custom_components/ocpp/__init__.py

View check run for this annotation

Codecov / codecov/patch

custom_components/ocpp/__init__.py#L144

Added line #L144 was not covered by tests

if config_entry.version == 1:
old_data = {**config_entry.data}
csid_data = {}
cpid_data = {}
cpid_keys = {
CONF_CPID: DEFAULT_CPID,
CONF_IDLE_INTERVAL: DEFAULT_IDLE_INTERVAL,
CONF_MAX_CURRENT: DEFAULT_MAX_CURRENT,
CONF_METER_INTERVAL: DEFAULT_METER_INTERVAL,
CONF_MONITORED_VARIABLES: DEFAULT_MONITORED_VARIABLES,
CONF_MONITORED_VARIABLES_AUTOCONFIG: DEFAULT_MONITORED_VARIABLES_AUTOCONFIG,
CONF_SKIP_SCHEMA_VALIDATION: DEFAULT_SKIP_SCHEMA_VALIDATION,
CONF_FORCE_SMART_CHARGING: DEFAULT_FORCE_SMART_CHARGING,
}
csid_keys = {
CONF_HOST: DEFAULT_HOST,
CONF_PORT: DEFAULT_PORT,
CONF_CSID: DEFAULT_CSID,
CONF_SSL: DEFAULT_SSL,
CONF_SSL_CERTFILE_PATH: DEFAULT_SSL_CERTFILE_PATH,
CONF_SSL_KEYFILE_PATH: DEFAULT_SSL_KEYFILE_PATH,
CONF_WEBSOCKET_CLOSE_TIMEOUT: DEFAULT_WEBSOCKET_CLOSE_TIMEOUT,
CONF_WEBSOCKET_PING_TRIES: DEFAULT_WEBSOCKET_PING_TRIES,
CONF_WEBSOCKET_PING_INTERVAL: DEFAULT_WEBSOCKET_PING_INTERVAL,
CONF_WEBSOCKET_PING_TIMEOUT: DEFAULT_WEBSOCKET_PING_TIMEOUT,
}
for key, value in cpid_keys.items():
cpid_data.update({key: old_data.get(key, value)})

for key, value in csid_keys.items():
csid_data.update({key: old_data.get(key, value)})

new_data = csid_data
new_data.update({CONF_CPIDS: [{cpid_data[CONF_CPID]: cpid_data}]})

hass.config_entries.async_update_entry(
config_entry, data=new_data, minor_version=0, version=2
)

_LOGGER.debug(
"Migration to configuration version %s.%s successful",
config_entry.version,
config_entry.minor_version,
)

return True

Comment on lines +134 to +192
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Review and test the migration logic thoroughly

The async_migrate_entry function is critical for migrating configuration entries from version 1 to version 2. Ensure that all possible scenarios are handled, including edge cases where users might downgrade or have incomplete data.

Consider adding comprehensive unit tests to cover the migration process. This will help identify any potential issues and ensure a smooth transition for users.

Would you like assistance in creating tests for the migration logic?

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 144-144: custom_components/ocpp/init.py#L144
Added line #L144 was not covered by tests


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Handle removal of an entry."""
unloaded = False
if DOMAIN in hass.data:
if entry.entry_id in hass.data[DOMAIN]:
# Close server
central_sys = hass.data[DOMAIN][entry.entry_id]
central_sys._server.close()
await central_sys._server.wait_closed()
# Unload services
for service in hass.services.async_services_for_domain(DOMAIN):
hass.services.async_remove(DOMAIN, service)
# Unload platforms
unloaded = await hass.config_entries.async_unload_platforms(
entry, PLATFORMS
)
# Remove entry
if unloaded:
hass.data[DOMAIN].pop(entry.entry_id)

Expand Down
Loading
Loading