"""Helper functions for the aiokem package."""from__future__importannotationsfromdatetimeimportdatetime,tzinfofromtypingimportAny
[docs]defreverse_mac_address(mac:str)->str:"""Reverse the bytes of a MAC address."""# Split the MAC address into individual bytesmac_bytes=mac.split(":")# Reverse the order of the bytesreversed_bytes=mac_bytes[::-1]# Join the reversed bytes back into a MAC address stringreversed_mac=":".join(reversed_bytes)returnreversed_mac
[docs]defconvert_timestamp(response:dict[str,Any],key:str,tz:tzinfo)->None:"""Convert a timestamp that does not have a tz in to the specified timezone."""value=response.get(key)ifvalue:dt=datetime.fromisoformat(value)# Different controllers can return timestamps with or without tzinfo# If tzinfo is None, we need to set it to the provided tzifdt.tzinfoisNone:dt=dt.replace(tzinfo=tz)response[key]=dt.isoformat()
[docs]defconvert_number_abs(response:dict[str,Any],key:str)->None:"""Convert a number to its absolute value."""value=response.get(key)ifvalueisnotNone:response[key]=abs(value)