Skip to content

Commit 25bb583

Browse files
committed
Calcolo fascia F23 per bioraria; fix #24
1 parent e519109 commit 25bb583

File tree

6 files changed

+28
-3
lines changed

6 files changed

+28
-3
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ template:
5252
{{ (1.1 * (states('sensor.pun_prezzo_fascia_corrente')|float(0) + 0.0087 + 0.04 + 0.0227))|round(3) }}
5353
```
5454
55+
### Fascia F23
56+
57+
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).
58+
Pertanto, seppur questo metodo non sia ufficiale, è stato implementato perché i risultati corrispondono sempre alle tabelle pubblicate online.
59+
5560
### In caso di problemi
5661
5762
È 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**.

custom_components/pun_sensor/__init__.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from .const import (
2323
DOMAIN,
2424
PUN_FASCIA_MONO,
25+
PUN_FASCIA_F23,
2526
PUN_FASCIA_F1,
2627
PUN_FASCIA_F2,
2728
PUN_FASCIA_F3,
@@ -138,8 +139,8 @@ def __init__(self, hass: HomeAssistant, config: ConfigEntry) -> None:
138139
# Inizializza i valori di default
139140
self.web_retries = 0
140141
self.schedule_token = None
141-
self.pun = [0.0, 0.0, 0.0, 0.0]
142-
self.orari = [0, 0, 0, 0]
142+
self.pun = [0.0, 0.0, 0.0, 0.0, 0.0]
143+
self.orari = [0, 0, 0, 0, 0]
143144
self.fascia_corrente = None
144145
_LOGGER.debug('Coordinator inizializzato (con \'usa dati reali\' = %s).', self.actual_data_only)
145146

@@ -263,6 +264,18 @@ async def _async_update_data(self):
263264
self.pun[PUN_FASCIA_F2] = mean(f2)
264265
if self.orari[PUN_FASCIA_F3] > 0:
265266
self.pun[PUN_FASCIA_F3] = mean(f3)
267+
268+
# Calcola la fascia F23 (a partire da F2 ed F3)
269+
# NOTA: la motivazione del calcolo è oscura ma sembra corretta; vedere:
270+
# https://github.com/virtualdj/pun_sensor/issues/24#issuecomment-1829846806
271+
if self.orari[PUN_FASCIA_F2] > 0 and self.orari[PUN_FASCIA_F3] > 0:
272+
# Esistono dati sia per F2 che per F3
273+
self.orari[PUN_FASCIA_F23] = self.orari[PUN_FASCIA_F2] + self.orari[PUN_FASCIA_F3]
274+
self.pun[PUN_FASCIA_F23] = 0.46 * self.pun[PUN_FASCIA_F2] + 0.54 * self.pun[PUN_FASCIA_F3]
275+
else:
276+
# Devono esserci dati sia per F2 che per F3 affinché il risultato sia valido
277+
self.orari[PUN_FASCIA_F23] = 0
278+
self.pun[PUN_FASCIA_F23] = 0
266279

267280
# Logga i dati
268281
_LOGGER.debug('Numero di dati: ' + ', '.join(str(i) for i in self.orari))

custom_components/pun_sensor/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
PUN_FASCIA_F1 = 1
77
PUN_FASCIA_F2 = 2
88
PUN_FASCIA_F3 = 3
9+
PUN_FASCIA_F23 = 4
910

1011
# Tipi di aggiornamento
1112
COORD_EVENT = "coordinator_event"

custom_components/pun_sensor/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"issue_tracker": "https://github.com/virtualdj/pun_sensor/issues",
1111
"loggers": ["pun_sensor"],
1212
"requirements": ["holidays", "bs4"],
13-
"version": "0.4.1"
13+
"version": "0.5.0"
1414
}

custom_components/pun_sensor/sensor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .const import (
2121
DOMAIN,
2222
PUN_FASCIA_MONO,
23+
PUN_FASCIA_F23,
2324
PUN_FASCIA_F1,
2425
PUN_FASCIA_F2,
2526
PUN_FASCIA_F3,
@@ -45,6 +46,7 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry,
4546
# Crea i sensori (legati al coordinator)
4647
entities = []
4748
entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_MONO))
49+
entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_F23))
4850
entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_F1))
4951
entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_F2))
5052
entities.append(PUNSensorEntity(coordinator, PUN_FASCIA_F3))
@@ -85,6 +87,8 @@ def __init__(self, coordinator: PUNDataUpdateCoordinator, tipo: int) -> None:
8587
self.entity_id = ENTITY_ID_FORMAT.format('pun_fascia_f1')
8688
elif (self.tipo == PUN_FASCIA_MONO):
8789
self.entity_id = ENTITY_ID_FORMAT.format('pun_mono_orario')
90+
elif (self.tipo == PUN_FASCIA_F23):
91+
self.entity_id = ENTITY_ID_FORMAT.format('pun_fascia_f23')
8892
else:
8993
self.entity_id = None
9094
self._attr_unique_id = self.entity_id
@@ -160,6 +164,8 @@ def name(self) -> str:
160164
return "PUN fascia F1"
161165
elif (self.tipo == PUN_FASCIA_MONO):
162166
return "PUN mono-orario"
167+
elif (self.tipo == PUN_FASCIA_F23):
168+
return "PUN fascia F23"
163169
else:
164170
return None
165171

screenshots_main.png

-17.8 KB
Loading

0 commit comments

Comments
 (0)