Skip to content

Commit 5954795

Browse files
committed
Refactor CEBL sensor transition logic for game state management
- Simplified transition rules to prioritize upcoming games if the most recent completed game is older than 1 day. - Improved logging for better clarity on game state transitions and fallback scenarios. - Removed outdated transition conditions to streamline the logic and enhance maintainability.
1 parent faec6b6 commit 5954795

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

custom_components/cebl/sensor.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def _get_team_fixture(self):
254254
_LOGGER.debug(f"Found {len(live_games)} live games, returning first")
255255
return live_games[0]
256256

257-
# Smart transition logic between completed and upcoming games
257+
# Simple transition logic: if completed game is >1 day old, move to next scheduled game
258258
if upcoming_games and completed_games:
259259
# Sort both lists
260260
upcoming_games.sort(key=lambda x: x[1] if x[1] else datetime.max.replace(tzinfo=pytz.UTC))
@@ -263,29 +263,21 @@ def _get_team_fixture(self):
263263
next_game = upcoming_games[0]
264264
recent_game = completed_games[0]
265265

266-
next_game_time = next_game[1]
267266
recent_game_time = recent_game[1]
268267

269-
if next_game_time and recent_game_time:
270-
time_until_next = (next_game_time - now).total_seconds()
268+
if recent_game_time:
271269
time_since_last = (now - recent_game_time).total_seconds()
272270

273-
# Transition rules:
274-
# 1. If next game is within 48 hours (172800 seconds), prioritize it
275-
# 2. If recent game ended more than 12 hours ago (43200 seconds) AND next game is within 7 days, show upcoming
276-
# 3. Otherwise show recent game for up to 12 hours
277-
278-
if time_until_next <= 172800: # Next game within 48 hours
279-
_LOGGER.debug(f"Next game within 48 hours ({time_until_next/3600:.1f}h), showing upcoming game")
280-
return next_game[0]
281-
elif time_since_last > 43200 and time_until_next <= 604800: # Recent game >12h old AND next game within 7 days
282-
_LOGGER.debug(f"Recent game >12h old ({time_since_last/3600:.1f}h), next game within 7 days ({time_until_next/86400:.1f}d), showing upcoming game")
271+
# Simple rule: If completed game is older than 1 day (86400 seconds), show upcoming game
272+
if time_since_last > 86400: # 1 day = 86400 seconds
273+
_LOGGER.debug(f"Recent game is {time_since_last/86400:.1f} days old - moving to next scheduled game")
283274
return next_game[0]
284275
else:
285-
_LOGGER.debug(f"Showing recent game (ended {time_since_last/3600:.1f}h ago, next game in {time_until_next/86400:.1f}d)")
276+
_LOGGER.debug(f"Recent game ended {time_since_last/3600:.1f} hours ago - still showing completed game")
286277
return recent_game[0]
287278
else:
288-
# Fallback to upcoming if times couldn't be parsed
279+
# Fallback to upcoming if recent game time couldn't be parsed
280+
_LOGGER.debug("Could not parse recent game time - showing upcoming game")
289281
return next_game[0]
290282

291283
if upcoming_games:

0 commit comments

Comments
 (0)