Skip to content
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/ecoflow/stream/bat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
from typing import TypedDict, Any

from modules.common.abstract_device import AbstractBat
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.simcount import SimCounter
from modules.common.store import get_component_value_store
from modules.devices.ecoflow.stream.config import EcoflowStreamBatSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType


class KwargsDict(TypedDict):
device_id: int


class EcoflowStreamBat(AbstractBat):
def __init__(self, component_config: EcoflowStreamBatSetup, **kwargs: Any) -> None:
self.component_config = component_config
self.kwargs: KwargsDict = kwargs

def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="speicher")
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.BAT, self.component_config.id, self.fault_state)

def update(self, response) -> None:
power = float(response['data']['powGetBpCms'])
soc = float(response['data']['cmsBattSoc'])

self.peak_filter.check_values(power)
imported, exported = self.sim_counter.sim_count(power)

bat_state = BatState(
power=power,
soc=soc,
imported=imported,
exported=exported
)
self.store.set(bat_state)


component_descriptor = ComponentDescriptor(configuration_factory=EcoflowStreamBatSetup)
68 changes: 68 additions & 0 deletions packages/modules/devices/ecoflow/stream/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from modules.common.component_setup import ComponentSetup
from ..vendor import vendor_descriptor


class EcoflowStreamConfiguration:
def __init__(self,
serial=None,
access_key=None,
secret_key=None):
self.serial = serial
self.access_key = access_key
self.secret_key = secret_key


class EcoflowStream:

def __init__(self,
name: str = "Ecoflow Stream",
type: str = "ecoflow_stream",
id: int = 0,
configuration: EcoflowStreamConfiguration = None) -> None:
self.name = name
self.type = type
self.vendor = vendor_descriptor.configuration_factory().type
self.id = id
self.configuration = configuration or EcoflowStreamConfiguration()


class EcoflowStreamBatConfiguration:
def __init__(self):
pass


class EcoflowStreamBatSetup(ComponentSetup[EcoflowStreamBatConfiguration]):
def __init__(self,
name: str = "Ecoflow Stream Speicher",
type: str = "bat",
id: int = 0,
configuration: EcoflowStreamBatConfiguration = None) -> None:
super().__init__(name, type, id, configuration or EcoflowStreamBatConfiguration())


class EcoflowStreamCounterConfiguration:
def __init__(self):
pass


class EcoflowStreamCounterSetup(ComponentSetup[EcoflowStreamCounterConfiguration]):
def __init__(self,
name: str = "Ecoflow Stream Zähler",
type: str = "counter",
id: int = 0,
configuration: EcoflowStreamCounterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or EcoflowStreamCounterConfiguration())


class EcoflowStreamInverterConfiguration:
def __init__(self):
pass


class EcoflowStreamInverterSetup(ComponentSetup[EcoflowStreamInverterConfiguration]):
def __init__(self,
name: str = "Ecoflow Stream Wechselrichter",
type: str = "inverter",
id: int = 0,
configuration: EcoflowStreamInverterConfiguration = None) -> None:
super().__init__(name, type, id, configuration or EcoflowStreamInverterConfiguration())
44 changes: 44 additions & 0 deletions packages/modules/devices/ecoflow/stream/counter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
from typing import TypedDict, Any

from modules.common.abstract_device import AbstractCounter
from modules.common.component_state import CounterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.simcount._simcounter import SimCounter
from modules.common.store import get_component_value_store
from modules.devices.ecoflow.stream.config import EcoflowStreamCounterSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType


class KwargsDict(TypedDict):
device_id: int


class EcoflowStreamCounter(AbstractCounter):
def __init__(self, component_config: EcoflowStreamCounterSetup, **kwargs: Any) -> None:
self.component_config = component_config
self.kwargs: KwargsDict = kwargs

def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="bezug")
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.COUNTER, self.component_config.id, self.fault_state)

def update(self, response) -> None:
power = float(response['data']['powGetSysGrid'])
self.peak_filter.check_values(power)
imported, exported = self.sim_counter.sim_count(power)

counter_state = CounterState(
imported=imported,
exported=exported,
power=power
)
self.store.set(counter_state)


component_descriptor = ComponentDescriptor(configuration_factory=EcoflowStreamCounterSetup)
79 changes: 79 additions & 0 deletions packages/modules/devices/ecoflow/stream/device.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
import logging
from typing import Union, Iterable
import hmac
import hashlib
import time
import uuid
from modules.common import req
from modules.common.abstract_device import DeviceDescriptor
from modules.common.component_context import SingleComponentUpdateContext
from modules.common.configurable_device import (ConfigurableDevice, ComponentFactoryByType,
MultiComponentUpdater)
from modules.devices.ecoflow.stream.bat import EcoflowStreamBat
from modules.devices.ecoflow.stream.config import (EcoflowStream,
EcoflowStreamBatSetup,
EcoflowStreamCounterSetup,
EcoflowStreamInverterSetup)
from modules.devices.ecoflow.stream.counter import EcoflowStreamCounter
from modules.devices.ecoflow.stream.inverter import EcoflowStreamInverter

