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

Make modbus available to plugins #211

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Make modbus available to plugins #211

wants to merge 3 commits into from

Conversation

belowm
Copy link

@belowm belowm commented Nov 12, 2024

This allows plugins to access the modbus by adding DeyeModbus to PluginContext.

@kbialek
Copy link
Owner

kbialek commented Nov 12, 2024

What do you want to use modbus for in the plugin?

@belowm
Copy link
Author

belowm commented Nov 14, 2024

What do you want to use modbus for in the plugin?

Because I'd like to mutate a specific modbus register in my plugin:

class DeyeGridChargeCurrentEventProcessor(DeyeEventProcessor):
    def __init__(
        self, logger_config: DeyeLoggerConfig, mqtt_client: DeyeMqttClient, modbus: DeyeModbus
    ):
        self.__log = logger_config.logger_adapter(logging.getLogger(DeyeGridChargeCurrentEventProcessor.__name__))
        self.__logger_config = logger_config
        self.__mqtt_client = mqtt_client
        self.__modbus = modbus
        self.__gridcharge_current_topic_suffix = "settings/gridcharge_current"

    def get_id(self):
        return "gridcharge_current"

    def get_description(self):
        return "Gridcharge Current over MQTT"

    def initialize(self):
        self.__mqtt_client.subscribe_command_handler(
            self.__logger_config.index, self.__gridcharge_current_topic_suffix, self.handle_command
        )

    def handle_command(self, client: Client, userdata, msg: MQTTMessage):
        try:
            gridcharge_current = int(msg.payload)
        except ValueError:
            self.__log.error("Invalid Gridcharge Current value: %s", msg.payload)
            return

        if gridcharge_current not in range(161):
            self.__log.error("Given Gridcharge Current invalid: %f", gridcharge_current)
            return

        self.__log.info("Setting Gridcharge Current to %f", gridcharge_current)

        if self.__modbus.write_register_uint(int(128), gridcharge_current):
            self.__log.info("Register updated")
        else:
            self.__log.error("Updating register failed")

class DeyePlugin:
    def __init__(self, plugin_context: DeyePluginContext):
        self.processor = DeyeGridChargeCurrentEventProcessor(plugin_context.config.logger_configs[0], plugin_context.mqtt_client, plugin_context.modbus)

    def get_event_processors(self) -> [DeyeEventProcessor]:
        return [self.processor]

I'm not much of a python guy, maybe there's a better way to archive this? :)

@@ -28,21 +28,21 @@
from deye_sensor import Sensor
from deye_plugin_loader import DeyePluginContext, DeyePluginLoader
from deye_multi_inverter_data_aggregator import DeyeMultiInverterDataAggregator

from deye_modbus import DeyeModbus
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@belowm Remove this extra import, otherwise the PR check will not pass and will block the merge of the PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants