Skip to content

Commit

Permalink
Merge pull request #57 from iprak/fix-missing-message-in-SensiConnect…
Browse files Browse the repository at this point in the history
…ionError

Fix missing message in sensi connection error
  • Loading branch information
iprak authored Aug 25, 2024
2 parents 1032d5c + a7c778a commit 3ab3006
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
5 changes: 3 additions & 2 deletions custom_components/sensi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The Sensi thermostat component."""

from __future__ import annotations

from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -57,9 +58,9 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
DOMAIN_DATA_COORDINATOR_KEY: coordinator,
}
await hass.config_entries.async_forward_entry_setups(entry, SUPPORTED_PLATFORMS)
except ConfigEntryAuthFailed as err:
except ConfigEntryAuthFailed:
# Pass ConfigEntryAuthFailed, this can be raised from the coordinator
raise err
raise
except AuthenticationError as err:
# Raising ConfigEntryAuthFailed will automatically put the config entry in a
# failure state and start a reauth flow.
Expand Down
2 changes: 1 addition & 1 deletion custom_components/sensi/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def _get_new_tokens(hass: HomeAssistant, refresh_token: str) -> any:
)
except (asyncio.TimeoutError, aiohttp.ClientError) as err:
LOGGER.warning("Timed out getting access token", exc_info=True)
raise SensiConnectionError from err
raise SensiConnectionError("Timed out getting access token") from err

if response.status != HTTPStatus.OK:
LOGGER.warning("Invalid token")
Expand Down
5 changes: 3 additions & 2 deletions custom_components/sensi/climate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Sensi Thermostat."""

from __future__ import annotations

from collections.abc import Mapping
from typing import Any, Union
from typing import Any

from homeassistant.components.climate import (
ENTITY_ID_FORMAT,
Expand Down Expand Up @@ -65,7 +66,7 @@ def __init__(self, device: SensiDevice, entry: ConfigEntry) -> None:
)

@property
def extra_state_attributes(self) -> Union[Mapping[str, Any], None]:
def extra_state_attributes(self) -> Mapping[str, Any] | None:
"""Return the state attributes."""
return self._device.attributes

Expand Down
3 changes: 2 additions & 1 deletion custom_components/sensi/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Config flow for Sensi thermostat."""

from __future__ import annotations

from collections.abc import Mapping
Expand Down Expand Up @@ -45,7 +46,7 @@ async def _try_login(self, config: AuthenticationConfig):
return {"base": "cannot_connect"}
except AuthenticationError:
return {"base": "invalid_auth"}
except Exception as err: # pylint: disable=broad-except
except Exception as err: # pylint: disable=broad-except # noqa: BLE001
LOGGER.exception(str(err))
return {"base": "unknown"}

Expand Down
23 changes: 12 additions & 11 deletions custom_components/sensi/coordinator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The Sensi data coordinator."""

from __future__ import annotations

import asyncio
Expand Down Expand Up @@ -669,8 +670,8 @@ async def _fetch_device_data(self) -> dict[str, SensiDevice]:
async with websockets.client.connect(
url, extra_headers=self._headers
) as websocket:
while (not done) and (fetch_count < MAX_DATA_FETCH_COUNT):
try:
try:
while (not done) and (fetch_count < MAX_DATA_FETCH_COUNT):
msg = await asyncio.wait_for(websocket.recv(), timeout=10)
done = self._parse_socket_response(msg, self._devices)
fetch_count = fetch_count + 1
Expand All @@ -679,14 +680,14 @@ async def _fetch_device_data(self) -> dict[str, SensiDevice]:
LOGGER.debug("Data updated, it failed last time")
self._last_update_failed = False

except (
asyncio.TimeoutError,
websockets.exceptions.WebSocketException,
) as exception:
done = True
self._last_update_failed = True
raise UpdateFailed(exception) from exception
# Pass AuthenticationError
except (
asyncio.TimeoutError,
websockets.exceptions.WebSocketException,
) as exception:
done = True
self._last_update_failed = True
raise UpdateFailed(exception) from exception
# Pass AuthenticationError

for device in self.get_devices():
device.authenticated = True
Expand All @@ -711,7 +712,7 @@ async def async_send_event_priv(self, data: str) -> None:
msg = await asyncio.wait_for(websocket.recv(), timeout=5)
self._last_event_time_stamp = datetime.now()
LOGGER.debug("async_send_event response=%s", msg)
except Exception as err: # pylint: disable=broad-except
except Exception as err: # pylint: disable=broad-except # noqa: BLE001
LOGGER.warning("Sending event with %s failed", data)
LOGGER.warning(str(err))

Expand Down
11 changes: 7 additions & 4 deletions custom_components/sensi/switch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Sensi thermostat setting switches."""

from __future__ import annotations

from dataclasses import dataclass
Expand Down Expand Up @@ -87,10 +88,12 @@ async def async_setup_entry(

entities = []
for device in coordinator.get_devices():
for description in SWITCH_TYPES:
# A device might not support a setting e.g. Continuous Backlight
if device.supports(description.capability):
entities.append(SensiCapabilitySettingSwitch(device, description))
# A device might not support a setting e.g. Continuous Backlight
entities.extend(
SensiCapabilitySettingSwitch(device, description)
for description in SWITCH_TYPES
if device.supports(description.capability)
)

entities.append(SensiFanSupportSwitch(device, entry))
entities.append(SensiAuxHeatSwitch(device, entry))
Expand Down

0 comments on commit 3ab3006

Please sign in to comment.