Skip to content

Commit

Permalink
Merge pull request #95 from Olen/unknownunavailable
Browse files Browse the repository at this point in the history
Trying even harder to get rid of exceptions
Fix #94
  • Loading branch information
Olen authored Aug 4, 2023
2 parents 16bbc3e + ba90bd5 commit bb7d462
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
7 changes: 6 additions & 1 deletion custom_components/plant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,12 @@ 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
2 changes: 1 addition & 1 deletion custom_components/plant/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"requirements": [
"async-timeout>=4.0.2"
],
"version": "2.2.0"
"version": "2023.8.0-beta1"
}
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 bb7d462

Please sign in to comment.