Skip to content

Commit

Permalink
adding sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerbranda committed Mar 14, 2024
1 parent 09e6bfe commit fcb3d7e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 96 deletions.
44 changes: 2 additions & 42 deletions .storage/lovelace.dashboard_test
Original file line number Diff line number Diff line change
Expand Up @@ -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": "<table width=\"100%\"> <tr><th colspan=2>{{as_timestamp(states('sensor.279669_next_match'))|timestamp_custom('%d-%m-%y')}}</th></tr> <tr> <td align=\"center\"><img src=\"{{state_attr('sensor.home_team','entity_picture')}}\" width=\"64\"></td> <td align=\"center\"><img src=\"{{state_attr('sensor.rbfa_rbfa_away_team_279669','entity_picture')}}\" width=\"64\"></td> </tr> <tr><td align=\"center\">{{states('sensor.home_team')}}</td> <td align=\"center\">{{states('sensor.rbfa_rbfa_away_team_279669')}}</td> </tr> </table>"
"content": "<table width=\"100%\">\n<tr>\n<th colspan=2>{{states('sensor.reeks')}}</th>\n</tr>\n<tr>\n<th colspan=2>\n<a href=\"https://www.rbfa.be/nl/wedstrijd/{{ states('sensor.rbfa_rbfa_upcoming_matchid_281445') }}\">{{as_timestamp(states('sensor.datum'))|timestamp_custom('%d-%m-%y om %H:%M')}}</a></th>\n</tr>\n<tr>\n<td align=\"center\"><img src=\"{{state_attr('sensor.thuis','entity_picture')}}\" width=\"64\"></td>\n<td align=\"center\"><img src=\"{{state_attr('sensor.uit','entity_picture')}}\" width=\"64\"></td>\n</tr>\n<tr>\n<td align=\"center\">{{states('sensor.thuis')}}</td>\n<td align=\"center\">{{states('sensor.uit')}}</td>\n</tr>\n<tr>\n<td align=\"center\">Positie: {{state_attr('sensor.thuis','position')}}</td>\n<td align=\"center\">Positie: {{state_attr('sensor.uit', 'position')}}</td>\n</tr>\n<tr>\n<td align=\"center\" colspan=\"2\">{{states('sensor.locatie') | replace(\"\\n\",\"<br>\")}}</td>\n</tr>\n</table>"
}
]
}
Expand Down
53 changes: 30 additions & 23 deletions custom_components/rbfa/API.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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']
Expand All @@ -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']
Expand Down
6 changes: 3 additions & 3 deletions custom_components/rbfa/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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),
Expand Down
27 changes: 17 additions & 10 deletions custom_components/rbfa/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
key="series",
translation_key="series",
),
SensorEntityDescription(
key="matchid",
translation_key="matchid",
),
# SensorEntityDescription(
# key="position",
# translation_key="position",
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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
return basic_attributes | self.extra_attributes
87 changes: 69 additions & 18 deletions custom_components/rbfa/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
}
}
}

0 comments on commit fcb3d7e

Please sign in to comment.