Skip to content

Commit

Permalink
Introduce integration with TGE energy sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofHajdamowicz committed Oct 30, 2024
1 parent 1a1c573 commit 40e769c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions custom_components/ev_smart_charging/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
PLATFORM_NORDPOOL = "nordpool"
PLATFORM_ENERGIDATASERVICE = "energidataservice"
PLATFORM_ENTSOE = "entsoe"
PLATFORM_TGE = "tge"
PLATFORM_VW = "volkswagen_we_connect_id"
PLATFORM_OCPP = "ocpp"
PLATFORM_GENERIC = "generic"
Expand Down
18 changes: 18 additions & 0 deletions custom_components/ev_smart_charging/helpers/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
PLATFORM_ENTSOE,
PLATFORM_GENERIC,
PLATFORM_NORDPOOL,
PLATFORM_TGE,
PLATFORM_OCPP,
PLATFORM_VW,
SWITCH,
Expand Down Expand Up @@ -131,6 +132,8 @@ def find_price_sensor(hass: HomeAssistant) -> str:
sensor = FindEntity.find_energidataservice_sensor(hass)
if len(sensor) == 0:
sensor = FindEntity.find_entsoe_sensor(hass)
if len(sensor) == 0:
sensor = FindEntity.find_tge_sensor(hass)
return sensor

@staticmethod
Expand Down Expand Up @@ -185,6 +188,21 @@ def find_generic_sensor(hass: HomeAssistant) -> str:
return entity_id
return ""

@staticmethod
def find_tge_sensor(hass: HomeAssistant) -> str:
"""Search for TGE sensor"""
entity_registry: EntityRegistry = async_entity_registry_get(hass)
registry_entries: UserDict[str, RegistryEntry] = (
entity_registry.entities.items()
)
for entry in registry_entries:
if entry[1].platform == PLATFORM_TGE:
entity_id = entry[1].entity_id
attributes = hass.states.get(entity_id).attributes
if all(attr in attributes for attr in ["prices", "prices_today", "prices_tomorrow"]):
return entity_id
return ""

@staticmethod
def find_vw_soc_sensor(hass: HomeAssistant) -> str:
"""Search for Volkswagen SOC sensor"""
Expand Down
3 changes: 2 additions & 1 deletion custom_components/ev_smart_charging/helpers/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from custom_components.ev_smart_charging.const import (
PLATFORM_ENERGIDATASERVICE,
PLATFORM_ENTSOE,
PLATFORM_TGE,
PLATFORM_GENERIC,
PLATFORM_NORDPOOL,
READY_HOUR_NONE,
Expand Down Expand Up @@ -59,7 +60,7 @@ def convert_raw_item(
# "price": float,
# }
# {'time': '2023-03-06 00:00:00+01:00', 'price': 0.1306} time is not datetime
if platform == PLATFORM_ENTSOE:
if platform in (PLATFORM_ENTSOE, PLATFORM_TGE):
if item["price"] is not None and isinstance(item["time"], str):
item_new = {}
item_new["value"] = item["price"]
Expand Down
9 changes: 5 additions & 4 deletions custom_components/ev_smart_charging/helpers/price_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
CONF_PRICE_SENSOR,
PLATFORM_ENERGIDATASERVICE,
PLATFORM_ENTSOE,
PLATFORM_TGE,
PLATFORM_GENERIC,
PLATFORM_NORDPOOL,
)
Expand Down Expand Up @@ -65,7 +66,7 @@ def get_raw_today_local(self, state) -> Raw:
if self._price_platform in (PLATFORM_NORDPOOL, PLATFORM_ENERGIDATASERVICE):
return Raw(state.attributes["raw_today"], self._price_platform)

if self._price_platform in (PLATFORM_ENTSOE, PLATFORM_GENERIC):
if self._price_platform in (PLATFORM_ENTSOE, PLATFORM_TGE, PLATFORM_GENERIC):
return Raw(state.attributes["prices_today"], self._price_platform)

return Raw([])
Expand All @@ -76,7 +77,7 @@ def get_raw_tomorrow_local(self, state) -> Raw:
if self._price_platform in (PLATFORM_NORDPOOL, PLATFORM_ENERGIDATASERVICE):
return Raw(state.attributes["raw_tomorrow"], self._price_platform)

if self._price_platform in (PLATFORM_ENTSOE, PLATFORM_GENERIC):
if self._price_platform in (PLATFORM_ENTSOE, PLATFORM_TGE, PLATFORM_GENERIC):
return Raw(state.attributes["prices_tomorrow"], self._price_platform)

return Raw([])
Expand All @@ -87,7 +88,7 @@ def get_current_price(self, state) -> float:
if self._price_platform in (PLATFORM_NORDPOOL, PLATFORM_ENERGIDATASERVICE):
return state.attributes["current_price"]

if self._price_platform in (PLATFORM_ENTSOE, PLATFORM_GENERIC):
if self._price_platform in (PLATFORM_ENTSOE, PLATFORM_TGE, PLATFORM_GENERIC):
time_now = dt.now()
return self.get_raw_today_local(state).get_value(time_now)

Expand Down Expand Up @@ -117,7 +118,7 @@ def validate_price_entity(
_LOGGER.debug("No attribute raw_tomorrow in price sensor")
return ("base", "sensor_is_not_price")

if price_platform in (PLATFORM_ENTSOE, PLATFORM_GENERIC):
if price_platform in (PLATFORM_ENTSOE, PLATFORM_TGE, PLATFORM_GENERIC):
if not "prices_today" in price_state.attributes.keys():
_LOGGER.debug("No attribute prices today in price sensor")
return ("base", "sensor_is_not_price")
Expand Down

0 comments on commit 40e769c

Please sign in to comment.