Skip to content

Commit

Permalink
Remaining fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KapJI committed Sep 18, 2024
1 parent d67c8f3 commit 089f37f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
1 change: 1 addition & 0 deletions glocaltokens/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Glocaltokens library."""
44 changes: 19 additions & 25 deletions glocaltokens/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def __init__(
network_device: NetworkDevice | None = None,
hardware: str | None = None,
):
"""Initializes a Device."""
"""Initialize a Device."""
log_prefix = f"[Device - {device_name}(id={device_id})]"
LOGGER.debug("%s Initializing new Device instance", log_prefix)
self.device_id = device_id
Expand Down Expand Up @@ -111,10 +111,11 @@ def __init__(
self.local_auth_token = local_auth_token

def __str__(self) -> str:
"""Return string representation of the device."""
return str(self.as_dict())

def as_dict(self) -> DeviceDict:
"""Dictionary representation."""
"""Return the dictionary representation of the device."""
return {
"device_id": self.device_id,
"device_name": self.device_name,
Expand All @@ -138,17 +139,14 @@ def __init__(
android_id: str | None = None,
verbose: bool = False,
):
"""Initialize an GLocalAuthenticationTokens instance with google account
credentials
:params
username: google account username;
password: google account password (can be an app password);
master_token: google master token (instead of username/password
combination);
android_id: the id of an android device. Will be randomly generated
if not set;
verbose: whether or not print debug logging information.
"""Initialize a GLocalAuthenticationTokens instance with Google account credentials.
:params
username: Google account username;
password: Google account password (can be an app password);
master_token: Google master token (instead of username/password combination);
android_id: The ID of an Android device. Will be randomly generated if not set;
verbose: Whether or not to print debug logging information.
"""
self.logging_level = logging.DEBUG if verbose else logging.ERROR
LOGGER.setLevel(self.logging_level)
Expand Down Expand Up @@ -208,7 +206,7 @@ def get_android_id(self) -> str:

@staticmethod
def _has_expired(creation_dt: datetime, duration: int) -> bool:
"""Checks if an specified token/object has expired."""
"""Check if an specified token/object has expired."""
return datetime.now().timestamp() - creation_dt.timestamp() > duration

@staticmethod
Expand Down Expand Up @@ -287,7 +285,7 @@ def get_access_token(self) -> str | None:
return self.access_token

def get_homegraph(self, auth_attempts: int = 3) -> GetHomeGraphResponse | None:
"""Returns the entire Google Home Foyer V2 service."""
"""Return the entire Google Home Foyer V2 service."""
if (
self.homegraph is None
or self.homegraph_date is None
Expand Down Expand Up @@ -361,8 +359,7 @@ def get_google_devices(
force_homegraph_reload: bool = False,
discovery_timeout: int = DISCOVERY_TIMEOUT,
) -> list[Device]:
"""Returns a list of google devices with their local authentication tokens,
and IP and ports if set in models_list.
"""Return a list of Google devices with their local authentication tokens, IP, and ports.
models_list: The list of accepted model names.
disable_discovery: Whether or not the device's IP and port should
Expand Down Expand Up @@ -397,11 +394,9 @@ def is_dict_with_valid_ipv4_addresses(data: dict[str, str]) -> bool:
)

if addresses and not is_dict_with_valid_ipv4_addresses(addresses):
# We need to disable flake8-use-fstring because of the brackets,
# it causes a false positive.
LOGGER.error(
"Invalid dictionary structure for addresses dictionary "
"argument. Correct structure is {'device_name': 'ipaddress'}" # noqa
"argument. Correct structure is {'device_name': 'ipaddress'}"
)
return devices

Expand Down Expand Up @@ -492,11 +487,10 @@ def get_google_devices_json(
zeroconf_instance: Zeroconf | None = None,
force_homegraph_reload: bool = False,
) -> str:
"""Returns a json list of google devices with their local authentication tokens,
and IP and ports if set in models_list.
"""Return a JSON list of devices with authentication tokens, IPs, and ports if models set.
models_list: The list of accepted model names.
indent: The indentation for the json formatting.
indent: The indentation for the JSON formatting.
disable_discovery: Whether or not the device's IP and port should
be searched for in the network.
addresses: Dict of network devices from the local network
Expand All @@ -517,18 +511,18 @@ def get_google_devices_json(
return json.dumps([obj.as_dict() for obj in google_devices], indent=indent)

def invalidate_access_token(self) -> None:
"""Invalidates the current access token."""
"""Invalidate the current access token."""
self.access_token = None
self.access_token_date = None
LOGGER.debug("Invalidated access_token")

def invalidate_master_token(self) -> None:
"""Invalidates the current master token."""
"""Invalidate the current master token."""
self.master_token = None
LOGGER.debug("Invalidated master_token")

def invalidate_homegraph(self) -> None:
"""Invalidates the stored homegraph data."""
"""Invalidate the stored homegraph data."""
self.homegraph = None
self.homegraph_date = None
LOGGER.debug("Invalidated homegraph")
6 changes: 4 additions & 2 deletions glocaltokens/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class NetworkDevice(NamedTuple):

class CastListener(ServiceListener):
"""Zeroconf Cast Services collection.
Credit (pychromecast):
https://github.com/home-assistant-libs/pychromecast/.
"""
Expand All @@ -39,6 +40,7 @@ def __init__(
remove_callback: Callable[[], None] | None = None,
update_callback: Callable[[], None] | None = None,
):
"""Create cast listener."""
self.devices: dict[str, NetworkDevice] = {}
self.add_callback = add_callback
self.remove_callback = remove_callback
Expand All @@ -60,7 +62,7 @@ def update_service(self, zc: Zeroconf, type_: str, name: str) -> None:
self._add_update_service(zc, type_, name, self.update_callback)

def remove_service(self, _zc: Zeroconf, type_: str, name: str) -> None:
"""Called when a cast has been lost (mDNS info expired or host down)."""
"""Remove a cast device when its mDNS info expires or the host is down."""
LOGGER.debug("remove_service %s, %s", type_, name)
if name in self.devices:
del self.devices[name]
Expand Down Expand Up @@ -154,7 +156,7 @@ def discover_devices(
LOGGER.debug("Discovering devices...")

def callback() -> None:
"""Called when zeroconf has discovered a new device."""
"""Handle the event when zeroconf discovers a new device."""
if max_devices is not None and listener.count >= max_devices:
discovery_complete.set()

Expand Down
1 change: 1 addition & 0 deletions glocaltokens/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Utils package."""
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ ignore = [

[tool.ruff.lint.per-file-ignores]
"tests/test_client.py" = ["SLF001"]
"example/*.py" = ["INP001"]

[tool.ruff.lint.isort]
force-sort-within-sections = true
Expand Down Expand Up @@ -189,7 +190,8 @@ good-names = [
]

[tool.pylint.format]
max-line-length = 88
expected-line-ending-format = "LF"
max-line-length = 100
min-similarity-lines = 7

[tool.pylint.messages_control]
Expand Down

0 comments on commit 089f37f

Please sign in to comment.