Exemplo n.º 1
0
class FritzBoxPhonebook:
    """This connects to a FritzBox router and downloads its phone book."""
    def __init__(self, host, username, password, phonebook_id, prefixes):
        """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):
        """Establish a connection to the FRITZ!Box and check if phonebook_id is valid."""
        self.fph = FritzPhonebook(
            address=self.host,
            user=self.username,
            password=self.password,
        )
        self.update_phonebook()

    @Throttle(MIN_TIME_PHONEBOOK_UPDATE)
    def update_phonebook(self):
        """Update the phone book dictionary."""
        if self.phonebook_id is None:
            return

        self.phonebook_dict = self.fph.get_all_names(self.phonebook_id)
        self.number_dict = {
            re.sub(REGEX_NUMBER, "", nr): name
            for name, nrs in self.phonebook_dict.items() for nr in nrs
        }
        _LOGGER.info("Fritz!Box phone book successfully updated")

    def get_phonebook_ids(self):
        """Return list of phonebook ids."""
        return self.fph.phonebook_ids

    def get_name(self, number):
        """Return a name for a given phone number."""
        number = re.sub(REGEX_NUMBER, "", str(number))
        if self.number_dict is None:
            return UNKOWN_NAME

        if number in self.number_dict:
            return self.number_dict[number]

        if not self.prefixes:
            return UNKOWN_NAME

        for prefix in self.prefixes:
            try:
                return self.number_dict[prefix + number]
            except KeyError:
                pass
            try:
                return self.number_dict[prefix + number.lstrip("0")]
            except KeyError:
                pass
Exemplo n.º 2
0
class FritzBoxPhonebook:
    """This connects to a FritzBox router and downloads its phone book."""
    def __init__(self,
                 host,
                 port,
                 username,
                 password,
                 phonebook_id=0,
                 prefixes=None):
        """Initialize the class."""
        self.host = host
        self.username = username
        self.password = password
        self.port = port
        self.phonebook_id = phonebook_id
        self.phonebook_dict = None
        self.number_dict = None
        self.prefixes = prefixes or []

        # Establish a connection to the FRITZ!Box.
        self.fph = FritzPhonebook(address=self.host,
                                  user=self.username,
                                  password=self.password)

        if self.phonebook_id not in self.fph.list_phonebooks:
            raise ValueError("Phonebook with this ID not found.")

        self.update_phonebook()

    @Throttle(MIN_TIME_PHONEBOOK_UPDATE)
    def update_phonebook(self):
        """Update the phone book dictionary."""
        self.phonebook_dict = self.fph.get_all_names(self.phonebook_id)
        self.number_dict = {
            re.sub(r"[^\d\+]", "", nr): name
            for name, nrs in self.phonebook_dict.items() for nr in nrs
        }
        _LOGGER.info("Fritz!Box phone book successfully updated")

    def get_name(self, number):
        """Return a name for a given phone number."""
        number = re.sub(r"[^\d\+]", "", str(number))
        if self.number_dict is None:
            return "unknown"
        try:
            return self.number_dict[number]
        except KeyError:
            pass
        if self.prefixes:
            for prefix in self.prefixes:
                try:
                    return self.number_dict[prefix + number]
                except KeyError:
                    pass
                try:
                    return self.number_dict[prefix + number.lstrip("0")]
                except KeyError:
                    pass
        return "unknown"
Exemplo n.º 3
0
def get_known_phonebook_numbers(config):
    fp = FritzPhonebook(address=config["fritz"]["address"],
                        password=config["fritz"]["password"])

    number_set = set()
    for phonebook_id in fp.phonebook_ids:
        contacts = fp.get_all_names(phonebook_id)
        for name, numbers in contacts.items():
            for number in numbers:
                number_set.add(
                    phonenumber.normalize(number,
                                          config["phone-default-region"]))
    return number_set
Exemplo n.º 4
0
class FritzBoxPhonebook:
    """This connects to a FritzBox router and downloads its phone book."""

    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.prefixes = prefixes

    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,
            user=self.username,
            password=self.password,
        )
        self.update_phonebook()

    @Throttle(MIN_TIME_PHONEBOOK_UPDATE)
    def update_phonebook(self) -> None:
        """Update the phone book dictionary."""
        if self.phonebook_id is None:
            return

        self.phonebook_dict = self.fph.get_all_names(self.phonebook_id)
        self.number_dict = {
            re.sub(REGEX_NUMBER, "", nr): name
            for name, nrs in self.phonebook_dict.items() for nr in nrs
        }
        _LOGGER.info("Fritz!Box phone book successfully updated")

    def get_phonebook_ids(self) -> list[int]:
        """Return list of phonebook ids."""
        return self.fph.phonebook_ids  # type: ignore[no-any-return]

    def get_name(self, number: str) -> str:
        """Return a name for a given phone number."""
        number = re.sub(REGEX_NUMBER, "", str(number))

        with suppress(KeyError):
            return self.number_dict[number]

        if not self.prefixes:
            return UNKNOWN_NAME

        for prefix in self.prefixes:
            with suppress(KeyError):
                return self.number_dict[prefix + number]
            with suppress(KeyError):
                return self.number_dict[prefix + number.lstrip("0")]

        return UNKNOWN_NAME