diff --git a/README.md b/README.md index 60adb55..0f4ba06 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,11 @@ template: {{ (1.1 * (states('sensor.pun_prezzo_fascia_corrente')|float(0) + 0.0087 + 0.04 + 0.0227))|round(3) }} ``` +### Fascia F23 + +A partire dalla versione v0.5.0, è stato aggiunto il sensore relativo al calcolo della fascia F23, cioè quella contrapposta alla F1 nella bioraria. Il calcolo non è documentato molto nei vari siti (si veda [QUI](https://github.com/virtualdj/pun_sensor/issues/24#issuecomment-1806864251)) e non è affatto la media dei prezzi in F2 e F3 come si potrebbe pensare: c'è invece una percentuale fissa, [come ha scoperto *virtualj*](https://github.com/virtualdj/pun_sensor/issues/24#issuecomment-1829846806). +Pertanto, seppur questo metodo non sia ufficiale, è stato implementato perché i risultati corrispondono sempre alle tabelle pubblicate online. + ### In caso di problemi È possibile abilitare la registrazione dei log tramite l'interfaccia grafica in **Impostazioni > Dispositivi e servizi > Prezzi PUN del mese** e cliccando sul pulsante **Abilita la registrazione di debug**. diff --git a/custom_components/pun_sensor/__init__.py b/custom_components/pun_sensor/__init__.py index a41fba4..396cfc6 100644 --- a/custom_components/pun_sensor/__init__.py +++ b/custom_components/pun_sensor/__init__.py @@ -22,6 +22,7 @@ from .const import ( DOMAIN, PUN_FASCIA_MONO, + PUN_FASCIA_F23, PUN_FASCIA_F1, PUN_FASCIA_F2, PUN_FASCIA_F3, @@ -138,8 +139,8 @@ def __init__(self, hass: HomeAssistant, config: ConfigEntry) -> None: # Inizializza i valori di default self.web_retries = 0 self.schedule_token = None - self.pun = [0.0, 0.0, 0.0, 0.0] - self.orari = [0, 0, 0, 0] + self.pun = [0.0, 0.0, 0.0, 0.0, 0.0] + self.orari = [0, 0, 0, 0, 0] self.fascia_corrente = None _LOGGER.debug('Coordinator inizializzato (con \'usa dati reali\' = %s).', self.actual_data_only) @@ -263,6 +264,18 @@ async def _async_update_data(self): self.pun[PUN_FASCIA_F2] = mean(f2) if self.orari[PUN_FASCIA_F3] > 0: self.pun[PUN_FASCIA_F3] = mean(f3) + + # Calcola la fascia F23 (a partire da F2 ed F3) + # NOTA: la motivazione del calcolo è oscura ma sembra corretta; vedere: + # https://github.com/virtualdj/pun_sensor/issues/24#issuecomment-1829846806 + if self.orari[PUN_FASCIA_F2] > 0 and self.orari[PUN_FASCIA_F3] > 0: + # Esistono dati sia per F2 che per F3 + self.orari[PUN_FASCIA_F23] = self.orari[PUN_FASCIA_F2] + self.orari[PUN_FASCIA_F3] + self.pun[PUN_FASCIA_F23] = 0.46 * self.pun[PUN_FASCIA_F2] + 0.54 * self.pun[PUN_FASCIA_F3] + else: + # Devono esserci dati sia per F2 che per F3 affinché il risultato sia valido + self.orari[PUN_FASCIA_F23] = 0 + self.pun[PUN_FASCIA_F23] = 0 # Logga i dati _LOGGER.debug('Numero di dati: ' + ', '.join(str(i) for i in self.orari)) diff --git a/custom_components/pun_sensor/const.py b/custom_components/pun_sensor/const.py index b56af35..42f57e4 100644 --- a/custom_components/pun_sensor/const.py +++ b/custom_components/pun_sensor/const.py @@ -6,6 +6,7 @@ PUN_FASCIA_F1 = 1 PUN_FASCIA_F2 = 2 PUN_FASCIA_F3 = 3 +PUN_FASCIA_F23 = 4 # Tipi di aggiornamento COORD_EVENT = "coordinator_event" diff --git a/custom_components/pun_sensor/manifest.json b/custom_components/pun_sensor/manifest.json index ebfaf0d..76261a3 100644 --- a/custom_components/pun_sensor/manifest.json +++ b/custom_components/pun_sensor/manifest.json @@ -10,5 +10,5 @@ "issue_tracker": "https://github.com/virtualdj/pun_sensor/issues", "loggers": ["pun_sensor"], "requirements": ["holidays", "bs4"], - "version": "0.4.1" + "version": "0.5.0" } diff --git a/custom_components/pun_sensor/sensor.py b/custom_components/pun_sensor/sensor.py index abeac78..49a49a5 100644 --- a/custom_components/pun_sensor/sensor.py +++ b/custom_components/pun_sensor/sensor.py @@ -20,6 +20,7 @@ from .const import ( DOMAIN, PUN_FASCIA_MONO, + PUN_FASCIA_F23, PUN_FASCIA_F1, PUN_FASCIA_F2, PUN_FASCIA_F3, @@ -45,6 +46,7 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, # Crea i sensori (legati al coordinator) entities = [] entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_MONO)) + entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_F23)) entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_F1)) entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_F2)) entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_F3)) @@ -85,6 +87,8 @@ def __init__(self, coordinator: PUNDataUpdateCoordinator, tipo: int) -> None: self.entity_id = ENTITY_ID_FORMAT.format('pun_fascia_f1') elif (self.tipo == PUN_FASCIA_MONO): self.entity_id = ENTITY_ID_FORMAT.format('pun_mono_orario') + elif (self.tipo == PUN_FASCIA_F23): + self.entity_id = ENTITY_ID_FORMAT.format('pun_fascia_f23') else: self.entity_id = None self._attr_unique_id = self.entity_id @@ -160,6 +164,8 @@ def name(self) -> str: return "PUN fascia F1" elif (self.tipo == PUN_FASCIA_MONO): return "PUN mono-orario" + elif (self.tipo == PUN_FASCIA_F23): + return "PUN fascia F23" else: return None diff --git a/screenshots_main.png b/screenshots_main.png index 24f375e..b582b1b 100644 Binary files a/screenshots_main.png and b/screenshots_main.png differ