diff --git a/homeassistant/components/fritzbox_callmonitor/base.py b/homeassistant/components/fritzbox_callmonitor/base.py index 0db40e2098f..6114e4b1fed 100644 --- a/homeassistant/components/fritzbox_callmonitor/base.py +++ b/homeassistant/components/fritzbox_callmonitor/base.py @@ -1,4 +1,6 @@ """Base class for fritzbox_callmonitor entities.""" +from __future__ import annotations + from contextlib import suppress from datetime import timedelta import logging @@ -19,18 +21,26 @@ MIN_TIME_PHONEBOOK_UPDATE = timedelta(hours=6) class FritzBoxPhonebook: """This connects to a FritzBox router and downloads its phone book.""" - def __init__(self, host, username, password, phonebook_id, prefixes): + fph: FritzPhonebook + phonebook_dict: dict[str, list[str]] + number_dict: dict[str, str] + + def __init__( + self, + host: str, + username: str, + password: str, + phonebook_id: int | None = None, + prefixes: list[str] | None = None, + ) -> None: """Initialize the class.""" self.host = host self.username = username self.password = password self.phonebook_id = phonebook_id - self.phonebook_dict = None - self.number_dict = None self.prefixes = prefixes - self.fph = None - def init_phonebook(self): + def init_phonebook(self) -> None: """Establish a connection to the FRITZ!Box and check if phonebook_id is valid.""" self.fph = FritzPhonebook( address=self.host, @@ -40,7 +50,7 @@ class FritzBoxPhonebook: self.update_phonebook() @Throttle(MIN_TIME_PHONEBOOK_UPDATE) - def update_phonebook(self): + def update_phonebook(self) -> None: """Update the phone book dictionary.""" if self.phonebook_id is None: return @@ -53,17 +63,15 @@ class FritzBoxPhonebook: } _LOGGER.info("Fritz!Box phone book successfully updated") - def get_phonebook_ids(self): + def get_phonebook_ids(self) -> list[int]: """Return list of phonebook ids.""" return self.fph.phonebook_ids - def get_name(self, number): + def get_name(self, number: str) -> str: """Return a name for a given phone number.""" number = re.sub(REGEX_NUMBER, "", str(number)) - if self.number_dict is None: - return UNKNOWN_NAME - if number in self.number_dict: + with suppress(KeyError): return self.number_dict[number] if not self.prefixes: @@ -74,3 +82,5 @@ class FritzBoxPhonebook: return self.number_dict[prefix + number] with suppress(KeyError): return self.number_dict[prefix + number.lstrip("0")] + + return UNKNOWN_NAME