diff --git a/custom_components/calcio_live/config_flow.py b/custom_components/calcio_live/config_flow.py index a8ea038..cc25705 100755 --- a/custom_components/calcio_live/config_flow.py +++ b/custom_components/calcio_live/config_flow.py @@ -4,6 +4,7 @@ import logging import aiohttp from datetime import datetime, timedelta +from dateutil.relativedelta import relativedelta from .const import DOMAIN _LOGGER = logging.getLogger(__name__) @@ -246,8 +247,12 @@ async def async_step_init(self, user_input=None): if user_input is not None: return self.async_create_entry(title="", data=user_input) - start_date = self.config_entry.options.get("start_date", datetime.now().strftime("%Y-%m-%d")) - end_date = self.config_entry.options.get("end_date", (datetime.now() + timedelta(days=30)).strftime("%Y-%m-%d")) + today = datetime.now() + + start_date = self.config_entry.options.get("start_date", (today - relativedelta(months=3)).strftime("%Y-%m-%d")) + + end_date = self.config_entry.options.get("end_date", (today + relativedelta(months=4)).strftime("%Y-%m-%d")) + return self.async_show_form( step_id="init", diff --git a/custom_components/calcio_live/manifest.json b/custom_components/calcio_live/manifest.json index 3b9bb6c..7902728 100755 --- a/custom_components/calcio_live/manifest.json +++ b/custom_components/calcio_live/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "issue_tracker": "https://github.com/Bobsilvio/calcio_live/issues", "requirements": ["arrow", "aiofiles", "pytz==2023.3"], - "version": "2.1.6" + "version": "2.1.7" } diff --git a/custom_components/calcio_live/sensor.py b/custom_components/calcio_live/sensor.py index ef91406..a77e8b3 100755 --- a/custom_components/calcio_live/sensor.py +++ b/custom_components/calcio_live/sensor.py @@ -86,8 +86,12 @@ def __init__(self, hass, name, code, sensor_type=None, scan_interval=timedelta(m self._attributes = {} self._config_entry_id = config_entry_id self._team_name = team_name - self._start_date = datetime.strptime(start_date, "%Y-%m-%d") if start_date else datetime.now() - self._end_date = datetime.strptime(end_date, "%Y-%m-%d") if end_date else datetime.now() + timedelta(days=30) + today = datetime.now() + self._start_date = (today - relativedelta(months=3)).strftime("%Y-%m-%d") if not start_date else start_date + self._end_date = (today + relativedelta(months=4)).strftime("%Y-%m-%d") if not end_date else end_date + self._start_date = datetime.strptime(self._start_date, "%Y-%m-%d") + self._end_date = datetime.strptime(self._end_date, "%Y-%m-%d") + self._request_count = 0 self._last_request_time = None @@ -106,7 +110,7 @@ def extra_state_attributes(self): "request_count": self._request_count, "last_request_time": self._last_request_time, "start_date": self._start_date.strftime("%Y-%m-%d"), - "end_date": self._end_date.strftime("%Y-%m-%d") + "end_date": self._end_date.strftime("%Y-%m-%d"), } @property diff --git a/custom_components/calcio_live/sensori/classifica.py b/custom_components/calcio_live/sensori/classifica.py index d456b85..229b83d 100755 --- a/custom_components/calcio_live/sensori/classifica.py +++ b/custom_components/calcio_live/sensori/classifica.py @@ -3,57 +3,61 @@ def classifica_data(data): try: - standings_data = data.get("children", [])[0].get("standings", {}).get("entries", []) - standings = [] - - for index, entry in enumerate(standings_data, start=1): - team = entry.get("team", {}) - stats = {stat['name']: stat['displayValue'] for stat in entry.get("stats", [])} - - rank = entry.get("note", {}).get("rank", index) - - team_data = { - "rank": rank, - "team_id": team.get("id"), - "team_name": team.get("displayName"), - "team_logo": team.get("logos", [])[0].get("href"), - "points": stats.get("points", "N/A"), - "games_played": stats.get("gamesPlayed", "N/A"), - "wins": stats.get("wins", "N/A"), - "draws": stats.get("ties", "N/A"), - "losses": stats.get("losses", "N/A"), - "goals_for": stats.get("pointsFor", "N/A"), - "goals_against": stats.get("pointsAgainst", "N/A"), - "goal_difference": stats.get("pointDifferential", "N/A") - } - standings.append(team_data) + standings_list = [] + + for child in data.get("children", []): + standings_data = child.get("standings", {}).get("entries", []) + standings = [] + + for index, entry in enumerate(standings_data, start=1): + team = entry.get("team", {}) + stats = {stat['name']: stat['displayValue'] for stat in entry.get("stats", [])} + + rank = entry.get("note", {}).get("rank", index) + + team_data = { + "rank": rank, + "team_id": team.get("id"), + "team_name": team.get("displayName"), + "team_logo": team.get("logos", [])[0].get("href"), + "points": stats.get("points", "N/A"), + "games_played": stats.get("gamesPlayed", "N/A"), + "wins": stats.get("wins", "N/A"), + "draws": stats.get("ties", "N/A"), + "losses": stats.get("losses", "N/A"), + "goals_for": stats.get("pointsFor", "N/A"), + "goals_against": stats.get("pointsAgainst", "N/A"), + "goal_difference": stats.get("pointDifferential", "N/A") + } + standings.append(team_data) + + full_table_link = child.get("standings", {}).get("links", [])[0].get("href", "N/A") if child.get("standings", {}).get("links") else "N/A" + + standings_list.append({ + "name": child.get("name", "Unknown"), + "standings": standings, + "full_table_link": full_table_link + }) seasons_data = data.get("seasons", []) - current_season = None - for season in seasons_data: - if season.get("year") == 2024: - current_season = season - break + current_season = next((s for s in seasons_data if s.get("year") == 2024), None) season_display_name = current_season.get("displayName", "N/A") if current_season else "N/A" - season_start_raw = current_season.get("startDate", "N/A") - season_end_raw = current_season.get("endDate", "N/A") - - season_start = _parse_date(season_start_raw) - season_end = _parse_date(season_end_raw) + season_start = _parse_date(current_season.get("startDate", "N/A")) if current_season else None + season_end = _parse_date(current_season.get("endDate", "N/A")) if current_season else None return { - "standings": standings, "season": season_display_name, "season_start": season_start, "season_end": season_end, - "full_table_link": data.get("children", [])[0].get("standings", {}).get("links", [])[0].get("href", "N/A") + "standings_groups": standings_list # Mantiene le classifiche separate } except Exception as e: _LOGGER.error(f"Errore nel processare i dati della classifica: {e}") return {} + def _parse_date(date_str): try: parsed_date = parser.isoparse(date_str)