Skip to content

Commit

Permalink
#15
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed Aug 14, 2023
1 parent 3bec8a0 commit b36ba8f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 30 deletions.
2 changes: 1 addition & 1 deletion custom_components/peaqnext/sensors/next_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ async def async_update(self) -> None:
self._non_hours_start = status.get("non_hours_start", [])
self._non_hours_end = status.get("non_hours_end", [])
self._closest_cheap_hour = status.get("closest_cheap_hour", 12)
self._price_source = status.get("price_source", "unknown")
self._price_source = status.get("price_source", "unknown").capitalize()

@property
def extra_state_attributes(self) -> dict:
Expand Down
1 change: 1 addition & 0 deletions custom_components/peaqnext/service/spotprice/const.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NORDPOOL = "nordpool"
ENERGIDATASERVICE = "energidataservice"
ENERGIDATASERVICE_SENSOR = "sensor.energi_data_service"
28 changes: 26 additions & 2 deletions custom_components/peaqnext/service/spotprice/energidataservice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from custom_components.peaqnext.service.spotprice.ispotprice import ISpotPrice
from custom_components.peaqnext.service.spotprice.spotprice_dto import EnergiDataServiceDTO
from custom_components.peaqnext.service.spotprice.const import ENERGIDATASERVICE
from custom_components.peaqnext.service.spotprice.const import ENERGIDATASERVICE, ENERGIDATASERVICE_SENSOR
import asyncio
import logging

_LOGGER = logging.getLogger(__name__)


class EnergiDataServiceUpdater(ISpotPrice):
def __init__(self, hub, test:bool = False):
Expand All @@ -13,4 +18,23 @@ async def async_set_dto(self, ret) -> None:
await self.hub.async_update_prices(
(self.prices, self.prices_tomorrow)
)
self._is_initialized = True
self._is_initialized = True

def setup(self):
try:
sensor = self.state_machine.states.get(ENERGIDATASERVICE_SENSOR)
if not sensor.state:
raise Exception("no entities found for Spotprice.")
else:
self._entity = ENERGIDATASERVICE_SENSOR
_LOGGER.debug(
f"EnergiDataService has been set up and is ready to be used with {self.entity}"
)
asyncio.run_coroutine_threadsafe(
self.async_update_spotprice(),
self.state_machine.loop,
)
except Exception as e:
_LOGGER.error(
f"I was unable to get a Spotprice-entity. Cannot continue.: {e}"
)
24 changes: 2 additions & 22 deletions custom_components/peaqnext/service/spotprice/ispotprice.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import logging
import homeassistant.helpers.template as template
import asyncio
from custom_components.peaqnext.service.spotprice.spotprice_dto import ISpotPriceDTO
from custom_components.peaqnext.service.spotprice.const import *
from abc import abstractmethod
Expand Down Expand Up @@ -78,27 +76,9 @@ async def async_update_spotprice(self) -> None:
async def async_set_dto(self, ret) -> None:
pass

@abstractmethod
def setup(self):
try:
entities = template.integration_entities(self.state_machine, self._source)
_LOGGER.debug(f"Found {list(entities)} Spotprice entities.")
if len(list(entities)) < 1:
raise Exception("no entities found for Spotprice.")
if len(list(entities)) == 1:
self._entity = entities[0]
_LOGGER.debug(
f"Nordpool has been set up and is ready to be used with {self.entity}"
)
asyncio.run_coroutine_threadsafe(
self.async_update_spotprice(),
self.state_machine.loop,
)
else:
_LOGGER.error(f"more than one Spotprice entity found. Cannot continue.")
except Exception as e:
_LOGGER.error(
f"I was unable to get a Spotprice-entity. Cannot continue.: {e}"
)
pass

async def async_update_spotprice(self) -> None:
if self.entity is not None:
Expand Down
29 changes: 28 additions & 1 deletion custom_components/peaqnext/service/spotprice/nordpool.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from custom_components.peaqnext.service.spotprice.ispotprice import ISpotPrice
from custom_components.peaqnext.service.spotprice.spotprice_dto import NordpoolDTO
from custom_components.peaqnext.service.spotprice.const import NORDPOOL
import logging
import asyncio
import homeassistant.helpers.template as template
_LOGGER = logging.getLogger(__name__)


class NordPoolUpdater(ISpotPrice):
def __init__(self, hub, test:bool = False):
Expand All @@ -13,4 +18,26 @@ async def async_set_dto(self, ret) -> None:
await self.hub.async_update_prices(
(self.prices, self.prices_tomorrow)
)
self._is_initialized = True
self._is_initialized = True

def setup(self):
try:
entities = template.integration_entities(self.state_machine, self._source)
_LOGGER.debug(f"Found {list(entities)} Spotprice entities.")
if len(list(entities)) < 1:
raise Exception("no entities found for Spotprice.")
if len(list(entities)) == 1:
self._entity = entities[0]
_LOGGER.debug(
f"Nordpool has been set up and is ready to be used with {self.entity}"
)
asyncio.run_coroutine_threadsafe(
self.async_update_spotprice(),
self.state_machine.loop,
)
else:
_LOGGER.error(f"more than one Spotprice entity found. Cannot continue.")
except Exception as e:
_LOGGER.error(
f"I was unable to get a Spotprice-entity. Cannot continue.: {e}"
)
15 changes: 11 additions & 4 deletions custom_components/peaqnext/service/spotprice/spotprice_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from custom_components.peaqnext.service.spotprice.ispotprice import ISpotPrice
from custom_components.peaqnext.service.spotprice.nordpool import NordPoolUpdater
from custom_components.peaqnext.service.spotprice.energidataservice import EnergiDataServiceUpdater
import logging

_LOGGER = logging.getLogger(__name__)


class SpotPriceFactory:

Expand All @@ -20,10 +24,13 @@ def create(hub, test:bool = False) -> ISpotPrice:

@staticmethod
def test_connections(hass) -> str:
entities = template.integration_entities(hass, ENERGIDATASERVICE)
if len(list(entities)):
ENERGIDATASERVICE
return NORDPOOL
sensor = hass.states.get(ENERGIDATASERVICE_SENSOR)
if sensor:
_LOGGER.debug("Found sensor %s", sensor)
return ENERGIDATASERVICE
else:
_LOGGER.debug("No sensor %s", sensor)
return NORDPOOL



0 comments on commit b36ba8f

Please sign in to comment.