Skip to content

Commit

Permalink
Merge pull request #35 from rine77/25-bug-not-unique-id
Browse files Browse the repository at this point in the history
25 bug not unique
  • Loading branch information
rine77 authored Nov 25, 2024
2 parents 655533a + 4ab4083 commit 4e22a02
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
9 changes: 7 additions & 2 deletions custom_components/homeassistantedupage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ async def fetch_data():
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload ConfigEntry."""
_LOGGER.debug("INIT called async_unload_entry")
unload_ok = await hass.config_entries.async_forward_entry_unload(entry, ["calendar", "sensor"])

unload_calendar = await hass.config_entries.async_forward_entry_unload(entry, "calendar")
unload_sensor = await hass.config_entries.async_forward_entry_unload(entry, "sensor")

unload_ok = unload_calendar and unload_sensor

if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)

return unload_ok

27 changes: 21 additions & 6 deletions custom_components/homeassistantedupage/sensor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
Expand Down Expand Up @@ -47,12 +48,25 @@ class EduPageSubjectSensor(CoordinatorEntity, SensorEntity):
def __init__(self, coordinator, student_id, student_name, subject_name, grades=None):
"""Initialize the sensor."""
super().__init__(coordinator)
self._student_id = student_id
self._student_name = student_name
self._subject_name = subject_name
self._grades = grades or []
self._attr_name = f"{student_name} - {subject_name}"
self._attr_unique_id = f"edupage_grades_{student_id}_{subject_name.lower().replace(' ', '_')}"

clean_student_id = re.sub(r'[^a-z0-9]', '_', str(student_id).lower())
clean_student_name = re.sub(r'[^a-z0-9]', '_', student_name.lower())
clean_subject_name = re.sub(r'[^a-z0-9]', '_', subject_name.lower())

self._student_id = clean_student_id
self._student_name = clean_student_name
self._subject_name = clean_subject_name
self._grades = grades or []

self._attr_name = f"{clean_student_name} - {clean_subject_name}"
self._unique_id = f"edupage_subject_{clean_student_id}_{clean_subject_name}"

_LOGGER.info("SENSOR unique_id %s", self._unique_id)

@property
def unique_id(self):
"""Return a unique identifier for this sensor."""
return self._unique_id

@property
def state(self):
Expand All @@ -66,6 +80,7 @@ def extra_state_attributes(self):
return {"info": "no grades yet"}

attributes = {"student": self._student_name}
attributes = {"unique_id": self._unique_id}
for i, grade in enumerate(self._grades):
attributes[f"grade_{i+1}_title"] = grade.title
attributes[f"grade_{i+1}_grade_n"] = grade.grade_n
Expand Down

0 comments on commit 4e22a02

Please sign in to comment.