Skip to content

Commit 27d7ac4

Browse files
authored
Merge pull request #2493 from cebtenzzre/fix-setstate-errcheck
fix set_state error handling
2 parents c421d61 + 0ab50df commit 27d7ac4

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

appdaemon/plugins/hass/hassplugin.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ async def set_plugin_state(
877877
entity_id: str,
878878
state: Any | None = None,
879879
attributes: Any | None = None,
880-
):
880+
) -> dict[str, Any] | None:
881881
self.logger.debug("set_plugin_state() %s %s %s %s", namespace, entity_id, state, attributes)
882882

883883
# if we get a request for not our namespace something has gone very wrong
@@ -887,7 +887,15 @@ async def set_plugin_state(
887887
async def safe_set_state(self: "HassPlugin"):
888888
return await self.http_method("post", f"api/states/{entity_id}", state=state, attributes=attributes)
889889

890-
return await safe_set_state(self)
890+
resp = await safe_set_state(self)
891+
match resp:
892+
case ClientResponseError(message=str(msg)):
893+
self.logger.error("Error setting state: %s", msg)
894+
return None
895+
case dict():
896+
return resp
897+
case _:
898+
return None
891899

892900
@utils.warning_decorator(error_text="Unexpected error getting state")
893901
async def get_plugin_state(

appdaemon/state.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import threading
44
import traceback
55
import uuid
6-
from collections.abc import Mapping
6+
from collections.abc import Awaitable, Callable, Mapping
77
from copy import copy, deepcopy
88
from datetime import timedelta
99
from enum import Enum
1010
from logging import Logger
1111
from pathlib import Path
12-
from typing import TYPE_CHECKING, Any, Awaitable, List, Literal, Optional, Protocol, Set, overload
12+
from typing import TYPE_CHECKING, Any, Literal, Optional, Protocol, overload
1313

1414
from . import exceptions as ade
1515
from . import utils
@@ -51,7 +51,7 @@ class State:
5151
name: str = "_state"
5252
state: dict[str, dict[str, Any] | utils.PersistentDict]
5353

54-
app_added_namespaces: Set[str]
54+
app_added_namespaces: set[str]
5555

5656
def __init__(self, ad: "AppDaemon"):
5757
self.AD = ad
@@ -234,7 +234,7 @@ async def remove_persistent_namespace(self, namespace: str, state: utils.Persist
234234
self.logger.error('Error removing namespace file %s: %s', ns_file.name, e)
235235
continue
236236

237-
def list_namespaces(self) -> List[str]:
237+
def list_namespaces(self) -> list[str]:
238238
return list(self.state.keys())
239239

240240
def list_namespace_entities(self, namespace: str) -> list[str]:
@@ -836,21 +836,25 @@ async def set_state(
836836

837837
plugin = self.AD.plugins.get_plugin_object(namespace)
838838

839-
if set_plugin_state := getattr(plugin, "set_plugin_state", False):
839+
plugin_handled = False
840+
set_plugin_state: Callable[..., Awaitable[dict[str, Any] | None]] | None
841+
if (set_plugin_state := getattr(plugin, "set_plugin_state", None)) is not None:
840842
# We assume that the state change will come back to us via the plugin
841843
self.logger.debug("sending event to plugin")
842844

843-
result = await set_plugin_state( # pyright: ignore[reportCallIssue]
845+
result = await set_plugin_state(
844846
namespace,
845847
entity,
846848
state=new_state["state"],
847-
attributes=new_state["attributes"]
848-
) # fmt: skip
849+
attributes=new_state["attributes"],
850+
)
849851
if result is not None:
850852
if "entity_id" in result:
851853
result.pop("entity_id")
852854
self.state[namespace][entity] = self.parse_state(namespace, entity, **result)
853-
else:
855+
plugin_handled = True
856+
857+
if not plugin_handled:
854858
# Set the state locally
855859
self.state[namespace][entity] = new_state
856860
# Fire the event locally

0 commit comments

Comments
 (0)