scaarup / aula

Fetches information from Aula about your children
GNU General Public License v3.0
61 stars 17 forks source link

Fejl i skoleskema #175

Closed Fumper1 closed 2 months ago

Fumper1 commented 2 months ago

Discussed in https://github.com/scaarup/aula/discussions/174 Kom til at oprette den som discussion, men jeg mener det må være en bug

Originally posted by **Fumper1** August 31, 2024 **Jeg får denne fejl, på en af mine to børn i skolen** ``` Logger: homeassistant.helpers.entity Source: helpers/entity.py:942 First occurred: 12:09:45 AM (965 occurrences) Last logged: 4:23:48 PM Update for calendar.skoleskema_barn fails Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 942, in async_update_ha_state await self.async_device_update() File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1302, in async_device_update await hass.async_add_executor_job(self.update) File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/aula/calendar.py", line 62, in update self.data.update() File "/usr/src/homeassistant/homeassistant/util/__init__.py", line 184, in wrapper result = method(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^ File "/config/custom_components/aula/calendar.py", line 126, in update self.parseCalendarData(self) File "/config/custom_components/aula/calendar.py", line 112, in parseCalendarData summary=summary+", "+teacher, ~~~~~~~~~~~~^~~~~~~~ TypeError: can only concatenate str (not "NoneType") to str ``` **Jeg har fået det til at virke, men kan nogen, klogere end mig bekræfte at jeg har løst problemet ved at opdatere min calendary.py (med hjælp fra chatGPT) til dette:** ``` from datetime import datetime, timedelta import logging, time from .const import DOMAIN from homeassistant import config_entries, core from .const import CONF_SCHOOLSCHEDULE from homeassistant.components.calendar import ( CalendarEntity, CalendarEvent, ) from homeassistant.util import Throttle _LOGGER = logging.getLogger(__name__) MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=10) PARALLEL_UPDATES = 1 async def async_setup_entry( hass: core.HomeAssistant, config_entry: config_entries.ConfigEntry, async_add_entities, ): config = hass.data[DOMAIN][config_entry.entry_id] if config_entry.options: config.update(config_entry.options) from .client import Client if not config[CONF_SCHOOLSCHEDULE] == True: return True client = hass.data[DOMAIN]["client"] calendar_devices = [] calendar = [] for i, child in enumerate(client._children): childid = child["id"] name = child["name"] calendar_devices.append(CalendarDevice(hass,calendar,name,childid)) async_add_entities(calendar_devices) class CalendarDevice(CalendarEntity): def __init__(self, hass, calendar, name, childid): self.data = CalendarData(hass, calendar, childid) self._cal_data = {} self._name = "Skoleskema " + name self._childid = childid @property def event(self): """Return the next upcoming event.""" return self.data.event @property def name(self): """Return the name of the entity.""" return self._name @property def unique_id(self): unique_id = "aulacalendar" + str(self._childid) _LOGGER.debug("Unique ID for calendar " + str(self._childid) + " " + unique_id) return unique_id def update(self): """Update the calendar data.""" _LOGGER.debug("Updating calendar for " + self._name) self.data.event = self.data.parseCalendarData() async def async_get_events(self, hass, start_date, end_date): """Get all events in a specific time frame.""" return await self.data.async_get_events(hass, start_date, end_date) class CalendarData: def __init__(self, hass, calendar, childid): self.event = None self._hass = hass self._calendar = calendar self._childid = childid self.all_events = [] self._client = hass.data[DOMAIN]["client"] def parseCalendarData(self, i=None): import json try: with open('skoleskema.json', 'r') as openfile: _data = json.load(openfile) data = json.loads(_data) except: _LOGGER.warning("Could not open and parse file skoleskema.json!") return False events = [] _LOGGER.debug("Parsing skoleskema.json...") for c in data['data']: if c['type'] == "lesson" and c['belongsToProfiles'][0] == self._childid: summary = c['title'] start = datetime.strptime(c['startDateTime'], "%Y-%m-%dT%H:%M:%S%z") end = datetime.strptime(c['endDateTime'], "%Y-%m-%dT%H:%M:%S%z") vikar = 0 teacher = "" # Initialize teacher as an empty string to handle None case for p in c['lesson']['participants']: if p['participantRole'] == 'substituteTeacher': teacher = "VIKAR: " + (p['teacherName'] or "") vikar = 1 break if vikar == 0: try: teacher = c['lesson']['participants'][0].get('teacherInitials', "") except: try: _LOGGER.debug("Lesson json dump: " + str(c['lesson'])) teacher = c['lesson']['participants'][0].get('teacherName', "") except: _LOGGER.debug("Could not find any teacher information for " + summary + " at " + str(start)) summary = summary + (", " + teacher if teacher else "") event = CalendarEvent( summary=summary, start=start, end=end, ) events.append(event) return events async def async_get_events(self, hass, start_date, end_date): events = self.parseCalendarData() return events @Throttle(MIN_TIME_BETWEEN_UPDATES) def update(self): _LOGGER.debug("Updating calendars...") self.event = self.parseCalendarData() ```
scaarup commented 2 months ago

@Fumper1 vil du teste pre-release v0.1.42? Jeg har blot konverteret de to (summary og teacher) til strings. Hvis en af disse er None, burde det ikke fejle, men dog kommer der så en tom string i skemaet. Se hvad der sker og åbn bare et nyt issue hvis der er problemer.

Fumper1 commented 2 months ago

Ej undskyld at jeg er dum, men hvordan tester jeg pre-release?

scaarup commented 2 months ago

@Fumper1, hop ind i HACS, find Aula og vælg "Redownload":

image

Via den næste pop-up får du mulighed for at vælge den version du vil have.