Skip to content

Commit

Permalink
Undo refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vasqued2 committed Nov 12, 2023
1 parent aef130f commit 8424c0f
Showing 1 changed file with 62 additions and 67 deletions.
129 changes: 62 additions & 67 deletions custom_components/teamtracker/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,93 +11,42 @@

_LOGGER = logging.getLogger(__name__)

async def async_process_event(values, sensor_name, data, sport_path, league_id, default_logo, team_id, lang) -> bool:
"""Loop through the JSON data returned by the API to find the right event and set values"""


async def async_process_event(
values, sensor_name, data, sport_path, league_id, default_logo, team_id, lang
) -> bool:
"""Loop throught the json data returned by the API to find the right event and set values"""

prev_values = {}

stop_flag = False
search_key = team_id
sport = sport_path

found_competitor = False

values["league_logo"] = await async_get_value(data, "leagues", 0, "logos", 0, "href", default=DEFAULT_LOGO)
values["league_logo"] = await async_get_value(
data, "leagues", 0, "logos", 0, "href", default=DEFAULT_LOGO
)

limit_hit = len(data["events"]) == API_LIMIT
first_date = datetime(9999, 12, 31, 1, 0, 0)
last_date = datetime(1900, 1, 31, 1, 0, 0)

for event in data["events"]:
event_state = "NOT_FOUND"
competition_index = -1
for competition_index, competition in enumerate(await async_get_value(event, "competitions", default=[])):
competition_date = datetime.strptime(await async_get_value(competition, "date", default=(await async_get_value(event, "date"))), "%Y-%m-%dT%H:%Mz")
first_date, last_date = min(competition_date, first_date), max(competition_date, last_date)

team_index = -1
for team_index, competitor in enumerate(await async_get_value(competition, "competitors", default=[])):
matched_index = await async_find_search_key(values, sensor_name, search_key, event, competition, competitor, team_index, sport_path)

if matched_index is not None:
found_competitor = True
prev_values = values.copy()
event_state = str(await async_get_value(event, "status", "type", "state", default="NOT_FOUND")).upper()
rc = await async_set_values(values, event, competition_index, matched_index, lang, sensor_name)

if not rc:
_LOGGER.debug("%s: event() Error occurred setting event values: %s", sensor_name, values)

if values["state"] == "IN" or (values["state"] == "PRE" and abs((arrow.get(values["date"]) - arrow.now()).total_seconds()) < 1200):
stop_flag = True

if stop_flag:
break

prev_flag = await async_use_prev_values_flag(prev_values, values, sensor_name, sport)

if prev_flag:
values = prev_values

if team_index == -1:
_LOGGER.debug("%s: async_process_event() No competitors in this competition: %s", sensor_name, str(await async_get_value(competition, "id", default="{id}")))

if stop_flag:
break

if values["state"] == "POST" and event_state == "IN":
stop_flag = True

if stop_flag:
break

if competition_index == -1:
_LOGGER.debug("%s: async_process_event() No competitions for this event: %s", sensor_name, await async_get_value(event, "shortName", default="{shortName}"))

if not found_competitor:
date_range_message = f"{first_date.strftime('%Y-%m-%dT%H:%MZ')} and {last_date.strftime('%Y-%m-%dT%H:%MZ')}"
values["api_message"] = f"API_LIMIT hit. No competition found for '{team_id}' between {date_range_message}" if limit_hit else f"No competition scheduled for '{team_id}' between {date_range_message}"
_LOGGER.debug("%s: {'API_LIMIT hit' if limit_hit else 'No competitor information'} '{search_key}' returned by API", sensor_name, search_key)

return values


async def async_process_competition(competition_parent, event,
values, sensor_name, data, sport_path, league_id, default_logo, team_id, lang
) -> bool:
"""Loop throught the json data returned by the API to find the right event and set values"""


# _LOGGER.debug("%s: event() Processing event: %s", sensor_name, str(await async_get_value(event, "shortName")))
competition_index = -1
for competition_index in range(
0, len(await async_get_value(competition_parent, "competitions", default=[]))
0, len(await async_get_value(event, "competitions", default=[]))
):
competition = await async_get_value(
competition_parent, "competitions", competition_index
event, "competitions", competition_index
)
team_index = -1

competition_date_str = await async_get_value(
competition, "date", default=(await async_get_value(competition_parent, "date"))
competition, "date", default=(await async_get_value(event, "date"))
)
competition_date = datetime.strptime(
competition_date_str, "%Y-%m-%dT%H:%Mz"
Expand All @@ -116,7 +65,7 @@ async def async_process_competition(competition_parent, event,
values,
sensor_name,
search_key,
competition_parent,
event,
competition,
competitor,
team_index,
Expand Down Expand Up @@ -174,6 +123,52 @@ async def async_process_competition(competition_parent, event,
)
if stop_flag:
break
#
# if the competition state is POST but the event state is IN, stop looking
# this happens in tennis where an event has many competitions
#
if values["state"] == "POST" and event_state == "IN":
stop_flag = True
if stop_flag:
break
if competition_index == -1:
_LOGGER.debug(
"%s: async_process_event() No competitions for this event: %s",
sensor_name,
await async_get_value(event, "shortName", default="{shortName}"),
)

if not found_competitor:
if limit_hit:
values["api_message"] = (
"API_LIMIT hit. No competition found for '"
+ team_id
+ "' between "
+ first_date.strftime("%Y-%m-%dT%H:%MZ")
+ " and "
+ last_date.strftime("%Y-%m-%dT%H:%MZ")
)
_LOGGER.debug(
"%s: API_LIMIT hit (%s). No competitor information '%s' returned by API",
sensor_name,
API_LIMIT,
search_key,
)
else:
values["api_message"] = (
"No competition scheduled for '"
+ team_id
+ "' between "
+ first_date.strftime("%Y-%m-%dT%H:%MZ")
+ " and "
+ last_date.strftime("%Y-%m-%dT%H:%MZ")
)
_LOGGER.debug(
"%s: No competitor information '%s' returned by API",
sensor_name,
search_key,
)

return values


Expand Down Expand Up @@ -299,4 +294,4 @@ async def async_use_prev_values_flag(prev_values, values, sensor_name, sport):
if time_diff < 64800:
return True

return False
return False

0 comments on commit 8424c0f

Please sign in to comment.