Skip to content

Commit

Permalink
added ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerbranda committed Mar 6, 2024
1 parent ab8de90 commit c969737
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 31 deletions.
70 changes: 42 additions & 28 deletions custom_components/rbfa/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def __get_url(self, operation, value):
DOMAIN,
"{}_invalid_config_{}_{}".format(DOMAIN, operation, value)
)
_LOGGER.debug(url)

elif rj['data'][REQUIRED[operation]] == None:
_LOGGER.debug('no results')
Expand Down Expand Up @@ -140,6 +141,11 @@ def __get_match(self):
response = self.__get_url('GetMatchDetail', self.match)
return response

def __get_ranking(self):
_LOGGER.debug(self.series)
response = self.__get_url('GetSeriesRankings', self.series)
return response

async def update(self):
_LOGGER.debug('Updating match details using Rest API')

Expand Down Expand Up @@ -169,45 +175,53 @@ async def update(self):

naive_dt = datetime.strptime(item['startTime'], '%Y-%m-%dT%H:%M:%S')
starttime = naive_dt.replace(tzinfo = ZoneInfo(TZ))
description = 'No match score'

if starttime >= now and self.upcoming == None:
self.upcoming = {
'uid': item['id'],
'date': starttime,
'location': location,
'hometeam': item['homeTeam']['name'],
'homelogo': item['homeTeam']['logo'],
'awayteam': item['awayTeam']['name'],
'awaylogo': item['awayTeam']['logo'],
}

result = 'No match score'
if item['outcome']['homeTeamGoals'] != None:
description = 'Goals: ' + str(item['outcome']['homeTeamGoals']) + ' - ' + str(item['outcome']['awayTeamGoals'])
result = 'Goals: ' + str(item['outcome']['homeTeamGoals']) + ' - ' + str(item['outcome']['awayTeamGoals'])
if item['outcome']['homeTeamPenaltiesScored'] != None:
description += '; Penalties: ' + str(item['outcome']['homeTeamPenaltiesScored']) + ' - ' + str(item['outcome']['awayTeamPenaltiesScored'])

if starttime < now:
self.lastmatch = {
'uid': item['id'],
'date': starttime,
'location': location,
'hometeam': item['homeTeam']['name'],
'homelogo': item['homeTeam']['logo'],
'awayteam': item['awayTeam']['name'],
'awaylogo': item['awayTeam']['logo'],
'description': description,
}
result += '; Penalties: ' + str(item['outcome']['homeTeamPenaltiesScored']) + ' - ' + str(item['outcome']['awayTeamPenaltiesScored'])

matchdata = {
'uid': item['id'],
'date': starttime,
'location': location,
'hometeam': item['homeTeam']['name'],
'homelogo': item['homeTeam']['logo'],
'awayteam': item['awayTeam']['name'],
'awaylogo': item['awayTeam']['logo'],
'series': item['series']['name'],
'seriesid': item['series']['id'],
'result': result,
'ranking': 0,
}

if starttime >= now and self.upcoming == None:
self.series = previous['seriesid']

self.upcoming = matchdata
self.lastmatch = previous

r = await self.hass.async_add_executor_job(self.__get_ranking)
for rank in r['data']['seriesRankings']['rankings'][0]['teams']:
if rank['teamId'] == self.team:
self.lastmatch['ranking'] = rank['position']


summary = '[' + item['state'] + '] ' + item['homeTeam']['name'] + ' - ' + item['awayTeam']['name']
# if item['state'] == 'postponed':
# summary = '[postponed] ' + summary

collection = {
'uid': item['id'],
'date': starttime,
'summary': item['homeTeam']['name'] + ' - ' + item['awayTeam']['name'],
'summary': summary,
'location': location,
'description': description
'description': item['series']['name'] + "; " + result,
}

self.collections.append(collection)
previous = matchdata


def get_rbfa_data_from_config(hass, config):
Expand Down
2 changes: 1 addition & 1 deletion custom_components/rbfa/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def event(self) -> Optional[CalendarEvent]:
start = team_items['date'],
end = team_items['date'] + timedelta(hours=1),
location = team_items['location'],
description = 'test',
description = team_items['series'],
)

async def async_get_events(
Expand Down
3 changes: 3 additions & 0 deletions custom_components/rbfa/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
'getClubInfo': 'clubId',
'GetUpcomingMatch': 'teamId',
'GetMatchDetail': 'matchId',
'GetSeriesRankings': 'seriesId',
}

HASHES = {
Expand All @@ -47,6 +48,7 @@
'getClubInfo': 'd7160a293f090e50f5ef52b1459530e9a11b36bd75c3ee1bcfbd9057889e009f',
'GetUpcomingMatch': '82e90ddafc6823e8cc5c5d876073e0e01c261f6844ad8e8bab5b8fd0b17da5e1',
'GetMatchDetail': '44adcb765b9b7159616dc339e33fcefa5b3aaadcc32a06cb6eece5855b1830c2',
'GetSeriesRankings': '7d13cbe2a17d6d5e7a3a0c1039d09c2e0ca326a454ec6fd2a471aa1fa2cf73e5',
}

REQUIRED = {
Expand All @@ -55,4 +57,5 @@
'getClubInfo': 'clubInfo',
'GetUpcomingMatch': 'upcomingMatch',
'GetMatchDetail': 'matchDetail',
'GetSeriesRankings': 'seriesRankings',
}
8 changes: 6 additions & 2 deletions custom_components/rbfa/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def update(self) -> None:
self._attr_name = f"{item['hometeam']} - {item['awayteam']}"
self._attr_native_value = item['date']
self._attr_extra_state_attributes = {
'test_attribute': 'test'
'Series': item['series']
}

class HomeSensor(SensorEntity):
Expand Down Expand Up @@ -145,4 +145,8 @@ def update(self) -> None:
"""Fetch new state data for the sensor."""
item = self.TeamData.lastmatch()
self._attr_name = f"{item['hometeam']} - {item['awayteam']}"
self._attr_native_value = item['description']
self._attr_native_value = item['result']
self._attr_extra_state_attributes = {
'Series': item['series'],
'Ranking': item['ranking']
}

0 comments on commit c969737

Please sign in to comment.