Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature enphase #1526

Merged
merged 7 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions packages/modules/devices/enphase/bat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
import logging
from typing import Any, Dict, Optional, Union

from dataclass_utils import dataclass_from_dict
from modules.common.component_state import BatState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.store import get_bat_value_store
from modules.common.simcount import SimCounter
from modules.devices.enphase.config import EnphaseBatSetup

log = logging.getLogger(__name__)


class EnphaseBat:
def __init__(self, device_id: int, component_config: Union[Dict, EnphaseBatSetup]) -> None:
self.component_config = dataclass_from_dict(EnphaseBatSetup, component_config)
self.store = get_bat_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.__device_id = device_id
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher")

def update(self, response, live_data: Optional[Dict[str, Any]] = None) -> None:
if live_data is None:
raise ValueError("Es stehen keine Daten vom Speicher zur Verfügung.")
bat_data = live_data["meters"]["storage"]
if bat_data is None:
# configuration wrong or error
raise ValueError("Es konnten keine Daten vom Speicher gelesen werden.")
soc = live_data["meters"]["soc"]
power = bat_data['agg_p_mw'] / -1000 # negative is charging
# powers = [bat_data['agg_p_ph_a_mw'] / 1000,
# bat_data['agg_p_ph_b_mw'] / 1000,
# bat_data['agg_p_ph_c_mw'] / 1000]
imported, exported = self.sim_counter.sim_count(power)
bat_state = BatState(
imported=imported,
exported=exported,
soc=soc,
power=power,
# powers=powers
)
self.store.set(bat_state)


component_descriptor = ComponentDescriptor(configuration_factory=EnphaseBatSetup)
33 changes: 32 additions & 1 deletion packages/modules/devices/enphase/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
from enum import Enum
from typing import Optional
from modules.common.component_setup import ComponentSetup


class EnphaseVersion(Enum):
V1 = 1
V2 = 2


class EnphaseConfiguration:
def __init__(self, hostname=None):
def __init__(self, hostname: Optional[str] = None,
version: EnphaseVersion = EnphaseVersion.V1,
user: Optional[str] = None,
password: Optional[str] = None,
serial: Optional[str] = None,
token: Optional[str] = None):
self.hostname = hostname
self.version = version
self.user = user
self.password = password
self.serial = serial
self.token = token


class Enphase:
Expand Down Expand Up @@ -44,3 +61,17 @@ def __init__(self,
id: int = 0,
configuration: EnphaseInverterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or EnphaseInverterConfiguration())


class EnphaseBatConfiguration:
def __init__(self):
pass


class EnphaseBatSetup(ComponentSetup[EnphaseBatConfiguration]):
def __init__(self,
name: str = "Enphase Speicher",
type: str = "bat",
id: int = 0,
configuration: EnphaseBatConfiguration = None) -> None:
super().__init__(name, type, id, configuration or EnphaseBatConfiguration())
13 changes: 4 additions & 9 deletions packages/modules/devices/enphase/counter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
import logging
from typing import Dict, Union
from typing import Any, Dict, Union

from dataclass_utils import dataclass_from_dict
from modules.common.component_state import CounterState
Expand All @@ -13,24 +13,20 @@


class EnphaseCounter:
def __init__(self, component_config: Union[Dict, EnphaseCounterSetup]) -> None:
def __init__(self, device_id: int, component_config: Union[Dict, EnphaseCounterSetup]) -> None:
self.component_config = dataclass_from_dict(EnphaseCounterSetup, component_config)
self.store = get_counter_value_store(self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))

def update(self, response):
config = self.component_config.configuration

def update(self, response: Dict[str, Any], live_data):
meter = None
for m in response:
if m['eid'] == int(config.eid):
if m['eid'] == int(self.component_config.configuration.eid):
meter = m
break

if meter is None:
# configuration wrong or error
raise ValueError("Es konnten keine Daten vom Messgerät gelesen werden.")

counter_state = CounterState(
imported=meter['actEnergyDlvd'],
exported=meter['actEnergyRcvd'],
Expand All @@ -49,7 +45,6 @@ def update(self, response):
meter['channels'][2]['pwrFactor']],
frequency=meter['freq']
)

self.store.set(counter_state)


Expand Down
Loading