]> git.graph-it.com Git - graphit/controlpi-statemachine.git/commitdiff
Ruff and Ty linting and bump to 0.4.0 master v0.4.0
authorBenjamin Braatz <bb@bbraatz.eu>
Tue, 31 Mar 2026 19:43:42 +0000 (21:43 +0200)
committerBenjamin Braatz <bb@bbraatz.eu>
Tue, 31 Mar 2026 19:43:42 +0000 (21:43 +0200)
conf.json
controlpi_plugins/statemachine.py
setup.py

index 45773d74878399687d436b21532a5a63b763b0f2..bc2f08ad6bbea9394f0624e6692f6aa339acbb4f 100644 (file)
--- a/conf.json
+++ b/conf.json
@@ -1,7 +1,7 @@
 {
     "Debug": {
         "plugin": "WSServer",
-        "port": 8080,
+        "port": 8888,
         "web": {
             "/": { "module": "controlpi_plugins.wsserver",
                    "location": "Debug" }
index 5cebe470e1a9b5c5b89b92cc20f6abab0a4549cd..aa69f47ea65b92c3dd1f659e63e6e4b49b17b48d 100644 (file)
@@ -8,157 +8,210 @@ class StateMachine(BasePlugin):
     switching states and sending messages defined for these states.
     """
 
-    CONF_SCHEMA = {'properties':
-                   {'init': {'type': 'string'},
-                    'states':
-                    {'type': 'object',
-                     'patternProperties':
-                     {'^[A-Za-z ]+$':
-                      {'type': 'object',
-                       'properties':
-                       {'entry':
-                        {'type': 'array', 'items': {'type': 'object'}},
-                        'exit':
-                        {'type': 'array', 'items': {'type': 'object'}},
-                        'commands':
-                        {'type': 'array', 'items': {'type': 'object'}},
-                        'transitions':
-                        {'type': 'array',
-                         'items':
-                         {'type': 'object',
-                          'properties':
-                          {'trigger': {'type': 'object'},
-                           'to': {'type': 'string'}},
-                          'required': ['trigger', 'to']}}},
-                       'required': ['commands', 'transitions']}},
-                     'additionalProperties': False}},
-                   'required': ['init', 'states']}
+    CONF_SCHEMA = {
+        "properties": {
+            "init": {"type": "string"},
+            "states": {
+                "type": "object",
+                "patternProperties": {
+                    "^[A-Za-z ]+$": {
+                        "type": "object",
+                        "properties": {
+                            "entry": {"type": "array", "items": {"type": "object"}},
+                            "exit": {"type": "array", "items": {"type": "object"}},
+                            "commands": {"type": "array", "items": {"type": "object"}},
+                            "transitions": {
+                                "type": "array",
+                                "items": {
+                                    "type": "object",
+                                    "properties": {
+                                        "trigger": {"type": "object"},
+                                        "to": {"type": "string"},
+                                    },
+                                    "required": ["trigger", "to"],
+                                },
+                            },
+                        },
+                        "required": ["commands", "transitions"],
+                    }
+                },
+                "additionalProperties": False,
+            },
+        },
+        "required": ["init", "states"],
+    }
 
     def process_conf(self) -> None:
         """Register plugin as bus client."""
-        self._state = ''
+        self._state = ""
         sends = []
-        sends.append(MessageTemplate({'event': {'const': 'changed'},
-                                      'state': {'type': 'string'}}))
-        sends.append(MessageTemplate({'state': {'type': 'string'}}))
-        sends.append(MessageTemplate({'init': {'type': 'string'},
-                                      'states':
-                                      {'type': 'object',
-                                       'patternProperties':
-                                       {'^[A-Za-z ]+$':
-                                        {'type': 'object',
-                                         'properties':
-                                         {'commands':
-                                          {'type': 'array',
-                                           'items': {'type': 'object'}},
-                                          'transitions':
-                                          {'type': 'array',
-                                           'items':
-                                           {'type': 'object',
-                                            'properties':
-                                            {'trigger': {'type': 'object'},
-                                             'to': {'type': 'string'}},
-                                            'required': ['trigger', 'to']}}},
-                                         'required': ['commands',
-                                                      'transitions']}},
-                                       'additionalProperties': False}}))
+        sends.append(
+            MessageTemplate(
+                {"event": {"const": "changed"}, "state": {"type": "string"}}
+            )
+        )
+        sends.append(MessageTemplate({"state": {"type": "string"}}))
+        sends.append(
+            MessageTemplate(
+                {
+                    "init": {"type": "string"},
+                    "states": {
+                        "type": "object",
+                        "patternProperties": {
+                            "^[A-Za-z ]+$": {
+                                "type": "object",
+                                "properties": {
+                                    "commands": {
+                                        "type": "array",
+                                        "items": {"type": "object"},
+                                    },
+                                    "transitions": {
+                                        "type": "array",
+                                        "items": {
+                                            "type": "object",
+                                            "properties": {
+                                                "trigger": {"type": "object"},
+                                                "to": {"type": "string"},
+                                            },
+                                            "required": ["trigger", "to"],
+                                        },
+                                    },
+                                },
+                                "required": ["commands", "transitions"],
+                            }
+                        },
+                        "additionalProperties": False,
+                    },
+                }
+            )
+        )
         triggers = []
-        for state in self.conf['states']:
-            commands = self.conf['states'][state]['commands']
+        for state in self.conf["states"]:
+            commands = self.conf["states"][state]["commands"]
             for command in commands:
                 template = MessageTemplate.from_message(command)
                 if template not in sends:
                     sends.append(template)
-            transitions = self.conf['states'][state]['transitions']
+            transitions = self.conf["states"][state]["transitions"]
             for transition in transitions:
-                template = MessageTemplate(transition['trigger'])
+                template = MessageTemplate(transition["trigger"])
                 if template not in triggers:
                     triggers.append(template)
-        self.bus.register(self.name, 'StateMachine', sends,
-                          [([MessageTemplate({'target':
-                                              {'const': self.name},
-                                              'command':
-                                              {'const': 'get state'}})],
-                            self._get_state),
-                           ([MessageTemplate({'target':
-                                              {'const': self.name},
-                                              'command':
-                                              {'const': 'get machine'}})],
-                            self._get_machine),
-                           ([MessageTemplate({'target':
-                                              {'const': self.name},
-                                              'command':
-                                              {'const': 'set state'},
-                                              'new state':
-                                              {'type': 'string'}})],
-                            self._set_state),
-                           (triggers, self._trigger)])
+        self.bus.register(
+            self.name,
+            "StateMachine",
+            sends,
+            [
+                (
+                    [
+                        MessageTemplate(
+                            {
+                                "target": {"const": self.name},
+                                "command": {"const": "get state"},
+                            }
+                        )
+                    ],
+                    self._get_state,
+                ),
+                (
+                    [
+                        MessageTemplate(
+                            {
+                                "target": {"const": self.name},
+                                "command": {"const": "get machine"},
+                            }
+                        )
+                    ],
+                    self._get_machine,
+                ),
+                (
+                    [
+                        MessageTemplate(
+                            {
+                                "target": {"const": self.name},
+                                "command": {"const": "set state"},
+                                "new state": {"type": "string"},
+                            }
+                        )
+                    ],
+                    self._set_state,
+                ),
+                (triggers, self._trigger),
+            ],
+        )
 
     async def _get_state(self, message: Message) -> None:
-        await self.bus.send(Message(self.name,
-                                    {'state': self._state}))
+        await self.bus.send(Message(self.name, {"state": self._state}))
 
     async def _get_machine(self, message: Message) -> None:
-        await self.bus.send(Message(self.name,
-                                    {'init': self.conf['init'],
-                                     'states': self.conf['states']}))
+        await self.bus.send(
+            Message(
+                self.name, {"init": self.conf["init"], "states": self.conf["states"]}
+            )
+        )
 
     async def _set_state(self, message: Message) -> None:
-        if (message['new state'] != self._state and
-                message['new state'] in self.conf['states']):
-            assert isinstance(message['new state'], str)
-            new_state = message['new state']
+        if (
+            message["new state"] != self._state
+            and message["new state"] in self.conf["states"]
+        ):
+            assert isinstance(message["new state"], str)
+            new_state = message["new state"]
             commands = []
-            if 'exit' in self.conf['states'][self._state]:
-                commands += self.conf['states'][self._state]['exit']
+            if "exit" in self.conf["states"][self._state]:
+                commands += self.conf["states"][self._state]["exit"]
             for command in commands:
                 await self.bus.send(Message(self.name, command))
             self._state = new_state
-            await self.bus.send(Message(self.name,
-                                        {'event': 'changed',
-                                         'state': new_state}))
+            await self.bus.send(
+                Message(self.name, {"event": "changed", "state": new_state})
+            )
             commands = []
-            if 'entry' in self.conf['states'][self._state]:
-                commands += self.conf['states'][self._state]['entry']
-            if 'commands' in self.conf['states'][self._state]:
-                commands += self.conf['states'][self._state]['commands']
+            if "entry" in self.conf["states"][self._state]:
+                commands += self.conf["states"][self._state]["entry"]
+            if "commands" in self.conf["states"][self._state]:
+                commands += self.conf["states"][self._state]["commands"]
             for command in commands:
                 await self.bus.send(Message(self.name, command))
 
     async def _trigger(self, message: Message) -> None:
-        transitions = self.conf['states'][self._state]['transitions']
+        transitions = self.conf["states"][self._state]["transitions"]
         for transition in transitions:
-            if (MessageTemplate(transition['trigger']).check(message) and
-                    transition['to'] in self.conf['states']):
-                assert isinstance(transition['to'], str)
-                new_state = transition['to']
+            if (
+                MessageTemplate(transition["trigger"]).check(message)
+                and transition["to"] in self.conf["states"]
+            ):
+                assert isinstance(transition["to"], str)
+                new_state = transition["to"]
                 commands = []
-                if 'exit' in self.conf['states'][self._state]:
-                    commands += self.conf['states'][self._state]['exit']
+                if "exit" in self.conf["states"][self._state]:
+                    commands += self.conf["states"][self._state]["exit"]
                 for command in commands:
                     await self.bus.send(Message(self.name, command))
                 self._state = new_state
-                await self.bus.send(Message(self.name,
-                                            {'event': 'changed',
-                                             'state': new_state,
-                                             'trigger': message}))
+                await self.bus.send(
+                    Message(
+                        self.name,
+                        {"event": "changed", "state": new_state, "trigger": message},
+                    )
+                )
                 commands = []
-                if 'entry' in self.conf['states'][self._state]:
-                    commands += self.conf['states'][self._state]['entry']
-                if 'commands' in self.conf['states'][self._state]:
-                    commands += self.conf['states'][self._state]['commands']
+                if "entry" in self.conf["states"][self._state]:
+                    commands += self.conf["states"][self._state]["entry"]
+                if "commands" in self.conf["states"][self._state]:
+                    commands += self.conf["states"][self._state]["commands"]
                 for command in commands:
                     await self.bus.send(Message(self.name, command))
                 break
 
     async def run(self) -> None:
         """Go into initial state."""
-        if self.conf['init'] in self.conf['states']:
-            self._state = self.conf['init']
+        if self.conf["init"] in self.conf["states"]:
+            self._state = self.conf["init"]
             commands = []
-            if 'entry' in self.conf['states'][self._state]:
-                commands += self.conf['states'][self._state]['entry']
-            if 'commands' in self.conf['states'][self._state]:
-                commands += self.conf['states'][self._state]['commands']
+            if "entry" in self.conf["states"][self._state]:
+                commands += self.conf["states"][self._state]["entry"]
+            if "commands" in self.conf["states"][self._state]:
+                commands += self.conf["states"][self._state]["commands"]
             for command in commands:
                 await self.bus.send(Message(self.name, command))
index e16159ecb2ddc76633bddf6e66f75fd8501b701b..5107b29d67b8f10a74e6b4a62962de08b6aeb332 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -5,7 +5,7 @@ with open("README.md", "r") as readme_file:
 
 setuptools.setup(
     name="controlpi-statemachine",
-    version="0.2.0",
+    version="0.4.0",
     author="Graph-IT GmbH",
     author_email="info@graph-it.com",
     description="ControlPi Plugin for Statemachines",
@@ -13,7 +13,7 @@ setuptools.setup(
     long_description_content_type="text/markdown",
     url="http://docs.graph-it.com/graphit/controlpi-statemachine",
     packages=["controlpi_plugins"],
-    install_requires=[                                                                              
+    install_requires=[
         "controlpi @ git+git://git.graph-it.com/graphit/controlpi.git",
     ],
     classifiers=[