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

Fix storage handling #326

Merged
merged 2 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 62 additions & 10 deletions custom_components/proxmoxve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,13 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
CONF_LXC: config_entry.data.get(CONF_LXC),
}

config_entry.version = 2
hass.config_entries.async_update_entry(config_entry, data=data_new, options={})
hass.config_entries.async_update_entry(
config_entry,
data=data_new,
options={},
version=2,
minor_version=1,
)

LOGGER.debug("Migration - remove devices: %s", device_identifiers)
for device_identifier in device_identifiers:
Expand Down Expand Up @@ -235,9 +240,12 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
for resource in config_entry.data[CONF_LXC]:
device_identifiers.append(f"{ProxmoxType.LXC.upper()}_{resource}")

config_entry.version = 3
hass.config_entries.async_update_entry(
config_entry, data=config_entry.data, options={}
config_entry,
data=config_entry.data,
options={},
version=3,
minor_version=1,
)

LOGGER.debug("Migration - remove devices: %s", device_identifiers)
Expand All @@ -260,7 +268,6 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
)

if config_entry.version == 3:
config_entry.version = 4
data_new = {
CONF_HOST: config_entry.data.get(CONF_HOST),
CONF_PORT: config_entry.data.get(CONF_PORT),
Expand All @@ -273,7 +280,52 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
CONF_LXC: config_entry.data.get(CONF_LXC),
CONF_STORAGE: [],
}
hass.config_entries.async_update_entry(config_entry, data=data_new, options={})
hass.config_entries.async_update_entry(
config_entry,
data=data_new,
options={},
version=4,
minor_version=1,
)

if config_entry.version == 4:
for storage in config_entry.data.get(CONF_STORAGE):
dev_reg = dr.async_get(hass)
device = dev_reg.async_get_or_create(
config_entry_id=config_entry.entry_id,
identifiers={
(
DOMAIN,
(
f"{config_entry.entry_id}_{ProxmoxType.Storage.upper()}_{storage}"
),
)
},
)
dev_reg.async_update_device(
device_id=device.id,
remove_config_entry_id=config_entry.entry_id,
)

data_new = {
CONF_HOST: config_entry.data.get(CONF_HOST),
CONF_PORT: config_entry.data.get(CONF_PORT),
CONF_USERNAME: config_entry.data.get(CONF_USERNAME),
CONF_PASSWORD: config_entry.data.get(CONF_PASSWORD),
CONF_REALM: config_entry.data.get(CONF_REALM),
CONF_VERIFY_SSL: config_entry.data.get(CONF_VERIFY_SSL),
CONF_NODES: config_entry.data.get(CONF_NODES),
CONF_QEMU: config_entry.data.get(CONF_QEMU),
CONF_LXC: config_entry.data.get(CONF_LXC),
CONF_STORAGE: [],
}
hass.config_entries.async_update_entry(
config_entry,
data=data_new,
options={},
version=5,
minor_version=1,
)

LOGGER.info("Migration to version %s successful", config_entry.version)

Expand Down Expand Up @@ -488,7 +540,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b

for storage_id in config_entry.data[CONF_STORAGE]:
if storage_id in [
(resource.get("storage", None))
(resource.get("id", None))
for resource in (resources if resources is not None else [])
]:
ir.async_delete_issue(
Expand Down Expand Up @@ -519,7 +571,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
"port": config_entry.data[CONF_PORT],
"resource_type": ProxmoxType.Storage.capitalize(),
"resource": storage_id,
"permission": f"['perm','/storage/{storage_id}',['Datastore.Audit'],'any',1]",
"permission": f"['perm','{storage_id}',['Datastore.Audit'],'any',1]",
},
)

Expand Down Expand Up @@ -610,8 +662,8 @@ def device_info(
node = coordinator_data.node

name = cordinator_resource.name
identifier = f"{config_entry.entry_id}_{api_category.upper()}_{resource_id}"
url = f"https://{host}:{port}/#v1:0:={api_category}/{node}/{resource_id}"
identifier = f"{config_entry.entry_id}_{api_category.upper()}_{resource_id.replace("storage/", "")}"
url = f"https://{host}:{port}/#v1:0:={resource_id}"
via_device = (
DOMAIN,
f"{config_entry.entry_id}_{ProxmoxType.Node.upper()}_{node}",
Expand Down
10 changes: 2 additions & 8 deletions custom_components/proxmoxve/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,7 @@ async def async_step_change_expose(
resource_lxc[str(resource["vmid"])] = f"{resource['vmid']}"
if ("type" in resource) and (resource["type"] == ProxmoxType.Storage):
if "storage" in resource:
resource_storage[str(resource["storage"])] = (
f"{resource['storage']} {resource['id']}"
)
else:
resource_storage[str(resource["storage"])] = (
f"{resource['storage']}"
)
resource_storage[str(resource["id"])] = resource["id"]

return self.async_show_form(
step_id="change_expose",
Expand Down Expand Up @@ -469,7 +463,7 @@ async def async_process_selection_changes(
class ProxmoxVEConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""ProxmoxVE Config Flow class."""

VERSION = 4
VERSION = 5
_reauth_entry: config_entries.ConfigEntry | None = None

def __init__(self) -> None:
Expand Down
5 changes: 2 additions & 3 deletions custom_components/proxmoxve/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ async def _async_update_data(self) -> ProxmoxStorageData:

for resource in resources if resources is not None else []:
if "storage" in resource:
if resource["storage"] == self.resource_id:
if resource["id"] == self.resource_id:
node_name = resource["node"]

api_path = "cluster/resources?type=storage"
Expand All @@ -435,13 +435,12 @@ async def _async_update_data(self) -> ProxmoxStorageData:

api_status = []
for api_storage in api_storages:
if api_storage["storage"] == self.resource_id:
if api_storage["id"] == self.resource_id:
api_status = api_storage

if api_status is None or "content" not in api_status:
raise UpdateFailed(f"Storage {self.resource_id} unable to be found")

update_device_via(self, ProxmoxType.Storage, node_name)
storage_id = api_status["id"]
name = f"Storage {storage_id.replace("storage/", "")}"
return ProxmoxStorageData(
Expand Down
Loading