Skip to content

Commit

Permalink
Created to_unique_id function to fix entities unique_id
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdelprete committed Feb 28, 2024
1 parent 858200f commit efe937a
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion custom_components/sinapsi_alfa/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import logging
import re
from typing import Any

from homeassistant.components.sensor import SensorEntity
Expand Down Expand Up @@ -52,6 +53,19 @@ async def async_setup_entry(hass: HomeAssistant, config_entry, async_add_entitie
return True


def to_unique_id(raw: str):
"""Convert string to unique_id."""
string = (
re.sub(r"(?<=[a-z0-9:_])(?=[A-Z])|[^a-zA-Z0-9:_]", " ", raw)
.strip()
.replace(" ", "_")
)
result = "".join(string.lower())
while "__" in result:
result = result.replace("__", "_")
return result


class SinapsiAlfaSensor(CoordinatorEntity, SensorEntity):
"""Representation of a Sinapsi Alfa sensor."""

Expand Down Expand Up @@ -141,7 +155,7 @@ def should_poll(self) -> bool:
@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return f"{DOMAIN}_{self._device_sn}_{self._key}"
return to_unique_id(f"{self._device_sn}_{self._key}")

@property
def device_info(self):
Expand Down

0 comments on commit efe937a

Please sign in to comment.