Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
magnuselden authored and magnuselden committed Apr 17, 2024
1 parent 52b0e89 commit b00340e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 18 deletions.
17 changes: 13 additions & 4 deletions peaqevcore/common/spotprice/energidataservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@


class EnergiDataServiceUpdater(SpotPriceBase):
def __init__(self, hub, observer, system:PeaqSystem, test:bool = False, is_active: bool = True):
super().__init__(hub=hub, source=ENERGIDATASERVICE, system=system, test=test, is_active=is_active, observer=observer)
def __init__(self, hub, observer, system:PeaqSystem, test:bool = False, is_active: bool = True, custom_sensor: str = None):
super().__init__(
hub=hub,
source=ENERGIDATASERVICE,
system=system,
test=test,
is_active=is_active,
observer=observer,
custom_sensor=custom_sensor
)

async def async_set_dto(self, ret, initial: bool = False) -> None:
_result = EnergiDataServiceDTO()
Expand All @@ -32,14 +40,15 @@ async def async_set_dto(self, ret, initial: bool = False) -> None:

def setup(self):
try:
sensor = self.state_machine.states.get(ENERGIDATASERVICE_SENSOR)
sensor_entity = self.custom_sensor if self.custom_sensor else ENERGIDATASERVICE_SENSOR
sensor = self.state_machine.states.get(sensor_entity)
if not sensor.state:
self.hub.options.price.price_aware = False # todo: composition
_LOGGER.error(
f"There were no Spotprice-entities. Cannot continue. with price-awareness."
)
else:
self.model.entity = ENERGIDATASERVICE_SENSOR
self.model.entity = sensor_entity
_LOGGER.debug(
f"EnergiDataService has been set up and is ready to be used with {self.model.entity}"
)
Expand Down
16 changes: 12 additions & 4 deletions peaqevcore/common/spotprice/nordpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@


class NordPoolUpdater(SpotPriceBase):
def __init__(self, hub, observer, system: PeaqSystem, test:bool = False, is_active: bool = True):
super().__init__(hub=hub, source=NORDPOOL, system=system, test=test, is_active=is_active, observer=observer)
def __init__(self, hub, observer, system: PeaqSystem, test:bool = False, is_active: bool = True, custom_sensor: str = None):
super().__init__(
hub=hub,
source=NORDPOOL,
system=system,
test=test,
is_active=is_active,
observer=observer,
custom_sensor=custom_sensor
)

async def async_set_dto(self, ret, initial: bool = False) -> None:
_result = NordpoolDTO()
Expand Down Expand Up @@ -42,8 +50,8 @@ def setup(self):
_LOGGER.error(
f"There were no Spotprice-entities. Cannot continue. with price-awareness."
)
if len(list(entities)) == 1:
self._setup_set_entity(list(entities)[0])
if len(list(entities)) == 1 or self.custom_sensor:
self._setup_set_entity(self.custom_sensor if self.custom_sensor else list(entities)[0])
else:
_found: bool = False
for e in list(entities):
Expand Down
16 changes: 7 additions & 9 deletions peaqevcore/common/spotprice/spotprice_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,20 @@ def create(
observer,
system: PeaqSystem,
test:bool = False,
is_active: bool = False
is_active: bool = False,
custom_sensor: str = None
) -> SpotPriceBase:
if test:
return NordPoolUpdater(hub, test, system, observer)
source = SpotPriceFactory.test_connections(hub.state_machine)
return SpotPriceFactory.sources[source](hub, observer, system, test, is_active)
source = SpotPriceFactory.test_connections(hub.state_machine, custom_sensor)
return SpotPriceFactory.sources[source](hub, observer, system, test, is_active, custom_sensor)

@staticmethod
def test_connections(hass) -> SpotPriceType:
def test_connections(hass, custom_sensor: str = None) -> SpotPriceType:
sensor = hass.states.get(ENERGIDATASERVICE_SENSOR)
if sensor:
_LOGGER.debug("Found sensor %s", sensor)
if sensor or (custom_sensor and custom_sensor == ENERGIDATASERVICE_SENSOR):
return SpotPriceType.EnergidataService
else:
_LOGGER.debug("No sensor %s", sensor)
return SpotPriceType.NordPool
return SpotPriceType.NordPool



7 changes: 6 additions & 1 deletion peaqevcore/common/spotprice/spotpricebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ async def async_broadcast(self, type: ObserverTypes, data = None) -> None:


class SpotPriceBase:
def __init__(self, hub, source: str, system: PeaqSystem, observer = MockObserver(), test:bool = False, is_active: bool = True):
def __init__(self, hub, source: str, system: PeaqSystem, observer = MockObserver(), test:bool = False, is_active: bool = True, custom_sensor: str = None):
_LOGGER.debug(f"Initializing Spotprice for {source} from system {system.value}.")
self.hub = hub
self.observer = observer
self.model = SpotPriceModel(source=source)
self._dynamic_top_price = DynamicTopPrice()
self._is_initialized: bool = False
self._custom_sensor: str = custom_sensor
if not test:
self.state_machine = hub.state_machine
if is_active:
self.setup()

@property
def custom_sensor(self) -> str|None:
return self._custom_sensor

@property
def tomorrow_valid(self) -> bool:
return self.model.tomorrow_valid
Expand Down

0 comments on commit b00340e

Please sign in to comment.