Allow force writing values

This commit is contained in:
Dominik Moritz Roth 2024-10-02 16:51:12 +02:00
parent 312de01f44
commit 2c58571d62

View File

@ -141,17 +141,17 @@ class Nucon(Enum):
@property @property
def value(self) -> Union[float, int, bool, str, Enum]: def value(self) -> Union[float, int, bool, str, Enum]:
return Nucon.get(self) return self.read()
def read(self) -> Union[float, int, bool, str, Enum]: def read(self) -> Union[float, int, bool, str, Enum]:
return self.value return Nucon.get(self)
@value.setter @value.setter
def value(self, new_value: Union[float, int, bool, str, Enum]) -> None: def value(self, new_value: Union[float, int, bool, str, Enum]) -> None:
Nucon.set(self, new_value) self.write(new_value)
def write(self, new_value: Union[float, int, bool, str, Enum]) -> None: def write(self, new_value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
self.value = new_value Nucon.set(self, new_value, force)
@classmethod @classmethod
def set_base_url(cls, url: str) -> None: def set_base_url(cls, url: str) -> None:
@ -212,21 +212,21 @@ class Nucon(Enum):
return cls.get_multiple(list(cls)) return cls.get_multiple(list(cls))
@classmethod @classmethod
def set(cls, parameter: 'Nucon', value: Union[float, int, bool, str, Enum]) -> None: def set(cls, parameter: 'Nucon', value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
if not parameter.is_writable: if not force and not parameter.is_writable:
raise ValueError(f"Parameter {parameter.name} is not writable") raise ValueError(f"Parameter {parameter.name} is not writable")
if parameter.enum_type and isinstance(value, parameter.enum_type): if parameter.enum_type and isinstance(value, parameter.enum_type):
value = value.value value = value.value
if NuconConfig.dummy_mode: if NuconConfig.dummy_mode:
print(f"Dummy mode: Setting {parameter.name} to {value}") print(f"Dummy mode: {'Force ' if force else ''}Setting {parameter.name} to {value}")
return return
response = requests.post(NuconConfig.base_url, params={"variable": parameter.name, "value": str(value)}) response = requests.post(NuconConfig.base_url, params={"variable": parameter.name, "value": str(value), "force": str(force).lower()})
if response.status_code != 200: if response.status_code != 200:
raise Exception(f"Failed to set parameter {parameter.name}. Status code: {response.status_code}") raise E
# Example usage # Example usage
if __name__ == "__main__": if __name__ == "__main__":