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

fixed #21 no teachers on grades #24

Merged
merged 1 commit into from
Nov 22, 2024
Merged
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
2 changes: 1 addition & 1 deletion custom_components/homeassistantedupage/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
31 changes: 20 additions & 11 deletions custom_components/homeassistantedupage/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"}

Expand All @@ -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
Loading