diff --git a/custom_components/homeassistantedupage/manifest.json b/custom_components/homeassistantedupage/manifest.json index 7ca50f3..df91a3e 100644 --- a/custom_components/homeassistantedupage/manifest.json +++ b/custom_components/homeassistantedupage/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/rine77/homeassistantedupage/issues", "requirements": ["edupage_api==0.11.0"], - "version": "0.1.0" + "version": "0.1.4" } \ No newline at end of file diff --git a/custom_components/homeassistantedupage/sensor.py b/custom_components/homeassistantedupage/sensor.py index 6334a1e..5cf3f19 100644 --- a/custom_components/homeassistantedupage/sensor.py +++ b/custom_components/homeassistantedupage/sensor.py @@ -9,27 +9,29 @@ _LOGGER = logging.getLogger("custom_components.homeassistant_edupage") + def group_grades_by_subject(grades): - """grouping grades based on subject_id.""" + """Group grades based on subject_id.""" grouped = defaultdict(list) for grade in grades: grouped[grade.subject_id].append(grade) return grouped + async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None: """Set up EduPage sensors based on subjects and their grades.""" _LOGGER.info("SENSOR called async_setup_entry") coordinator = hass.data[DOMAIN][entry.entry_id] subjects = coordinator.data.get("subjects", []) - grades = coordinator.data.get("grades", []) + grades = coordinator.data.get("grades", []) - # group grades based on subject_id + # Group grades based on subject_id grades_by_subject = group_grades_by_subject(grades) sensors = [] for subject in subjects: - # get grades per subject based on subject_id + # Get grades per subject based on subject_id subject_grades = grades_by_subject.get(subject.subject_id, []) sensor = EduPageSubjectSensor( coordinator, @@ -40,25 +42,26 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry, async_add_e async_add_entities(sensors, True) + class EduPageSubjectSensor(CoordinatorEntity, SensorEntity): - """subject sensor entity.""" + """Subject sensor entity.""" def __init__(self, coordinator, subject_name, grades=None): - """initializing""" + """Initialize the sensor.""" super().__init__(coordinator) self._subject_name = subject_name - self._grades = grades or [] + self._grades = grades or [] self._attr_name = f"EduPage subject - {subject_name}" self._attr_unique_id = f"edupage_grades_{subject_name.lower().replace(' ', '_')}" @property def state(self): - """return grade count""" + """Return grade count.""" return len(self._grades) @property def extra_state_attributes(self): - """return additional attributes""" + """Return additional attributes.""" if not self._grades: return {"info": "no grades yet"} @@ -67,6 +70,12 @@ def extra_state_attributes(self): attributes[f"grade_{i+1}_title"] = grade.title attributes[f"grade_{i+1}_grade_n"] = grade.grade_n attributes[f"grade_{i+1}_date"] = grade.date.strftime("%Y-%m-%d %H:%M:%S") - attributes[f"grade_{i+1}_teacher"] = grade.teacher.name - return attributes + # Check if teacher exists before accessing name + if grade.teacher: + attributes[f"grade_{i+1}_teacher"] = grade.teacher.name + else: + attributes[f"grade_{i+1}_teacher"] = None # Optional: Log warning + _LOGGER.warning(f"Teacher information missing for grade {i+1} in subject {self._subject_name}.") + + return attributes