Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request new sas_token if connection fails #189

Merged
merged 3 commits into from
Jun 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion custom_components/toshiba_ac/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""The Toshiba AC integration."""
from __future__ import annotations

import logging

from toshiba_ac.device_manager import ToshibaAcDeviceManager

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

PLATFORMS = ["climate", "select", "sensor", "switch"]


Expand All @@ -33,7 +37,23 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
try:
await device_manager.connect()
except Exception:
return False
_LOGGER.warning("Initial connection failed, trying to get new sas_token...")
# If it fails to connect, try to get a new sas_token
device_manager = ToshibaAcDeviceManager(
entry.data["username"], entry.data["password"], entry.data["device_id"]
)

try:
new_sas_token = await device_manager.connect()

_LOGGER.info("Successfully got new sas_token!")

# Save new sas_token
new_data = {**entry.data, "sas_token": new_sas_token}
hass.config_entries.async_update_entry(entry, data=new_data)
except Exception:
_LOGGER.warning("Connection failed on second try, aborting!")
return False

hass.data[DOMAIN][entry.entry_id] = device_manager

Expand Down
Loading