diff --git a/.storage/lovelace.dashboard_test b/.storage/lovelace.dashboard_test index cb4b0e8..5a72a87 100644 --- a/.storage/lovelace.dashboard_test +++ b/.storage/lovelace.dashboard_test @@ -8,53 +8,13 @@ { "title": "Home", "cards": [ - { - "type": "entities", - "entities": [ - { - "entity": "calendar.rbfa_281473" - }, - { - "type": "attribute", - "entity": "calendar.rbfa_281473", - "attribute": "message", - "name": "teams" - }, - { - "type": "attribute", - "entity": "calendar.rbfa_281473", - "attribute": "start_time", - "name": "start" - }, - { - "type": "attribute", - "entity": "calendar.rbfa_281473", - "attribute": "location", - "name": "location" - } - ] - }, - { - "type": "entities", - "entities": [ - "sensor.sun_next_dawn", - "sensor.sun_next_dusk", - "sensor.sun_next_midnight" - ] - }, - { - "type": "markdown", - "title": "Ranking", - "content": "{% set sensor = \"sensor.result_279669\" %}\n{{state_attr(sensor, \"Series\")}}\n-\n\n{% if state_attr(sensor, \"Ranking\") != None %} {% for item in state_attr(sensor, \"Ranking\") %}\n{{item.position}}. {% if item.id == state_attr(sensor, \"TeamID\") %}**{{item.team}}** {% else %}{{item.team}} {% endif %} {%- endfor %} {% endif %}" - }, { "type": "markdown", - "content": "[{{ states('sensor.home_team_279669') }} - {{ states('sensor.away_team_279669') }}](https://www.rbfa.be/nl/wedstrijd/{{ state_attr('sensor.datum_279669', 'MatchID') }}) {{ as_timestamp(states('sensor.datum_279669'))|timestamp_custom('%d-%m-%Y %H:%M') }}", - "title": "Link" + "content": "{% set sensor = \"sensor.reeks\" %} {{states(sensor)}}\n-\n{% if state_attr(sensor, \"ranking\") != None %} {% for item in state_attr(sensor, \"ranking\") %} {{item.position}}. {% if item.id == state_attr(sensor, \"baseid\") %}**{{item.team}}** \n{% else %}{{item.team}} \n{% endif %} {%- endfor %} {% endif %}" }, { "type": "markdown", - "content": "
{{as_timestamp(states('sensor.279669_next_match'))|timestamp_custom('%d-%m-%y')}}
{{states('sensor.home_team')}} {{states('sensor.rbfa_rbfa_away_team_279669')}}
" + "content": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
{{states('sensor.reeks')}}
\n{{as_timestamp(states('sensor.datum'))|timestamp_custom('%d-%m-%y om %H:%M')}}
{{states('sensor.thuis')}}{{states('sensor.uit')}}
Positie: {{state_attr('sensor.thuis','position')}}Positie: {{state_attr('sensor.uit', 'position')}}
{{states('sensor.locatie') | replace(\"\\n\",\"
\")}}
" } ] } diff --git a/custom_components/rbfa/API.py b/custom_components/rbfa/API.py index bc472ed..a3f3254 100644 --- a/custom_components/rbfa/API.py +++ b/custom_components/rbfa/API.py @@ -74,19 +74,6 @@ def __get_ranking(self): response = self.__get_url('GetSeriesRankings', self.series) return response - async def ranking_position(self, home, away): - result = {ranking: [], 'home': None, 'away': None} - r = await self.hass.async_add_executor_job(self.__get_ranking) - if r != None: - for rank in r['data']['seriesRankings']['rankings'][0]['teams']: - rankteam = {'position': rank['position'], 'team': rank['name'], 'id': rank['teamId']} - result['ranking'].append(rankteam) - if rank['teamId'] == home: - result['home'] = rank['position'] - if rank['teamId'] == away: - result['away'] = rank['position'] - return result - async def update(self): _LOGGER.debug('Updating match details using Rest API') @@ -103,7 +90,7 @@ async def update(self): previous = None self.collections = [] - ranking = None + ranking = [] for item in r['data']['teamCalendar']: self.match = item['id'] @@ -122,38 +109,58 @@ async def update(self): starttime = naive_dt.replace(tzinfo = ZoneInfo(TZ)) matchdata = { - 'uid': item['id'], + 'matchid': item['id'], 'team': self.team, 'teamname': self.teamdata['name'], 'clubname': self.teamdata['clubName'], 'date': starttime, 'location': location, 'hometeam': item['homeTeam']['name'], + 'hometeamid': item['homeTeam']['id'], 'hometeamlogo': item['homeTeam']['logo'], 'hometeamgoals': item['outcome']['homeTeamGoals'], 'hometeampenalties': item['outcome']['homeTeamPenaltiesScored'], 'hometeamposition': None, 'awayteam': item['awayTeam']['name'], + 'awayteamid': item['awayTeam']['id'], 'awayteamlogo': item['awayTeam']['logo'], 'awayteamgoals': item['outcome']['awayTeamGoals'], 'awayteampenalties': item['outcome']['awayTeamPenaltiesScored'], 'awayteamposition': None, 'series': item['series']['name'], 'seriesid': item['series']['id'], - 'ranking': ranking, + 'ranking': [], } if starttime + timedelta(hours=1) >= now and not upcoming: - self.series = item['series']['id'] - -# rankpos = self.ranking_position(item['homeTeam']['id'], item['awayTeam']['id']) -# matchdata['ranking'] = rankpos['ranking'] -# matchdata['hometeamposition'] = rankpos['home'] -# matchdata['awayteamposition'] = rankpos['away'] + self.series = matchdata['seriesid'] + r = await self.hass.async_add_executor_job(self.__get_ranking) + if r != None: + for rank in r['data']['seriesRankings']['rankings'][0]['teams']: + rankteam = {'position': rank['position'], 'team': rank['name'], 'id': rank['teamId']} + matchdata['ranking'].append(rankteam) + if rank['teamId'] == matchdata['hometeamid']: + matchdata['hometeamposition'] = rank['position'] + if rank['teamId'] == matchdata['awayteamid']: + matchdata['awayteamposition'] = rank['position'] + + self.series = previous['seriesid'] + r = await self.hass.async_add_executor_job(self.__get_ranking) + if r != None: + for rank in r['data']['seriesRankings']['rankings'][0]['teams']: + rankteam = {'position': rank['position'], 'team': rank['name'], 'id': rank['teamId']} + previous['ranking'].append(rankteam) + if rank['teamId'] == previous['hometeamid']: + previous['hometeamposition'] = rank['position'] + if rank['teamId'] == previous['awayteamid']: + previous['awayteamposition'] = rank['position'] upcoming = True - self.matchdata = {'upcoming': matchdata, 'lastmatch': previous} + self.matchdata = { + 'upcoming': matchdata, + 'lastmatch': previous + } summary = '[' + item['state'] + '] ' + item['homeTeam']['name'] + ' - ' + item['awayTeam']['name'] diff --git a/custom_components/rbfa/calendar.py b/custom_components/rbfa/calendar.py index a6dc593..e4b44cf 100644 --- a/custom_components/rbfa/calendar.py +++ b/custom_components/rbfa/calendar.py @@ -62,7 +62,7 @@ def event(self) -> Optional[CalendarEvent]: _LOGGER.debug('upcoming teamname: %r', upcoming['teamname']) self._attr_name = f"{upcoming['clubname']} | {upcoming['teamname']}" return CalendarEvent( - uid = upcoming['uid'], + uid = upcoming['matchid'], summary = upcoming['hometeam'] + ' - ' + upcoming['awayteam'], start = upcoming['date'], end = upcoming['date'] + timedelta(hours=1), @@ -72,7 +72,7 @@ def event(self) -> Optional[CalendarEvent]: elif lastmatch != None: self._attr_name = f"{lastmatch['clubname']} | {lastmatch['teamname']}" return CalendarEvent( - uid = lastmatch['uid'], + uid = lastmatch['matchid'], summary = lastmatch['hometeam'] + ' - ' + lastmatch['awayteam'], start = lastmatch['date'], end = lastmatch['date'] + timedelta(hours=1), @@ -97,7 +97,7 @@ async def async_get_events( # Summary below will define the name of event in calendar events.append( CalendarEvent( - uid = team_items['uid'], + uid = team_items['matchid'], summary = team_items['summary'], start = team_items['date'], end = team_items['date'] + timedelta(hours=1), diff --git a/custom_components/rbfa/sensor.py b/custom_components/rbfa/sensor.py index 13d5a6b..7cec70d 100644 --- a/custom_components/rbfa/sensor.py +++ b/custom_components/rbfa/sensor.py @@ -54,6 +54,10 @@ key="series", translation_key="series", ), + SensorEntityDescription( + key="matchid", + translation_key="matchid", + ), # SensorEntityDescription( # key="position", # translation_key="position", @@ -112,6 +116,7 @@ def __init__( ) -> None: super().__init__(coordinator) self.entity_description = description + self.collection = collection self.extra_attributes = {} self.TeamData = coordinator.matchdata().get(collection) @@ -120,18 +125,18 @@ def __init__( self._attr_unique_id = f"{DOMAIN}_{collection}_{description.key}_{self.team}" if self.TeamData != None: + self.extra_attributes = {} if description.key == 'hometeam' or description.key == 'awayteam': self._attr_entity_picture = self.TeamData[description.key + 'logo'] - self.extra_attributes = { - 'goals': self.TeamData[description.key + 'goals'], - 'penalties': self.TeamData[description.key + 'penalties'], - 'position': self.TeamData[description.key + 'position'], - } - if description.key == 'series': - self.extra_attributes = { - 'ranking': self.TeamData['ranking'], - } + results = ['id', 'goals', 'penalties', 'position'] + + for t in results: + if self.TeamData[description.key + t] != None: + self.extra_attributes[t] = self.TeamData[description.key + t] + + if description.key == 'series' and len(self.TeamData['ranking']) > 0: + self.extra_attributes ['ranking'] = self.TeamData['ranking'] @property def native_value(self): @@ -145,11 +150,13 @@ def extra_state_attributes(self): if self.TeamData != None: basic_attributes = { 'team': self.TeamData['teamname'], + 'baseid': self.team, 'date': self.TeamData['date'], + 'tag': self.collection, } if self.entity_description.key == 'position': self.extra_attributes = { 'ranking': self.TeamData['ranking'], } - return basic_attributes | self.extra_attributes \ No newline at end of file + return basic_attributes | self.extra_attributes diff --git a/custom_components/rbfa/translations/nl.json b/custom_components/rbfa/translations/nl.json index 22809ab..07b9943 100644 --- a/custom_components/rbfa/translations/nl.json +++ b/custom_components/rbfa/translations/nl.json @@ -22,51 +22,102 @@ "team":{ "name":"Elftal" }, + "date":{ + "name":"Datum" + }, + "id":{ + "name":"Team ID" + }, "goals":{ "name":"Doelpunten" }, "penalties":{ "name":"Strafschoppen" }, - "date":{ - "name":"Datum" + "position":{ + "name":"Positie" } } }, "awayteam":{ - "name":"Uit" + "name":"Uit", + "state_attributes":{ + "team":{ + "name":"Elftal" + }, + "goals":{ + "name":"Doelpunten" + }, + "penalties":{ + "name":"Strafschoppen" + }, + "date":{ + "name":"Datum" + }, + "id":{ + "name":"Team ID" + }, + "position":{ + "name":"Positie" + } + } }, "location":{ - "name":"Locatie" + "name":"Locatie", + "state_attributes":{ + "team":{ + "name":"Elftal" + }, + "date":{ + "name":"Datum" + }, + "id":{ + "name":"Team ID" + } + } }, "date":{ - "name":"Datum" - }, - "series":{ - "name":"Reeks", + "name":"Datum", "state_attributes":{ "team":{ "name":"Elftal" }, "date":{ "name":"Datum" + }, + "id":{ + "name":"Team ID" } } }, - "position":{ - "name":"Positie", + "matchid":{ + "name":"Wedstrijd ID", "state_attributes":{ - "ranking":{ - "name":"Rangschikking" + "team":{ + "name":"Elftal" + }, + "date":{ + "name":"Datum" + }, + "id":{ + "name":"Team ID" } } }, - "hometeamgoals":{ - "name":"Score thuis" - }, - "awayteamgoals":{ - "name":"Score uit" + "series":{ + "name":"Reeks", + "state_attributes":{ + "team":{ + "name":"Elftal" + }, + "date":{ + "name":"Datum" + }, + "id":{ + "name":"Team ID" + } + } } } } -} \ No newline at end of file +}