[docs]classLightsController(PLCModule):"""Controller for the light settings."""flag=LightStatusinterval=30.0asyncdef_update_internal(self,use_cache:bool=True,**kwargs):"""Update status."""assertself.flagisnotNonelight_registers=awaitself.plc.modbus.read_group("lights",use_cache=use_cache,)active_bits=self.flag(0)forkeyinlight_registers:if"status"inkeyandlight_registers[key]isTrue:code=key.split("_")[0]active_bits|=CODE_TO_FLAG[code]returnactive_bits
[docs]defget_code(self,light:str):"""Returns the short-form code for a light. Case-insensitive. Parameters ---------- light The light for which the code is seeked. Examples -------- >>> get_code('telescope_red') 'tr' >>> get_code('telescope bright') 'tb' >>> get_code('uMa Room') 'uma' Raises ------ ValueError When a code cannot be found for the input light. """light=light.lower()iflightinCODE_TO_LIGHT:returnlightforcode,descrinCODE_TO_LIGHT.items():iflight==descr:returncodeforreplin[" ","-",""]:iflight==descr.replace("_",repl):returncoderaiseValueError(f"Cannot find matching code for {light!r}.")
[docs]defget_flag(self,light:str):"""Gets the `.LightStatus` flag associated with a light. Parameters ---------- light The light for which the `.LightStatus` a flag is requested. It can be specified in short form (e.g., ``tr``), using underscores (``telescope_red``), or spaces (``telescope red``). The light name is case-insensitive. """code=self.get_code(light)returnCODE_TO_FLAG[code]
[docs]asyncdeftoggle(self,light:str):"""Switches a light."""code=self.get_code(light)log.debug(f"Toggling light {code}.")awaitself.modbus[f"{code}_new"].write(True)awaitasyncio.sleep(0.5)awaitself.update(use_cache=False)
[docs]asyncdefon(self,light:str):"""Turns on a light."""assertself.statusisnotNoneawaitself.update(use_cache=False)flag=self.get_flag(light)ifself.status&flag:returnawaitself.toggle(light)
[docs]asyncdefoff(self,light:str):"""Turns off a light."""assertself.statusisnotNoneawaitself.update(use_cache=False)flag=self.get_flag(light)ifnot(self.status&flag):returnawaitself.toggle(light)