Add fritzbox_callmonitor type hints (1) (#70292)

pull/70289/head
Marc Mueller 2022-04-25 14:28:17 +02:00 committed by GitHub
parent d045e8678d
commit c467f318ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 11 deletions

View File

@ -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