Skip to content

Commit

Permalink
Nuovo sensore per aggiungere il team id
Browse files Browse the repository at this point in the history
#Team id
Andate sul sito https://www.football-data.org/coverage
Selezionate il campionato e poi click sulla vostra squadra, in alto compare un numero, è il vostro team_id
  • Loading branch information
Bobsilvio committed Sep 10, 2024
1 parent 423fd38 commit 7405753
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions custom_components/calcio_live/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import aiohttp
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity import DeviceInfo

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -35,6 +36,22 @@ def __init__(self, hass, name, api_key, competition_code, sensor_type):
self._sensor_type = sensor_type
self._state = None
self._attributes = {}
self._unique_id = f"{competition_code}_{sensor_type}"

@property
def unique_id(self):
"""Return a unique ID for this sensor."""
return self._unique_id

@property
def device_info(self):
"""Return device info for this entity."""
return DeviceInfo(
identifiers={(DOMAIN, self._competition_code)},
name=f"{self._competition_code} Sensor",
manufacturer="CalcioLive",
model="Football Data API",
)

@property
def name(self):
Expand All @@ -49,24 +66,37 @@ def extra_state_attributes(self):
return self._attributes

async def async_update(self):
url = await self._build_url() # Usa 'await' per chiamare la funzione asincrona _build_url
url = await self._build_url()
headers = {"X-Auth-Token": self._api_key}

if url is None:
_LOGGER.error(f"URL is None for {self._name}")
self._state = "Errore URL"
return

try:
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
if response.status != 200:
_LOGGER.error(f"Errore HTTP {response.status} per {self._name}")
self._state = f"Errore HTTP {response.status}"
return

data = await response.json()

# Controllo se i dati sono validi
if data is None:
_LOGGER.error(f"Nessun dato ricevuto per {self._name}")
self._state = "Dati non disponibili"
return

# Elabora i dati solo se sono validi
self._process_data(data)

except aiohttp.ClientError as error:
_LOGGER.error(f"Error fetching data for {self._name}: {error}")
self._state = None
_LOGGER.error(f"Errore nel recupero dei dati per {self._name}: {error}")
self._state = "Errore di connessione"


async def _build_url(self):
base_url = "https://api.football-data.org/v4/competitions"
Expand Down

0 comments on commit 7405753

Please sign in to comment.