Skip to content

Commit

Permalink
Add config_flow's seperated reaseon and more debug information (#131131)
Browse files Browse the repository at this point in the history
Co-authored-by: yunseon.park <[email protected]>
  • Loading branch information
LG-ThinQ-Integration and YunseonPark-LGE authored Nov 22, 2024
1 parent 49eeb2d commit 02f16ff
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/lg_thinq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ async def async_setup_coordinators(
raise ConfigEntryNotReady(exc.message) from exc

if not bridge_list:
_LOGGER.warning("No devices registered with the correct profile")
return

# Setup coordinator per device.
Expand Down
14 changes: 11 additions & 3 deletions homeassistant/components/lg_thinq/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Any
import uuid

from thinqconnect import ThinQApi, ThinQAPIException
from thinqconnect import ThinQApi, ThinQAPIErrorCodes, ThinQAPIException
from thinqconnect.country import Country
import voluptuous as vol

Expand All @@ -26,6 +26,13 @@
)

SUPPORTED_COUNTRIES = [country.value for country in Country]
THINQ_ERRORS = {
ThinQAPIErrorCodes.INVALID_TOKEN: "invalid_token",
ThinQAPIErrorCodes.NOT_ACCEPTABLE_TERMS: "not_acceptable_terms",
ThinQAPIErrorCodes.NOT_ALLOWED_API_AGAIN: "not_allowed_api_again",
ThinQAPIErrorCodes.NOT_SUPPORTED_COUNTRY: "not_supported_country",
ThinQAPIErrorCodes.EXCEEDED_API_CALLS: "exceeded_api_calls",
}

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -83,8 +90,9 @@ async def async_step_user(

try:
return await self._validate_and_create_entry(access_token, country_code)
except ThinQAPIException:
errors["base"] = "token_unauthorized"
except ThinQAPIException as exc:
errors["base"] = THINQ_ERRORS.get(exc.code, "token_unauthorized")
_LOGGER.error("Failed to validate access_token %s", exc)

return self.async_show_form(
step_id="user",
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/lg_thinq/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,9 @@ async def async_setup_device_coordinator(
coordinator = DeviceDataUpdateCoordinator(hass, ha_bridge)
await coordinator.async_refresh()

_LOGGER.debug("Setup device's coordinator: %s", coordinator.device_name)
_LOGGER.debug(
"Setup device's coordinator: %s, model:%s",
coordinator.device_name,
coordinator.api.device.model_name,
)
return coordinator
2 changes: 1 addition & 1 deletion homeassistant/components/lg_thinq/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def __init__(
self._attr_device_info = dr.DeviceInfo(
identifiers={(DOMAIN, coordinator.unique_id)},
manufacturer=COMPANY,
model=coordinator.api.device.model_name,
model=f"{coordinator.api.device.model_name} ({self.coordinator.api.device.device_type})",
name=coordinator.device_name,
)
self._attr_unique_id = f"{coordinator.unique_id}_{self.property_id}"
Expand Down
7 changes: 6 additions & 1 deletion homeassistant/components/lg_thinq/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def on_message_received(

async def async_handle_device_event(self, message: dict) -> None:
"""Handle received mqtt message."""
_LOGGER.debug("async_handle_device_event: message=%s", message)
unique_id = (
f"{message["deviceId"]}_{list(message["report"].keys())[0]}"
if message["deviceType"] == DeviceType.WASHTOWER
Expand All @@ -178,6 +177,12 @@ async def async_handle_device_event(self, message: dict) -> None:
_LOGGER.error("Failed to handle device event: No device")
return

_LOGGER.debug(
"async_handle_device_event: %s, model:%s, message=%s",
coordinator.device_name,
coordinator.api.device.model_name,
message,
)
push_type = message.get("pushType")

if push_type == DEVICE_STATUS_MESSAGE:
Expand Down
6 changes: 6 additions & 0 deletions homeassistant/components/lg_thinq/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
"already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]"
},
"error": {
"invalid_token": "The token is not valid.",
"not_acceptable_terms": "The service terms are not accepted.",
"not_allowed_api_again": "The user does NOT have permission on the API call.",
"not_supported_country": "The country is not supported.",
"exceeded_api_calls": "The number of API calls has been exceeded.",
"exceeded_user_api_calls": "The number of User API calls has been exceeded.",
"token_unauthorized": "The token is invalid or unauthorized."
},
"step": {
Expand Down
2 changes: 1 addition & 1 deletion tests/components/lg_thinq/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def test_config_flow_invalid_pat(
data={CONF_ACCESS_TOKEN: MOCK_PAT, CONF_COUNTRY: MOCK_COUNTRY},
)
assert result["type"] is FlowResultType.FORM
assert result["errors"] == {"base": "token_unauthorized"}
assert result["errors"]
mock_invalid_thinq_api.async_get_device_list.assert_called_once()


Expand Down

0 comments on commit 02f16ff

Please sign in to comment.