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
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]:
return self.value
return Nucon.get(self)
@value.setter
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:
self.value = new_value
def write(self, new_value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
Nucon.set(self, new_value, force)
@classmethod
def set_base_url(cls, url: str) -> None:
@ -212,21 +212,21 @@ class Nucon(Enum):
return cls.get_multiple(list(cls))
@classmethod
def set(cls, parameter: 'Nucon', value: Union[float, int, bool, str, Enum]) -> None:
if not parameter.is_writable:
def set(cls, parameter: 'Nucon', value: Union[float, int, bool, str, Enum], force: bool = False) -> None:
if not force and not parameter.is_writable:
raise ValueError(f"Parameter {parameter.name} is not writable")
if parameter.enum_type and isinstance(value, parameter.enum_type):
value = value.value
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
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:
raise Exception(f"Failed to set parameter {parameter.name}. Status code: {response.status_code}")
raise E
# Example usage
if __name__ == "__main__":