Skip to content

Commit

Permalink
Añadida la tarifa actual a los atributos
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelAngelLV committed Sep 3, 2023
1 parent 75666e5 commit a8894a9
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions custom_components/tarifa_20td/sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import asyncio
from datetime import datetime
from typing import Mapping, Any
from datetime import datetime, timedelta

Expand Down Expand Up @@ -71,8 +69,8 @@ def __init__(self, description: SensorEntityDescription, valle: float, llana: fl
self._valle = valle
self._llana = llana
self._punta = punta
self._period = ''
self._holidays = holidays.ES()

async def update_price_and_schedule(now):
self.update_price()
next = now + timedelta(hours=1)
Expand All @@ -86,10 +84,13 @@ async def update_price_and_schedule(now):
def native_value(self) -> StateType:
return self._state

@property
def extra_state_attributes(self) -> Mapping[str, Any] | None:
return {'Period': self._period}

async def async_added_to_hass(self) -> None:
self.update_price()


@property
def should_poll(self):
return False
Expand All @@ -99,20 +100,27 @@ def update_price(self):

if now in self._holidays or 5 <= now.weekday() <= 6:
self._state = self._valle
self._period = 'P3'
else:
hour = now.hour
if hour < 8:
self._state = self._valle
self._period = 'P3'
elif hour < 10:
self._state = self._llana
self._period = 'P2'
elif hour < 14:
self._state = self._punta
self._period = 'P1'
elif hour < 18:
self._state = self._llana
self._period = 'P2'
elif hour < 22:
self._state = self._punta
self._period = 'P3'
else:
self._state = self._llana
self._period = 'P2'

self.async_write_ha_state()

Expand Down Expand Up @@ -156,7 +164,6 @@ def update_price(self):
self.async_write_ha_state()



class DummySensor(SensorEntity):

def __init__(self, description: SensorEntityDescription) -> None:
Expand Down

0 comments on commit a8894a9

Please sign in to comment.