Skip to content

Commit

Permalink
Get electricity prices
Browse files Browse the repository at this point in the history
  • Loading branch information
hectorespert committed Jan 21, 2022
1 parent 1f2265d commit 659bc91
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.idea
__pycache__
.coverage
9 changes: 9 additions & 0 deletions custom_components/som_energia/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Som Energia integration."""

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config, HomeAssistant

Expand All @@ -13,4 +14,12 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Som Energia from a config entry."""
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)
return True


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
return True
48 changes: 45 additions & 3 deletions custom_components/som_energia/sensor.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,56 @@
from datetime import timedelta

from homeassistant.components.sensor import (
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ENERGY_KILO_WATT_HOUR, CURRENCY_EURO
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import StateType
from homeassistant.util import utcnow
from pytz import timezone


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
async_add_entities([ElectricityPriceSensor()], False)

SCAN_INTERVAL = timedelta(minutes=15)


class ElectricityPriceSensor(SensorEntity):
"""Class to hold the prices of electricity as a sensor."""

def __init__(self) -> None:
"""Initialize the sensor."""
self._attr_unique_id = 'som_energia_electricity_price'
self._attr_name = 'Som Energia electricity price'
self.entity_description = SensorEntityDescription(
key='electricity_price',
icon="mdi:currency-eur",
native_unit_of_measurement=f"{CURRENCY_EURO}/{ENERGY_KILO_WATT_HOUR}",
state_class=SensorStateClass.MEASUREMENT,
)
self._state = 0

@property
def name(self):
"""Return the name of the sensor"""
return "Som Energia electricity prices"
def native_value(self) -> StateType:
"""Return the state of the sensor."""
datetime = utcnow().astimezone(timezone('Europe/Madrid'))
weekday = datetime.isoweekday()
if weekday == 6 or weekday == 7:
return 0.262
hour = datetime.hour
if 0 <= hour < 8:
return 0.262
elif 8 <= hour < 10 or 14 <= hour < 18 or 22 <= hour < 24:
return 0.320
else:
return 0.407

0 comments on commit 659bc91

Please sign in to comment.