37 lines
1.2 KiB
Python
Executable File
37 lines
1.2 KiB
Python
Executable File
from enum import Enum
|
|
from typing import Any
|
|
|
|
from aare.devices.set_get_pv import SetGetPV, MoveResult
|
|
|
|
|
|
class EnumPV(SetGetPV):
|
|
def __init__(self, name: str, setpv: str, getpv: str, **kwargs):
|
|
super().__init__(name, setpv, getpv, **kwargs)
|
|
if not self.setpoint_pv.enum_strs:
|
|
raise RuntimeError(f"{setpv} is not an ENUM PV")
|
|
|
|
@property
|
|
def position(self) -> str:
|
|
return self.readback_pv.get(as_string=True)
|
|
|
|
def _resolve(self, x: Any) -> MoveResult:
|
|
# accept Enum member
|
|
if isinstance(x, Enum):
|
|
x = x.name
|
|
|
|
# accept index
|
|
if isinstance(x, int):
|
|
try:
|
|
return MoveResult(target=self.setpoint_pv.enum_strs[x], name=None)
|
|
except Exception as e:
|
|
raise ValueError(f"Bad enum index {x}") from e
|
|
|
|
# accept name -> match against enum strings (case-insensitive)
|
|
if isinstance(x, str):
|
|
for s in self.setpoint_pv.enum_strs:
|
|
if s.strip().lower() == x.strip().lower():
|
|
return MoveResult(target=s, name=s)
|
|
raise ValueError(f"'{x}' not in {list(self.setpoint_pv.enum_strs)}")
|
|
|
|
raise TypeError(f"Unsupported enum command type: {type(x).__name__}")
|