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 duplicate isue #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions custom_components/portainer/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
DEFAULT_SSL = False
DEFAULT_SSL_VERIFY = True

# attributes used in the entity id
DEVICE_ATTRIBUTES_CONTAINERS_UNIQUE = [
"Compose stack",
"Compose service",
"Environment",
]

TO_REDACT = {
"password",
}
Expand Down
23 changes: 19 additions & 4 deletions custom_components/portainer/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import slugify

from .const import ATTRIBUTION, DOMAIN, CUSTOM_ATTRIBUTE_ARRAY
from .const import (
ATTRIBUTION,
DOMAIN,
CUSTOM_ATTRIBUTE_ARRAY,
DEVICE_ATTRIBUTES_CONTAINERS_UNIQUE,
)
from .coordinator import PortainerCoordinator
from .helper import format_attribute
from .helper import format_attribute, format_camel_case

_LOGGER = getLogger(__name__)

Expand Down Expand Up @@ -50,7 +55,12 @@ async def async_check_exist(obj, coordinator, uid: None) -> None:
"""Check entity exists."""
entity_registry = er.async_get(hass)
if uid:
unique_id = f"{obj._inst.lower()}-{obj.description.key}-{slugify(str(obj._data[obj.description.data_reference]).lower())}"
slug = config_entry.entry_id
for key in DEVICE_ATTRIBUTES_CONTAINERS_UNIQUE:
if key in obj.extra_state_attributes:
slug = slug + " " + obj.extra_state_attributes[key]
slug = format_camel_case(slug).lower()
unique_id = f"{obj._inst.lower()}-{obj.description.key}-{slugify(slug)}"
else:
unique_id = f"{obj._inst.lower()}-{obj.description.key}"

Expand Down Expand Up @@ -130,7 +140,12 @@ def name(self) -> str:
def unique_id(self) -> str:
"""Return a unique id for this entity."""
if self._uid:
return f"{self._inst.lower()}-{self.description.key}-{slugify(str(self._data[self.description.data_reference]).lower())}"
slug = self.coordinator.config_entry.entry_id
for key in DEVICE_ATTRIBUTES_CONTAINERS_UNIQUE:
if key in self.extra_state_attributes:
slug = slug + " " + self.extra_state_attributes[key]
slug = format_camel_case(slug).lower()
return f"{self._inst.lower()}-{self.description.key}-{slugify(slug)}"
else:
return f"{self._inst.lower()}-{self.description.key}"

Expand Down
10 changes: 10 additions & 0 deletions custom_components/portainer/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def format_attribute(attr: str) -> str:
return res


# ---------------------------
# formatCamelCase
# ---------------------------
def format_camel_case(attr: str) -> str:
"""Format attribute."""
res = format_attribute(attr)
res = res.replace(" ", "")
return res


# ---------------------------
# as_local
# ---------------------------
Expand Down
Loading