Use f-string literal.

DoorController init call has now named parameters.
This commit is contained in:
Valdur Kana 2022-03-11 19:48:52 +02:00
parent b7f3f57efd
commit 7cc479f468
2 changed files with 16 additions and 20 deletions

View File

@ -69,14 +69,13 @@ class DoorController:
def wiegand_callback(self, bits, value): def wiegand_callback(self, bits, value):
uid_h = hash_uid(value, self.uid_hash) uid_h = hash_uid(value, self.uid_hash)
logging.debug("hash %s", uid_h) logging.debug(f"hash {uid_h}")
if uid_h in self.uids: if uid_h in self.uids:
logging.info("Opening door for UID hash trail %s", uid_h[-10:]) logging.info(f"Opening door for UID hash trail {uid_h[-10:]}")
self.wiegand.open_door() self.wiegand.open_door()
success = True success = True
else: else:
logging.info("Access card not in allow list, hash trail %s", logging.info(f"Access card not in allow list, hash trail {uid_h[-10:]}")
uid_h[-10:])
success = False success = False
data = { data = {
"uid": value, "uid": value,
@ -127,9 +126,10 @@ class DoorController:
if __name__ == "__main__": if __name__ == "__main__":
DoorController( DoorController(
os.environ["KDOORPI_DOOR"], door=os.environ["KDOORPI_DOOR"],
os.environ["KDOORPI_API_ALLOWED"], api_allowed=os.environ["KDOORPI_API_ALLOWED"],
os.environ["KDOORPI_API_LONGPOLL"], api_longpoll=os.environ["KDOORPI_API_LONGPOLL"],
os.environ["KDOORPI_API_SWIPE"], api_swipe=os.environ["KDOORPI_API_SWIPE"],
os.environ["KDOORPI_API_KEY"], api_key=os.environ["KDOORPI_API_KEY"],
os.environ["KDOORPI_UID_SALT"]) uid_hash=os.environ["KDOORPI_UID_SALT"],
)

View File

@ -79,12 +79,11 @@ class Decoder:
bits))).rstrip() bits))).rstrip()
except ValueError: except ValueError:
logging.error("Failed to convert binary to hex: %s" % self.items) logging.error(f"Failed to convert binary to hex: {self.items}")
return False return False
except Exception as e: except Exception as e:
logging.error("Wiegand convert error (raw: %s): %s" % ( logging.error(f"Wiegand convert error (raw: {self.items}): {e}")
self.items, e))
return False return False
def _cb(self, gpio_pin, level, tick): def _cb(self, gpio_pin, level, tick):
@ -99,7 +98,7 @@ class Decoder:
if self.in_code: if self.in_code:
self.bits += 1 self.bits += 1
else: else:
logging.debug("Wiegand data transfer start") logging.debug(f"Wiegand data transfer start")
self.bits = 1 self.bits = 1
self.items = "" self.items = ""
self.in_code = True self.in_code = True
@ -133,16 +132,13 @@ class Decoder:
if hex: if hex:
self.callback(self.bits, hex) self.callback(self.bits, hex)
else: else:
logging.error("Expected 26 bits, but got %i: %s" % logging.error(f"Expected 26 bits, but got {self.bits:d}: {self.items}")
(self.bits, self.items))
except Exception as e: except Exception as e:
logging.error("Wiegand callback error: %s" % e) logging.error(f"Wiegand callback error: {e}")
def button_cb(self, gpio_pin, level, tick): def button_cb(self, gpio_pin, level, tick):
print("button: gpio_pin:{}, level:{}, tick:{}".format(gpio_pin, print(f"button: gpio_pin:{gpio_pin}, level:{level}, tick:{tick}")
level,
tick))
def open_door(self): def open_door(self):
self.pi.write(self.door_pin, 1) self.pi.write(self.door_pin, 1)