log = logging.getLogger(__name__)
EcoflowStreamComponent = Union[EcoflowStreamBat, EcoflowStreamCounter, EcoflowStreamInverter]


def create_device(device_config: EcoflowStream):
def create_bat(component_config: EcoflowStreamBatSetup) -> EcoflowStreamBat:
return EcoflowStreamBat(component_config=component_config, device_id=device_config.id)

def create_counter(component_config: EcoflowStreamCounterSetup) -> EcoflowStreamCounter:
return EcoflowStreamCounter(component_config=component_config, device_id=device_config.id)

def create_inverter(component_config: EcoflowStreamInverterSetup) -> EcoflowStreamInverter:
return EcoflowStreamInverter(component_config=component_config, device_id=device_config.id)

def generate_sign(access_key, secret_key, timestamp, nonce):
message = f"accessKey={access_key}&nonce={nonce}&timestamp={timestamp}"
sign = hmac.new(
secret_key.encode("utf-8"),
message.encode("utf-8"),
hashlib.sha256
).hexdigest()
return sign

def update_components(components: Iterable[EcoflowStreamComponent]):
access_key = device_config.configuration.access_key
secret_key = device_config.configuration.secret_key
serial = device_config.configuration.serial
base_url = "https://api-e.ecoflow.com"

timestamp = str(int(time.time() * 1000))
nonce = str(uuid.uuid4())
sign = generate_sign(access_key, secret_key, timestamp, nonce)

headers = {
"accessKey": access_key,
"timestamp": timestamp,
"nonce": nonce,
"sign": sign,
"Content-Type": "application/json"
}
url = f"{base_url}/iot-open/sign/device/quota/all?sn={serial}"

response = req.get_http_session().get(
url,
headers=headers,
timeout=10
).json()
for component in components:
with SingleComponentUpdateContext(component.fault_state):
component.update(response)

return ConfigurableDevice(
device_config,
component_factory=ComponentFactoryByType(bat=create_bat, counter=create_counter, inverter=create_inverter),
component_updater=MultiComponentUpdater(update_components)
)


device_descriptor = DeviceDescriptor(configuration_factory=EcoflowStream)
44 changes: 44 additions & 0 deletions packages/modules/devices/ecoflow/stream/inverter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
from typing import TypedDict, Any

from modules.common.abstract_device import AbstractInverter
from modules.common.component_state import InverterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo, FaultState
from modules.common.simcount import SimCounter
from modules.common.store import get_component_value_store
from modules.devices.ecoflow.stream.config import EcoflowStreamInverterSetup
from modules.common.utils.peak_filter import PeakFilter
from modules.common.component_type import ComponentType


class KwargsDict(TypedDict):
device_id: int


class EcoflowStreamInverter(AbstractInverter):
def __init__(self, component_config: EcoflowStreamInverterSetup, **kwargs: Any) -> None:
self.component_config = component_config
self.kwargs: KwargsDict = kwargs

def initialize(self) -> None:
self.__device_id: int = self.kwargs['device_id']
self.sim_counter = SimCounter(self.__device_id, self.component_config.id, prefix="pv")
self.store = get_component_value_store(self.component_config.type, self.component_config.id)
self.fault_state = FaultState(ComponentInfo.from_component_config(self.component_config))
self.peak_filter = PeakFilter(ComponentType.INVERTER, self.component_config.id, self.fault_state)

def update(self, response) -> None:
power = float(response['data']['powGetPvSum']) * -1

self.peak_filter.check_values(power)
_, exported = self.sim_counter.sim_count(power)

inverter_state = InverterState(
power=power,
exported=exported
)
self.store.set(inverter_state)


component_descriptor = ComponentDescriptor(configuration_factory=EcoflowStreamInverterSetup)
14 changes: 14 additions & 0 deletions packages/modules/devices/ecoflow/vendor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from pathlib import Path

from modules.common.abstract_device import DeviceDescriptor
from modules.devices.vendors import VendorGroup


class Vendor:
def __init__(self):
self.type = Path(__file__).parent.name
self.vendor = "Ecoflow"
self.group = VendorGroup.VENDORS.value


vendor_descriptor = DeviceDescriptor(configuration_factory=Vendor)