Skip to content

Commit

Permalink
modify ComponentFactoryByType to also use partial for component creators
Browse files Browse the repository at this point in the history
  • Loading branch information
LKuemmel committed Oct 14, 2022
1 parent 1fe8a95 commit d989858
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/modules/common/configurable_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def __call__(self, component_config: T_COMPONENT_CONFIG) -> T_COMPONENT:
raise Exception(
"Unknown component type <%s>, known types are: <%s>", e, ','.join(self.__type_to_factory.keys())
)
required_type, = inspect.getfullargspec(factory).annotations.values()
return factory(dataclass_from_dict(required_type, component_config))
ins = inspect.getfullargspec(factory)
required_type = ins.annotations["component_config"]
return factory(component_config=dataclass_from_dict(required_type, component_config))


class ConfigurableDevice(Generic[T_COMPONENT, T_DEVICE_CONFIG, T_COMPONENT_CONFIG], AbstractDevice):
Expand Down
10 changes: 5 additions & 5 deletions packages/modules/kostal_plenticore/device.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
import functools
import logging
from functools import partial
from ipparser import ipparser
from pymodbus.constants import Endian
from typing import Any, Callable, Iterable, List, Union
Expand Down Expand Up @@ -56,10 +56,10 @@ def update_components(
update(components)

tcp_client = modbus.ModbusTcpClient_(device_config.configuration.ip_address, 1502)
reader = functools.partial(tcp_client.read_holding_registers, unit=71, wordorder=Endian.Little)
create_bat_component = functools.partial(KostalPlenticoreBat, device_id=device_config.id, reader=reader)
create_counter_component = functools.partial(KostalPlenticoreCounter, device_id=device_config.id, reader=reader)
create_inverter_component = functools.partial(KostalPlenticoreInverter, reader=reader)
reader = partial(tcp_client.read_holding_registers, unit=71, wordorder=Endian.Little)
create_bat_component = partial(KostalPlenticoreBat, device_id=device_config.id, reader=reader)
create_counter_component = partial(KostalPlenticoreCounter, device_id=device_config.id, reader=reader)
create_inverter_component = partial(KostalPlenticoreInverter, reader=reader)
return ConfigurableDevice(
device_config=device_config,
component_factory=ComponentFactoryByType(
Expand Down

0 comments on commit d989858

Please sign in to comment.