Skip to content

Commit

Permalink
fix and adapt the format of discovergy to the other modules (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
LKuemmel committed May 4, 2023
1 parent 9c0f1f9 commit cb8ac8a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
17 changes: 13 additions & 4 deletions packages/modules/devices/discovergy/counter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
from modules.common.store import get_counter_value_store
from requests import Session

from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo
from modules.common.store import get_counter_value_store
from modules.devices.discovergy import api
from modules.devices.discovergy.config import DiscovergyCounterSetup
from modules.devices.discovergy.utils import DiscovergyComponent


def create_component(component_config: DiscovergyCounterSetup):
return DiscovergyComponent(component_config, get_counter_value_store(component_config.id).set)
class DiscovergyCounter:
def __init__(self, component_config: DiscovergyCounterSetup) -> None:
self.component_config = component_config
self.store = get_counter_value_store(self.component_config.id)
self.component_info = ComponentInfo.from_component_config(self.component_config)

def update(self, session: Session):
self.store.set(api.get_last_reading(session, self.component_config.configuration.meter_id))


component_descriptor = ComponentDescriptor(configuration_factory=DiscovergyCounterSetup)
8 changes: 7 additions & 1 deletion packages/modules/devices/discovergy/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@


def create_device(device_config: Discovergy):
def create_counter_component(component_config: DiscovergyCounterSetup):
return counter.DiscovergyCounter(component_config)

def create_inverter_component(component_config: DiscovergyInverterSetup):
return inverter.DiscovergInverter(component_config)

session = get_http_session()
session.auth = (device_config.configuration.user, device_config.configuration.password)
return ConfigurableDevice(
device_config=device_config,
component_factory=ComponentFactoryByType(counter=counter.create_component, inverter=inverter.create_component),
component_factory=ComponentFactoryByType(counter=create_counter_component, inverter=create_inverter_component),
component_updater=IndependentComponentUpdater(lambda component: component.update(session)),
)

Expand Down
4 changes: 2 additions & 2 deletions packages/modules/devices/discovergy/device_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from modules.common.component_state import CounterState
from modules.common.component_state import InverterState
from modules.devices.discovergy import counter, inverter, utils
from modules.devices.discovergy import api, counter, inverter
from modules.devices.discovergy.device import read_legacy

SAMPLE_COUNTER_STATE = CounterState(
Expand Down Expand Up @@ -34,7 +34,7 @@ def setup(self, monkeypatch):
self.mock_get_last_reading = Mock(return_value=SAMPLE_COUNTER_STATE)
monkeypatch.setattr(counter, 'get_counter_value_store', Mock(return_value=self.mock_counter_value_store))
monkeypatch.setattr(inverter, 'get_inverter_value_store', Mock(return_value=self.mock_inverter_value_store))
monkeypatch.setattr(utils, 'get_last_reading', self.mock_get_last_reading)
monkeypatch.setattr(api, 'get_last_reading', self.mock_get_last_reading)

def test_read_legacy_reads_counter(self, monkeypatch):
# execution
Expand Down
21 changes: 13 additions & 8 deletions packages/modules/devices/discovergy/inverter.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
from modules.common.component_state import InverterState, CounterState
from requests import Session

from modules.common.component_state import InverterState
from modules.common.component_type import ComponentDescriptor
from modules.common.fault_state import ComponentInfo
from modules.common.store import get_inverter_value_store
from modules.devices.discovergy.utils import DiscovergyComponent
from modules.devices.discovergy import api
from modules.devices.discovergy.config import DiscovergyInverterSetup


def create_component(component_config: DiscovergyInverterSetup):
store = get_inverter_value_store(component_config.id)
class DiscovergInverter:
def __init__(self, component_config: DiscovergyInverterSetup) -> None:
self.component_config = component_config
self.store = get_inverter_value_store(self.component_config.id)
self.component_info = ComponentInfo.from_component_config(self.component_config)

def persister(reading: CounterState):
store.set(InverterState(
def update(self, session: Session):
reading = api.get_last_reading(session, self.component_config.configuration.meter_id)
self.store.set(InverterState(
exported=reading.exported,
power=reading.power,
currents=reading.currents
))

return DiscovergyComponent(component_config, persister)


component_descriptor = ComponentDescriptor(configuration_factory=DiscovergyInverterSetup)
20 changes: 0 additions & 20 deletions packages/modules/devices/discovergy/utils.py

This file was deleted.

0 comments on commit cb8ac8a

Please sign in to comment.