start on type hints
This commit is contained in:
parent
8769240b93
commit
99ba23e3e6
1 changed files with 15 additions and 9 deletions
|
@ -3,12 +3,16 @@
|
|||
# Copyright 2024, Paul Warren <pwarren@pwarren.id.au>
|
||||
# Licensed under AGPLv3, See LICENSE.md for terms
|
||||
|
||||
from typing import Any
|
||||
|
||||
from pymodbus.constants import Endian
|
||||
from pymodbus.payload import BinaryPayloadDecoder, BinaryPayloadBuilder
|
||||
|
||||
from pymodbus.client.base import ModbusBaseClient
|
||||
|
||||
class DataType:
|
||||
def __init__(self, width, decode, add):
|
||||
width: int
|
||||
|
||||
self._width = width
|
||||
self._decode = decode
|
||||
self._add = add
|
||||
|
@ -30,28 +34,28 @@ class DataType:
|
|||
|
||||
|
||||
# helper functions for DataType constructors
|
||||
def decode_string8(decoder):
|
||||
def decode_string8(decoder) -> str:
|
||||
try:
|
||||
return str(decoder.decode_string(8).decode("utf-8"))
|
||||
except UnicodeDecodeError:
|
||||
return decoder.decode_string(8)
|
||||
|
||||
|
||||
def decode_string16(decoder):
|
||||
def decode_string16(decoder) -> str:
|
||||
try:
|
||||
return str(decoder.decode_string(16).decode("utf-8"))
|
||||
except UnicodeDecodeError:
|
||||
return decoder.decode_string(16)
|
||||
|
||||
|
||||
def decode_string32(decoder):
|
||||
def decode_string32(decoder) -> str:
|
||||
try:
|
||||
return str(decoder.decode_string(32).decode("utf-8"))
|
||||
except UnicodeDecodeError:
|
||||
return decoder.decode_string(32)
|
||||
|
||||
|
||||
def encode_16bit_int(encoder, value):
|
||||
def encode_16bit_int(encoder, value) -> int:
|
||||
return encoder.add_16bit_int(int(value))
|
||||
|
||||
|
||||
|
@ -124,16 +128,18 @@ class FroniusReg:
|
|||
|
||||
|
||||
class ScaledFroniusReg:
|
||||
def __init__(self, value_register, scale_register):
|
||||
def __init__(self, value_register: FroniusReg, scale_register: FroniusReg):
|
||||
"A nicer way of dealing with scaled registers"
|
||||
self.value_register = value_register
|
||||
self.scale_register = scale_register
|
||||
|
||||
def get(self, modbus_client):
|
||||
def get(self, modbus_client: ModbusBaseClient) -> float:
|
||||
"Return the value_register's value, scaled by scale_register."
|
||||
return self.value_register.get(modbus_client) * 10 ** self.scale_register.get(
|
||||
modbus_client
|
||||
)
|
||||
|
||||
def set(self, modbus_client, value):
|
||||
def set(self, modbus_client: ModbusBaseClient, value: [ int, float, str ]):
|
||||
return self.value_register.set(
|
||||
modbus_client, value / 10 ** self.scale_register.get(modbus_client)
|
||||
modbus_client, value / 10 ** self.scale_register.get(modbus_client),
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue