diff --git a/src/get_data.py b/src/get_data.py new file mode 100644 index 0000000..6ba3c55 --- /dev/null +++ b/src/get_data.py @@ -0,0 +1,81 @@ +import logging +from datetime import datetime +from bs4 import BeautifulSoup + +_LOGGER = logging.getLogger(__name__) + +MONTH_MAP = { + "jan": "Jan", + "feb": "Feb", + "mar": "Mar", + "apr": "Apr", + "mai": "May", + "jun": "Jun", + "jul": "Jul", + "aug": "Aug", + "sep": "Sep", + "okt": "Oct", + "nov": "Nov", + "des": "Dec", +} + +async def get_dates(session, url): + _LOGGER.debug(f"Attempting to fetch data from URL: {url}") + + async with session.get(url) as response: + if response.status != 200: + _LOGGER.error(f"Failed to get content from URL: {url}. HTTP Status Code: {response.status}") + return "Failed to get content" + + html = await response.text() + soup = BeautifulSoup(html, 'html.parser') + + data = {} + target_items = soup.select('.address-page-box__list__item') + + _LOGGER.debug(f"Found {len(target_items)} target items.") + + for item in target_items: + text_content_elem = item.select_one('.text-content__inner') + date_month_elem = item.select_one('.date__month') + + if text_content_elem and date_month_elem: + text_content = text_content_elem.get_text(strip=True) + date_month = date_month_elem.get_text(strip=True) + + date_parts = date_month.split(". ") + if len(date_parts) < 2: + _LOGGER.warning("Could not split date into day and month") + continue + + date_day = date_parts[0].strip() + date_month = date_parts[1].strip() + + # Translate month to English abbreviation + date_month = MONTH_MAP.get(date_month.lower(), date_month) + + date, err = parseToDate(date_day, date_month) + if err: + _LOGGER.error(f"Failed to parse date. Error: {err}") + continue + + formatted_date = date.strftime("%Y-%m-%d") + + if "Restavfall" in text_content: + data['mixed_waste'] = formatted_date + elif "Papir og plastemballasje" in text_content: + data['paper_and_plastic_waste'] = formatted_date + elif "Matavfall" in text_content: + data['food_waste'] = formatted_date + + _LOGGER.debug(f"Collected data: {data}") + + return data + +def parseToDate(date_day, date_month): + try: + date_str = f"{date_day} {date_month} 2023" + return datetime.strptime(date_str, '%d %b %Y'), None + except Exception as e: + _LOGGER.error(f"Exception while parsing date: {str(e)}") + return None, str(e) diff --git a/src/sensor.py b/src/sensor.py index f5c2ba9..5304280 100644 --- a/src/sensor.py +++ b/src/sensor.py @@ -1,76 +1,29 @@ -import logging from datetime import timedelta, datetime import aiohttp -from bs4 import BeautifulSoup from homeassistant.components.sensor import SensorEntity from homeassistant.helpers.event import async_track_time_interval - +import logging +from .get_data import get_dates _LOGGER = logging.getLogger(__name__) -async def get_dates(session, url): - async with session.get(url) as response: - if response.status != 200: - return "Failed to get content" - - html = await response.text() - soup = BeautifulSoup(html, 'html.parser') - - data = {} - - target_items = soup.select('.address-page-box__list__item') - - for item in target_items: - text_content_elem = item.select_one('.text-content__inner') - date_month_elem = item.select_one('.date__month') - - if text_content_elem and date_month_elem: - text_content = text_content_elem.get_text(strip=True) - date_month = date_month_elem.get_text(strip=True) - - date_parts = date_month.split(". ") - if len(date_parts) < 2: - continue - - date_day = date_parts[0].strip() - date_month = date_parts[1].strip() - - date, err = parseToDate(date_day, date_month) - if err: - continue - - formatted_date = date.strftime("%Y-%m-%d") - - if "Restavfall" in text_content: - data['mixed_waste'] = formatted_date - elif "Papir og plastemballasje" in text_content: - data['paper_and_plastic_waste'] = formatted_date - elif "Matavfall" in text_content: - data['food_waste'] = formatted_date - - return data - -def parseToDate(date_day, date_month): - try: - date_str = f"{date_day} {date_month} 2023" - return datetime.strptime(date_str, '%d %b %Y'), None - except Exception as e: - return None, str(e) - async def async_setup_entry(hass, config_entry, async_add_entities): url = config_entry.data.get("url") - update_interval = config_entry.data.get("update_interval", 4) # Default to 4 if not found + update_interval = config_entry.data.get("update_interval", 4) session = aiohttp.ClientSession() data = await get_dates(session, url) + _LOGGER.debug(f"Data retrieved from get_dates function: {data}") # Debug log to check data + if data: sensors = [] for waste_type, date in data.items(): sensor = WasteCollectionSensor(session, url, waste_type, date, config_entry.entry_id) sensors.append(sensor) - # Set up the update interval for each sensor async_track_time_interval(hass, sensor.async_update, timedelta(hours=int(update_interval))) + + _LOGGER.debug(f"Sensors to add: {sensors}") # Debug log to check sensors if sensors: async_add_entities(sensors, True)