Skip to content

Commit

Permalink
Handle exceptions when a switch does not support reading the targetValue
Browse files Browse the repository at this point in the history
  • Loading branch information
Mat931 committed Feb 21, 2024
1 parent 94858bc commit 2c2041c
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions custom_components/digitalstrom/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .api.exceptions import ServerError
from .const import CONF_DSUID, DOMAIN
from .entity import DigitalstromEntity

Expand Down Expand Up @@ -78,6 +79,7 @@ def __init__(self, channel):
self._attr_has_entity_name = False
self.entity_id = f"{DOMAIN}.{self.device.dsuid}_{channel.index}"
self._attr_name = self.device.name
self.supports_target_value = True

@property
def is_on(self) -> bool | None:
Expand All @@ -99,10 +101,15 @@ async def async_turn_off(self, **kwargs: Any) -> None:
)

async def async_update(self, **kwargs: Any):
result = await self.client.request(
f"property/getFloating?path=/apartment/zones/zone{self.device.zone_id}/devices/{self.device.dsuid}/status/outputs/powerState/targetValue"
)
self.last_value = result.get("value", None)
if not self.supports_target_value:
return
try:
result = await self.client.request(
f"property/getFloating?path=/apartment/zones/zone{self.device.zone_id}/devices/{self.device.dsuid}/status/outputs/powerState/targetValue"
)
self.last_value = result.get("value", None)
except ServerError:
self.supports_target_value = False


class DigitalstromApartmentScene(SwitchEntity):
Expand Down

0 comments on commit 2c2041c

Please sign in to comment.