Skip to content

Commit

Permalink
Trying evne harder to get rid of exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Olen committed Aug 4, 2023
1 parent 16bbc3e commit 0b7a465
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
5 changes: 4 additions & 1 deletion custom_components/plant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ def ws_get_info(
plant_entity = hass.data[DOMAIN][key][ATTR_PLANT]
if plant_entity.entity_id == msg["entity_id"]:
# _LOGGER.debug("Sending websocket response: %s", plant_entity.websocket_info)
connection.send_result(msg["id"], {"result": plant_entity.websocket_info})
try:
connection.send_result(msg["id"], {"result": plant_entity.websocket_info})
except ValueError as e:
_LOGGER.warning(e)
return
connection.send_error(
msg["id"], "entity_not_found", f"Entity {msg['entity_id']} not found"
Expand Down
48 changes: 30 additions & 18 deletions custom_components/plant/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,30 +234,42 @@ async def async_added_to_hass(self) -> None:

async def async_update(self) -> None:
"""Set state and unit to the parent sensor state and unit"""
if self.external_sensor and self._hass.states.get(self.external_sensor):
if (
self._hass.states.get(self.external_sensor).state == STATE_UNKNOWN
or self._hass.states.get(self.external_sensor).state
== STATE_UNAVAILABLE
):
if self.external_sensor:
try:
self._attr_native_value = float(self._hass.states.get(
self.external_sensor
).state
if (
ATTR_UNIT_OF_MEASUREMENT
in self._hass.states.get(self.external_sensor).attributes
):
self._attr_native_unit_of_measurement = self._hass.states.get(
self.external_sensor
).attributes[ATTR_UNIT_OF_MEASUREMENT]
except AttributeError:
_LOGGER.debug(
"Unknown external value for %s, setting to default: %s",
"Unknown external sensor for %s: %s, setting to default: %s",
self.entity_id,
self.external_sensor,
self._default_state,
)
self._attr_native_value = self._default_state
else:
self._attr_native_value = self._hass.states.get(
self.external_sensor
).state
if (
ATTR_UNIT_OF_MEASUREMENT
in self._hass.states.get(self.external_sensor).attributes
):
self._attr_native_unit_of_measurement = self._hass.states.get(
self.external_sensor
).attributes[ATTR_UNIT_OF_MEASUREMENT]
except ValueError:
_LOGGER.debug(
"Unknown external value for %s: %s = %s, setting to default: %s",
self.entity_id,
self.external_sensor,
self._hass.states.get(self.external_sensor).state,
self._default_state,
)
self._attr_native_value = self._default_state

else:
_LOGGER.debug(
"External sensor not set for %s, setting to default: %s",
self.entity_id,
self._default_state,
)
self._attr_native_value = self._default_state

@callback
Expand Down

0 comments on commit 0b7a465

Please sign in to comment.