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

Add unique id to emoncms integration #117911

24 changes: 8 additions & 16 deletions homeassistant/components/emoncms/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
)


def get_id(sensorid, feedtag, feedname, feedid, feeduserid):
def get_id(sensorid: str, feeduserid: str, feedid: str) -> str:
"""Return unique identifier for feed / sensor."""
return f"emoncms{sensorid}_{feedtag}_{feedname}_{feedid}_{feeduserid}"
return f"emoncms{sensorid}_{feeduserid}_{feedid}"


def setup_platform(
Expand Down Expand Up @@ -150,12 +150,11 @@ def __init__(
id_for_name = "" if str(sensorid) == "1" else sensorid
# Use the feed name assigned in EmonCMS or fall back to the feed ID
feed_name = elem.get("name") or f"Feed {elem['id']}"
self._name = f"EmonCMS{id_for_name} {feed_name}"
self._attr_name = f"EmonCMS{id_for_name} {feed_name}"
else:
self._name = name
self._identifier = get_id(
sensorid, elem["tag"], elem["name"], elem["id"], elem["userid"]
)
self._attr_name = name
self._attr_unique_id = get_id(sensorid, elem["userid"], elem["id"])
self._attr_native_unit_of_measurement = unit_of_measurement
self._hass = hass
self._data = data
self._value_template = value_template
Expand Down Expand Up @@ -197,11 +196,6 @@ def __init__(
else:
self._state = None

@property
def name(self):
"""Return the name of the sensor."""
return self._name

@property
def native_unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any."""
Expand Down Expand Up @@ -238,12 +232,10 @@ def update(self) -> None:
for elem in self._data.data
if get_id(
self._sensorid,
elem["tag"],
elem["name"],
elem["id"],
elem["userid"],
elem["id"],
)
== self._identifier
== self._attr_unique_id
),
None,
)
Expand Down