Skip to content

Commit

Permalink
Merge pull request #61 from Snuffy2/Move-redact-to-local-function
Browse files Browse the repository at this point in the history
Move redact to internal function
  • Loading branch information
Snuffy2 authored Sep 19, 2024
2 parents 5ad9517 + d572510 commit 3d1d2e4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 72 deletions.
8 changes: 4 additions & 4 deletions custom_components/resmed_myair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers.redact import async_redact_data

from .const import CONF_REGION, DOMAIN, KEYS_TO_REDACT, REGION_NA, VERSION
from .const import CONF_REGION, DOMAIN, REGION_NA, VERSION
from .helpers import redact_dict

_LOGGER: logging.Logger = logging.getLogger(__name__)
PLATFORMS: list[str] = [Platform.SENSOR]
Expand All @@ -23,7 +23,7 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
"""Set up from a config entry."""
_LOGGER.info(f"Starting ResMed myAir Integration Version: {VERSION}")
_LOGGER.debug(
f"[init async_setup_entry] config_entry.data: {async_redact_data(config_entry.data, KEYS_TO_REDACT)}"
f"[init async_setup_entry] config_entry.data: {redact_dict(config_entry.data)}"
)
hass.data.setdefault(DOMAIN, {})
hass_data: dict[str, Any] = dict(config_entry.data)
Expand Down Expand Up @@ -53,7 +53,7 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry) -> bool:

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload a config entry."""
_LOGGER.info(f"Unloading: {async_redact_data(entry.data, KEYS_TO_REDACT)}")
_LOGGER.info(f"Unloading: {redact_dict(entry.data)}")
unload_ok: bool = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id, None)
Expand Down
59 changes: 29 additions & 30 deletions custom_components/resmed_myair/client/rest_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

from aiohttp import ClientResponse, ClientSession
from aiohttp.http_exceptions import HttpProcessingError
from homeassistant.helpers.redact import async_redact_data
import jwt

from custom_components.resmed_myair.const import (
AUTH_NEEDS_MFA,
AUTHN_SUCCESS,
KEYS_TO_REDACT,
REGION_NA,
)
from custom_components.resmed_myair.helpers import redact_dict

from .myair_client import (
AuthenticationError,
Expand Down Expand Up @@ -94,7 +93,7 @@ class RESTClient(MyAirClient):

def __init__(self, config: MyAirConfig, session: ClientSession) -> None:
_LOGGER.debug(
f"[RESTClient init] config: {async_redact_data(config._asdict(), KEYS_TO_REDACT)}"
f"[RESTClient init] config: {redact_dict(config._asdict())}"
)
self._config: MyAirConfig = config
self._session: ClientSession = session
Expand Down Expand Up @@ -175,7 +174,7 @@ async def is_email_verified(self) -> bool:

_LOGGER.debug(f"[is_email_verified] authorize_url: {userinfo_url}")
_LOGGER.debug(
f"[is_email_verified] headers: {async_redact_data(headers, KEYS_TO_REDACT)}"
f"[is_email_verified] headers: {redact_dict(headers)}"
)

async with self._session.get(
Expand All @@ -186,7 +185,7 @@ async def is_email_verified(self) -> bool:
_LOGGER.debug(f"[is_email_verified] userinfo_res: {userinfo_res}")
userinfo_dict: dict[str, Any] = await userinfo_res.json()
_LOGGER.debug(
f"[is_email_verified] introspect_dict: {async_redact_data(userinfo_dict, KEYS_TO_REDACT)}"
f"[is_email_verified] introspect_dict: {redact_dict(userinfo_dict)}"
)
await self._resmed_response_error_check(
"userinfo_query", userinfo_res, userinfo_dict
Expand Down Expand Up @@ -222,7 +221,7 @@ async def _get_initial_dt(self) -> None:
)
_LOGGER.debug(f"[get_initial_dt] initial_dt_url: {initial_dt_url}")
_LOGGER.debug(
f"[get_initial_dt] headers: {async_redact_data(self._json_headers, KEYS_TO_REDACT)}"
f"[get_initial_dt] headers: {redact_dict(self._json_headers)}"
)

async with self._session.get(
Expand Down Expand Up @@ -253,10 +252,10 @@ async def _is_access_token_active(self) -> bool:
}
_LOGGER.debug(f"[is_access_token_active] introspect_url: {introspect_url}")
_LOGGER.debug(
f"[is_access_token_active] headers: {async_redact_data(headers, KEYS_TO_REDACT)}"
f"[is_access_token_active] headers: {redact_dict(headers)}"
)
_LOGGER.debug(
f"[is_access_token_active] introspect_query: {async_redact_data(introspect_query, KEYS_TO_REDACT)}"
f"[is_access_token_active] introspect_query: {redact_dict(introspect_query)}"
)

async with self._session.post(
Expand All @@ -265,7 +264,7 @@ async def _is_access_token_active(self) -> bool:
_LOGGER.debug(f"[is_access_token_active] introspect_res: {introspect_res}")
introspect_dict: dict[str, Any] = await introspect_res.json()
_LOGGER.debug(
f"[is_access_token_active] introspect_dict: {async_redact_data(introspect_dict, KEYS_TO_REDACT)}"
f"[is_access_token_active] introspect_dict: {redact_dict(introspect_dict)}"
)
await self._resmed_response_error_check(
"introspect_query", introspect_res, introspect_dict
Expand Down Expand Up @@ -312,10 +311,10 @@ async def _authn_check(self) -> str:
}
_LOGGER.debug(f"[authn_check] authn_url: {authn_url}")
_LOGGER.debug(
f"[authn_check] headers: {async_redact_data(self._json_headers, KEYS_TO_REDACT)}"
f"[authn_check] headers: {redact_dict(self._json_headers)}"
)
_LOGGER.debug(
f"[authn_check] json_query: {async_redact_data(json_query, KEYS_TO_REDACT)}"
f"[authn_check] json_query: {redact_dict(json_query)}"
)

async with self._session.post(
Expand All @@ -327,7 +326,7 @@ async def _authn_check(self) -> str:
_LOGGER.debug(f"[authn_check] authn_res: {authn_res}")
authn_dict: dict[str, Any] = await authn_res.json()
_LOGGER.debug(
f"[authn_check] authn_dict: {async_redact_data(authn_dict, KEYS_TO_REDACT)}"
f"[authn_check] authn_dict: {redact_dict(authn_dict)}"
)
await self._resmed_response_error_check("authn", authn_res, authn_dict)
if "status" not in authn_dict:
Expand Down Expand Up @@ -364,10 +363,10 @@ async def _trigger_mfa(self) -> None:
json_query: dict[str, Any] = {"passCode": "", "stateToken": self._state_token}
_LOGGER.debug(f"[trigger_mfa] mfa_url: {self._mfa_url}")
_LOGGER.debug(
f"[trigger_mfa] headers: {async_redact_data(self._json_headers, KEYS_TO_REDACT)}"
f"[trigger_mfa] headers: {redact_dict(self._json_headers)}"
)
_LOGGER.debug(
f"[trigger_mfa] json_query: {async_redact_data(json_query, KEYS_TO_REDACT)}"
f"[trigger_mfa] json_query: {redact_dict(json_query)}"
)

async with self._session.post(
Expand All @@ -379,7 +378,7 @@ async def _trigger_mfa(self) -> None:
_LOGGER.debug(f"[trigger_mfa] trigger_mfa_res: {trigger_mfa_res}")
trigger_mfa_dict: dict[str, Any] = await trigger_mfa_res.json()
_LOGGER.debug(
f"[trigger_mfa] trigger_mfa_dict: {async_redact_data(trigger_mfa_dict, KEYS_TO_REDACT)}"
f"[trigger_mfa] trigger_mfa_dict: {redact_dict(trigger_mfa_dict)}"
)
await self._resmed_response_error_check(
"trigger_mfa", trigger_mfa_res, trigger_mfa_dict
Expand All @@ -392,7 +391,7 @@ async def _verify_mfa(self, verification_code: str) -> str:
json_query: dict[str, Any] = {"passCode": verification_code, "stateToken": self._state_token}
_LOGGER.debug(f"[verify_mfa] mfa_url: {self._mfa_url}")
_LOGGER.debug(
f"[verify_mfa] headers: {async_redact_data(self._json_headers, KEYS_TO_REDACT)}"
f"[verify_mfa] headers: {redact_dict(self._json_headers)}"
)
_LOGGER.debug(f"[verify_mfa] json_query: {json_query}")

Expand All @@ -405,7 +404,7 @@ async def _verify_mfa(self, verification_code: str) -> str:
_LOGGER.debug(f"[verify_mfa] verify_mfa_res: {verify_mfa_res}")
verify_mfa_dict: dict[str, Any] = await verify_mfa_res.json()
_LOGGER.debug(
f"[verify_mfa] verify_mfa_dict: {async_redact_data(verify_mfa_dict, KEYS_TO_REDACT)}"
f"[verify_mfa] verify_mfa_dict: {redact_dict(verify_mfa_dict)}"
)
await self._resmed_response_error_check(
"verify_mfa", verify_mfa_res, verify_mfa_dict
Expand Down Expand Up @@ -455,10 +454,10 @@ async def _get_access_token(self) -> None:
}
_LOGGER.debug(f"[get_access_token code] authorize_url: {authorize_url}")
_LOGGER.debug(
f"[get_access_token code] headers: {async_redact_data(self._json_headers, KEYS_TO_REDACT)}"
f"[get_access_token code] headers: {redact_dict(self._json_headers)}"
)
_LOGGER.debug(
f"[get_access_token code] params_query: {async_redact_data(params_query, KEYS_TO_REDACT)}"
f"[get_access_token code] params_query: {redact_dict(params_query)}"
)

async with self._session.get(
Expand Down Expand Up @@ -501,10 +500,10 @@ async def _get_access_token(self) -> None:
)
_LOGGER.debug(f"[get_access_token token] token_url: {token_url}")
_LOGGER.debug(
f"[get_access_token token] headers: {async_redact_data(headers, KEYS_TO_REDACT)}"
f"[get_access_token token] headers: {redact_dict(headers)}"
)
_LOGGER.debug(
f"[get_access_token token] token_query: {async_redact_data(token_query, KEYS_TO_REDACT)}"
f"[get_access_token token] token_query: {redact_dict(token_query)}"
)

async with self._session.post(
Expand All @@ -517,7 +516,7 @@ async def _get_access_token(self) -> None:
_LOGGER.debug(f"[get_access_token] token_res: {token_res}")
token_dict: dict[str, Any] = await token_res.json()
_LOGGER.debug(
f"[get_access_token] token_dict: {async_redact_data(token_dict, KEYS_TO_REDACT)}"
f"[get_access_token] token_dict: {redact_dict(token_dict)}"
)
await self._resmed_response_error_check(
"get_access_token", token_res, token_dict
Expand Down Expand Up @@ -550,7 +549,7 @@ async def _gql_query(self, operation_name: str, query: str, initial: bool | None
)
raise ParsingError("Unable to decode id_token into jwt_data") from e
_LOGGER.debug(
f"[gql_query] jwt_data: {async_redact_data(jwt_data, KEYS_TO_REDACT)}"
f"[gql_query] jwt_data: {redact_dict(jwt_data)}"
)

# The graphql API only works properly if we provide the expected country code
Expand Down Expand Up @@ -592,10 +591,10 @@ async def _gql_query(self, operation_name: str, query: str, initial: bool | None
}
_LOGGER.debug(f"[gql_query] graphql_url: {graphql_url}")
_LOGGER.debug(
f"[gql_query] headers: {async_redact_data(headers, KEYS_TO_REDACT)}"
f"[gql_query] headers: {redact_dict(headers)}"
)
_LOGGER.debug(
f"[gql_query] json_query: {async_redact_data(json_query, KEYS_TO_REDACT)}"
f"[gql_query] json_query: {redact_dict(json_query)}"
)

async with self._session.post(
Expand All @@ -606,7 +605,7 @@ async def _gql_query(self, operation_name: str, query: str, initial: bool | None
_LOGGER.debug(f"[gql_query] records_res: {records_res}")
records_dict: dict[str, Any] = await records_res.json()
_LOGGER.debug(
f"[gql_query] records_dict: {async_redact_data(records_dict, KEYS_TO_REDACT)}"
f"[gql_query] records_dict: {redact_dict(records_dict)}"
)
await self._resmed_response_error_check(
"gql_query", records_res, records_dict, initial
Expand Down Expand Up @@ -655,7 +654,7 @@ async def get_sleep_records(self, initial: bool | None = False) -> list[SleepRec
_LOGGER.info("Getting Sleep Records")
records_dict: dict[str, Any] = await self._gql_query("GetPatientSleepRecords", query, initial)
_LOGGER.debug(
f"[get_sleep_records] records_dict: {async_redact_data(records_dict, KEYS_TO_REDACT)}"
f"[get_sleep_records] records_dict: {redact_dict(records_dict)}"
)
try:
records: list[SleepRecord] = records_dict["data"]["getPatientWrapper"]["sleepRecords"]["items"]
Expand All @@ -665,7 +664,7 @@ async def get_sleep_records(self, initial: bool | None = False) -> list[SleepRec
)
raise ParsingError("Error getting Patient Sleep Records") from e
_LOGGER.debug(
f"[get_sleep_records] records: {async_redact_data(records, KEYS_TO_REDACT)}"
f"[get_sleep_records] records: {redact_dict(records)}"
)
return records

Expand All @@ -689,7 +688,7 @@ async def get_user_device_data(self, initial: bool | None = False) -> MyAirDevic
_LOGGER.info("Getting User Device Data")
records_dict: dict[str, Any] = await self._gql_query("getPatientWrapper", query, initial)
_LOGGER.debug(
f"[get_user_device_data] records_dict: {async_redact_data(records_dict, KEYS_TO_REDACT)}"
f"[get_user_device_data] records_dict: {redact_dict(records_dict)}"
)
try:
device: MyAirDevice = records_dict["data"]["getPatientWrapper"]["fgDevices"][0]
Expand All @@ -699,6 +698,6 @@ async def get_user_device_data(self, initial: bool | None = False) -> MyAirDevic
)
raise ParsingError("Error getting User Device Data") from e
_LOGGER.debug(
f"[get_user_device_data] device: {async_redact_data(device, KEYS_TO_REDACT)}"
f"[get_user_device_data] device: {redact_dict(device)}"
)
return device
34 changes: 12 additions & 22 deletions custom_components/resmed_myair/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers import selector
from homeassistant.helpers.aiohttp_client import async_create_clientsession
from homeassistant.helpers.redact import async_redact_data
import voluptuous as vol

from custom_components.resmed_myair.client.myair_client import MyAirDevice
from custom_components.resmed_myair.client.rest_client import RESTClient

from .client.myair_client import (
AuthenticationError,
IncompleteAccountError,
MyAirConfig,
MyAirDevice,
ParsingError,
)
from .client.rest_client import RESTClient
from .const import (
AUTHN_SUCCESS,
CONF_DEVICE_TOKEN,
Expand All @@ -29,11 +27,11 @@
CONF_USER_NAME,
CONF_VERIFICATION_CODE,
DOMAIN,
KEYS_TO_REDACT,
REGION_EU,
REGION_NA,
VERSION,
)
from .helpers import redact_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -96,9 +94,7 @@ async def async_step_user(
self._data[CONF_REGION],
)
if status == AUTHN_SUCCESS:
_LOGGER.debug(
f"[async_step_user] device: {async_redact_data(device, KEYS_TO_REDACT)}"
)
_LOGGER.debug(f"[async_step_user] device: {redact_dict(device)}")
if "serialNumber" not in device: # type: ignore
raise ParsingError(
f"Unable to get Serial Number from Device Data"
Expand All @@ -109,9 +105,7 @@ async def async_step_user(
await self.async_set_unique_id(serial_number)
self._abort_if_unique_id_configured()
self._data.update({CONF_DEVICE_TOKEN: self._client.device_token})
_LOGGER.debug(
f"[async_step_user] data: {async_redact_data(self._data, KEYS_TO_REDACT)}"
)
_LOGGER.debug(f"[async_step_user] data: {redact_dict(self._data)}")

return self.async_create_entry(
title=f"{device['fgDeviceManufacturerName']}-{device['localizedName']}", # type: ignore
Expand Down Expand Up @@ -181,7 +175,7 @@ async def async_step_verify_mfa(
self._data.pop(CONF_VERIFICATION_CODE, None)
self._data.update({CONF_DEVICE_TOKEN: self._client.device_token}) # type: ignore
_LOGGER.debug(
f"[async_step_verify_mfa] user_input: {async_redact_data(self._data, KEYS_TO_REDACT)}"
f"[async_step_verify_mfa] user_input: {redact_dict(self._data)}"
)
return self.async_create_entry(
title=f"{device['fgDeviceManufacturerName']}-{device['localizedName']}",
Expand Down Expand Up @@ -240,12 +234,8 @@ async def async_step_reauth(
_LOGGER.info("Starting Reauthorization")
if entry := self.hass.config_entries.async_get_entry(self.context["entry_id"]):
self._entry = entry
_LOGGER.debug(
f"[async_step_reauth] entry: {async_redact_data(self._entry, KEYS_TO_REDACT)}"
)
_LOGGER.debug(
f"[async_step_reauth] entry_data: {async_redact_data(entry_data, KEYS_TO_REDACT)}"
)
_LOGGER.debug(f"[async_step_reauth] entry: {redact_dict(self._entry)}")
_LOGGER.debug(f"[async_step_reauth] entry_data: {redact_dict(entry_data)}")
self._data.update(entry_data)
return await self.async_step_reauth_confirm()

Expand All @@ -267,7 +257,7 @@ async def async_step_reauth_confirm(
)
if status == AUTHN_SUCCESS:
_LOGGER.debug(
f"[async_step_reauth_confirm] device: {async_redact_data(device, KEYS_TO_REDACT)}"
f"[async_step_reauth_confirm] device: {redact_dict(device)}"
)
if "serialNumber" not in device: # type: ignore
raise ParsingError(
Expand All @@ -279,7 +269,7 @@ async def async_step_reauth_confirm(
# self._abort_if_unique_id_configured()
self._data.update({CONF_DEVICE_TOKEN: self._client.device_token})
_LOGGER.debug(
f"[async_step_reauth_confirm] data: {async_redact_data(self._data, KEYS_TO_REDACT)}"
f"[async_step_reauth_confirm] data: {redact_dict(self._data)}"
)

self.hass.config_entries.async_update_entry(
Expand Down Expand Up @@ -317,7 +307,7 @@ async def async_step_reauth_confirm(
return self.async_abort(reason="incomplete_account")

_LOGGER.debug(
f"[async_step_reauth_confirm] initial data: {async_redact_data(self._data, KEYS_TO_REDACT)}"
f"[async_step_reauth_confirm] initial data: {redact_dict(self._data)}"
)
_LOGGER.info("Showing Reauth Confirm Form")
return self.async_show_form(
Expand Down Expand Up @@ -355,7 +345,7 @@ async def async_step_reauth_verify_mfa(
self._data.pop(CONF_VERIFICATION_CODE, None)
self._data.update({CONF_DEVICE_TOKEN: self._client.device_token}) # type: ignore
_LOGGER.debug(
f"[async_step_reauth_verify_mfa] user_input: {async_redact_data(self._data, KEYS_TO_REDACT)}"
f"[async_step_reauth_verify_mfa] user_input: {redact_dict(self._data)}"
)

self.hass.config_entries.async_update_entry(
Expand Down
Loading

0 comments on commit 3d1d2e4

Please sign in to comment.