From 659bc91d02e5ba7276e6b1d2e263a18e6f6996ce Mon Sep 17 00:00:00 2001 From: Hector Espert Date: Sat, 22 Jan 2022 00:59:34 +0100 Subject: [PATCH] Get electricity prices --- .gitignore | 2 + custom_components/som_energia/__init__.py | 9 +++++ custom_components/som_energia/sensor.py | 48 +++++++++++++++++++++-- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 485dee6..4c5c499 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ .idea +__pycache__ +.coverage \ No newline at end of file diff --git a/custom_components/som_energia/__init__.py b/custom_components/som_energia/__init__.py index f446609..390d6aa 100644 --- a/custom_components/som_energia/__init__.py +++ b/custom_components/som_energia/__init__.py @@ -1,4 +1,5 @@ """Som Energia integration.""" + from homeassistant.config_entries import ConfigEntry from homeassistant.core import Config, HomeAssistant @@ -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 diff --git a/custom_components/som_energia/sensor.py b/custom_components/som_energia/sensor.py index 4fbb6be..be5d7d7 100644 --- a/custom_components/som_energia/sensor.py +++ b/custom_components/som_energia/sensor.py @@ -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" \ No newline at end of file + 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 +