Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional attributes #15

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions custom_components/elektro_network_tariff/elektro_network_tariff.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,21 @@ def is_weekend_or_holiday(date):
easter_saturday, easter_monday = get_easter_saturday_monday(date.year)

# Add Easter Saturday and Easter Monday to the list of public holidays
public_holidays.append((easter_saturday.month, easter_saturday.day))
public_holidays.append((easter_monday.month, easter_monday.day))
public_holidays.extend([(easter_saturday.month, easter_saturday.day),
(easter_monday.month, easter_monday.day)])

# Check if the date matches any public holiday
if (date.month, date.day) in public_holidays:
print("hollyday")
return True

return False

return (date.month, date.day) in public_holidays

def calculate_tariff():
"""Calculate the current tariff and additional attributes for electricity tariff blocks."""
date = datetime.datetime.now()
month = date.month
hour = date.hour
is_high_season = month in [11, 12, 1, 2]
weekend_or_holiday = is_weekend_or_holiday(date)
is_holiday = is_weekend_or_holiday(date)

# Define tariff rates in a more structured form
# Define tariff rates in a structured form
# (hour_range, high_season_rate, low_season_rate)
tariffs = [
((0, 5), (3, 4), (5, 4)), # Early morning
Expand All @@ -88,9 +84,9 @@ def calculate_tariff():
for time_range, high_season_tariff, low_season_tariff in tariffs:
start, end = time_range
if start <= hour <= end:
if is_high_season and not weekend_or_holiday:
if is_high_season and not is_holiday:
blocks.append(high_season_tariff[0])
elif not is_high_season and weekend_or_holiday:
elif not is_high_season and is_holiday:
blocks.append(low_season_tariff[0])
else:
blocks.append(high_season_tariff[1] if is_high_season else low_season_tariff[1])
Expand All @@ -99,7 +95,19 @@ def calculate_tariff():
# Default tariff if none of the conditions above are met
blocks.append(0)

# Now return the current tariff and the blocks
current_tariff = blocks[date.hour] # Get the tariff for the current hour
return current_tariff, blocks # Return both the current tariff and the blocks
# Get the tariff for the current hour
current_tariff = blocks[date.hour]
# Get the tariff block for the next hour
next_tariff_block = blocks[(date.hour + 1) % 24]
# Determine if the next block is higher than the current
is_next_block_higher = next_tariff_block > current_tariff

# Return a dictionary with the required attributes
return {
"current_tariff": current_tariff, # Current hour's tariff
"is_holiday": is_holiday, # Is today a holiday or weekend
"is_high_season": is_high_season, # Is high tariff season
"next_tariff_block": next_tariff_block, # Tariff for the next hour
"is_next_block_higher": is_next_block_higher, # Is the next block higher
"blocks": blocks # Tariff blocks for the entire day
}
2 changes: 1 addition & 1 deletion custom_components/elektro_network_tariff/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "local_polling",
"issue_tracker": "https://github.com/frlequ/elektro_network_tariff/issues",
"requirements": ["requests>=2.25.1"],
"version": "0.1.5"
"version": "0.1.6"
}
19 changes: 17 additions & 2 deletions custom_components/elektro_network_tariff/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ def __init__(self, name, entity_id):
self._attr_unique_id = entity_id # Set a unique ID for the entity
self._state = None
self._blocks = None
self._is_holiday = None
self._is_high_season = None
self._next_tariff_block = None
self._is_next_block_higher = None

@property
def state(self):
Expand All @@ -36,7 +40,11 @@ def extra_state_attributes(self):
"""Return the state attributes."""
return {
"state_class": "measurement",
"blocks": ','.join(map(str, self._blocks)) if self._blocks else ''
"blocks": ','.join(map(str, self._blocks)) if self._blocks else '',
"holiday": self._is_holiday,
"high_season": self._is_high_season,
"next_tariff_block": self._next_tariff_block,
"next_block_higher": self._is_next_block_higher
}

@property
Expand All @@ -47,6 +55,13 @@ def icon(self):
async def async_update(self):
"""Fetch new state data for the sensor asynchronously."""
try:
self._state, self._blocks = calculate_tariff() # Fetch data asynchronously if needed
# Fetch all attributes from the calculate_tariff function
tariff_data = calculate_tariff()
self._state = tariff_data["current_tariff"]
self._blocks = tariff_data["blocks"]
self._is_holiday = tariff_data["is_holiday"]
self._is_high_season = tariff_data["is_high_season"]
self._next_tariff_block = tariff_data["next_tariff_block"]
self._is_next_block_higher = tariff_data["is_next_block_higher"]
except Exception as e:
_LOGGER.error(f"Error updating Elektro Network Tariff Sensor: {e}")
Loading