Skip to content

Commit

Permalink
Merge pull request #70 from gjohansson-ST/error_handling
Browse files Browse the repository at this point in the history
Improve error handling
  • Loading branch information
gjohansson-ST authored Jun 5, 2022
2 parents 9702a37 + 3b0cdba commit cc01b06
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 7 additions & 0 deletions custom_components/sector/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
Expand Down Expand Up @@ -99,6 +100,8 @@ async def async_alarm_arm_home(self, code=None) -> None:
if self._hub.log_name:
self._attr_changed_by = self._hub.log_name
self.async_write_ha_state()
return
raise HomeAssistantError("No code provided")

async def async_alarm_disarm(self, code=None) -> None:
"""Arm alarm off."""
Expand All @@ -111,6 +114,8 @@ async def async_alarm_disarm(self, code=None) -> None:
if self._hub.log_name:
self._attr_changed_by = self._hub.log_name
self.async_write_ha_state()
return
raise HomeAssistantError("No code provided")

async def async_alarm_arm_away(self, code=None) -> None:
"""Arm alarm away."""
Expand All @@ -123,6 +128,8 @@ async def async_alarm_arm_away(self, code=None) -> None:
if self._hub.log_name:
self._attr_changed_by = self._hub.log_name
self.async_write_ha_state()
return
raise HomeAssistantError("No code provided")

@callback
def _handle_coordinator_update(self) -> None:
Expand Down
13 changes: 10 additions & 3 deletions custom_components/sector/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
from aiohttp import ClientResponse
import async_timeout

from homeassistant.exceptions import ConfigEntryAuthFailed, HomeAssistantError
from homeassistant.exceptions import (
ConfigEntryAuthFailed,
ConfigEntryNotReady,
HomeAssistantError,
)

from .const import API_URL, LOGGER

Expand Down Expand Up @@ -158,7 +162,7 @@ async def fetch_info(self, tempcheck: bool = True) -> None:
if not self._panel:
response = await self._request(API_URL + "/Panel/getFullSystem")
if response is None:
return None
raise ConfigEntryNotReady
json_data = await response.json()
if json_data is not None:
self._panel = json_data["Panel"]
Expand Down Expand Up @@ -279,7 +283,10 @@ async def _request(
LOGGER.debug("request status: %s", response.status)
return response

return None
text = await response.text
LOGGER.error(
"Error fetching data status %s with text %s", response.status, text
)

except aiohttp.ClientConnectorError as error:
LOGGER.error("ClientError connecting to Sector: %s ", error, exc_info=True)
Expand Down
5 changes: 5 additions & 0 deletions custom_components/sector/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_CODE, STATE_LOCKED
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -107,6 +108,8 @@ async def async_unlock(self, **kwargs) -> None:
await self._hub.triggerlock(self.entity_description.key, code, command)
self._attr_is_locked = False
self.async_write_ha_state()
return
raise HomeAssistantError("No code provided")

async def async_lock(self, **kwargs) -> None:
"""Lock lock."""
Expand All @@ -116,6 +119,8 @@ async def async_lock(self, **kwargs) -> None:
await self._hub.triggerlock(self.entity_description.key, code, command)
self._attr_is_locked = True
self.async_write_ha_state()
return
raise HomeAssistantError("No code provided")

@callback
def _handle_coordinator_update(self) -> None:
Expand Down

0 comments on commit cc01b06

Please sign in to comment.