diff --git a/nucon/core.py b/nucon/core.py index 6351016..8df8bcf 100644 --- a/nucon/core.py +++ b/nucon/core.py @@ -130,10 +130,12 @@ class Nucon(Enum): STEAM_TURBINE_1_PRESSURE = ("STEAM_TURBINE_1_PRESSURE", float, False) STEAM_TURBINE_2_PRESSURE = ("STEAM_TURBINE_2_PRESSURE", float, False) - def __init__(self, id: str, param_type: Type, is_writable: bool): + def __init__(self, id: str, param_type: Type, is_writable: bool, min_val: Optional[Union[int, float]] = None, max_val: Optional[Union[int, float]] = None): self.id = id self.param_type = param_type self.is_writable = is_writable + self.min_val = min_val + self.max_val = max_val def __str__(self): return self.id @@ -156,6 +158,17 @@ class Nucon(Enum): def write(self, new_value: Union[float, int, bool, str, Enum], force: bool = False) -> None: Nucon.set(self, new_value, force) + def check_in_range(self, value: Union[int, float], raise_on_oob=False) -> None: + if self.min_val is not None and value < self.min_val: + if raise_on_oob: + raise ValueError(f"Value {value} is below the minimum allowed value {self.min_val} for {self.name}") + return False + if self.max_val is not None and value > self.max_val: + if raise_on_oob: + raise ValueError(f"Value {value} is above the maximum allowed value {self.max_val} for {self.name}") + return False + return True + @classmethod def set_base_url(cls, url: str) -> None: NuconConfig.base_url = url @@ -220,11 +233,16 @@ class Nucon(Enum): @classmethod def set(cls, parameter: Union['Nucon', str], value: Union[float, int, bool, str, Enum], force: bool = False) -> None: if isinstance(parameter, str): - parameter = cls[parameter] + parameter = next((param for param in cls if param.id == parameter), None) + if parameter is None: + raise ValueError(f"No parameter found with id '{parameter}'") if not force and not parameter.is_writable: raise ValueError(f"Parameter {parameter.name} is not writable") + if not force: + parameter.check_in_range(value, raise_on_oob=True) + if parameter.enum_type and isinstance(value, parameter.enum_type): value = value.value @@ -237,7 +255,6 @@ class Nucon(Enum): if response.status_code != 200: raise Exception(f"Failed to set parameter {parameter.name}. Status code: {response.status_code}") - # Example usage if __name__ == "__main__": # Enable dummy mode for testing