Skip to content

Commit 7bc14a1

Browse files
committed
State.{set,get,setattr,getattr} don't need to be async
1 parent 43aa488 commit 7bc14a1

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

custom_components/pyscript/eval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,10 +1214,10 @@ async def recurse_assign(self, lhs, val):
12141214
raise NotImplementedError(f"unknown lhs type {lhs} (got {var_name}) in assign")
12151215
dot_count = var_name.count(".")
12161216
if dot_count == 1:
1217-
await State.set(var_name, val)
1217+
State.set(var_name, val)
12181218
return
12191219
if dot_count == 2:
1220-
await State.setattr(var_name, val)
1220+
State.setattr(var_name, val)
12211221
return
12221222
if dot_count > 0:
12231223
raise NameError(

custom_components/pyscript/state.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def notify_var_get(cls, var_names, new_vars):
147147
return notify_vars
148148

149149
@classmethod
150-
async def set(cls, var_name, value=None, new_attributes=None, **kwargs):
150+
def set(cls, var_name, value=None, new_attributes=None, **kwargs):
151151
"""Set a state variable and optional attributes in hass."""
152152
if var_name.count(".") != 1:
153153
raise NameError(f"invalid name {var_name} (should be 'domain.entity')")
@@ -196,14 +196,14 @@ async def set(cls, var_name, value=None, new_attributes=None, **kwargs):
196196
cls.notify_var_last[var_name] = StateVal(cls.hass.states.get(var_name))
197197

198198
@classmethod
199-
async def setattr(cls, var_attr_name, value):
199+
def setattr(cls, var_attr_name, value):
200200
"""Set a state variable's attribute in hass."""
201201
parts = var_attr_name.split(".")
202202
if len(parts) != 3:
203203
raise NameError(f"invalid name {var_attr_name} (should be 'domain.entity.attr')")
204204
if not cls.exist(f"{parts[0]}.{parts[1]}"):
205205
raise NameError(f"state {parts[0]}.{parts[1]} doesn't exist")
206-
await cls.set(f"{parts[0]}.{parts[1]}", **{parts[2]: value})
206+
cls.set(f"{parts[0]}.{parts[1]}", **{parts[2]: value})
207207

208208
@classmethod
209209
async def register_persist(cls, var_name):
@@ -223,12 +223,12 @@ async def persist(cls, var_name, default_value=None, default_attributes=None):
223223
exists = cls.exist(var_name)
224224

225225
if not exists and default_value is not None:
226-
await cls.set(var_name, default_value, default_attributes)
226+
cls.set(var_name, default_value, default_attributes)
227227
elif exists and default_attributes is not None:
228228
# Patch the attributes with new values if necessary
229229
current = cls.hass.states.get(var_name)
230230
new_attributes = {k: v for (k, v) in default_attributes.items() if k not in current.attributes}
231-
await cls.set(var_name, current.state, **new_attributes)
231+
cls.set(var_name, current.state, **new_attributes)
232232

233233
@classmethod
234234
def exist(cls, var_name):
@@ -308,7 +308,7 @@ async def service_call(*args, **kwargs):
308308
)
309309

310310
@classmethod
311-
async def getattr(cls, var_name):
311+
def getattr(cls, var_name):
312312
"""Return a dict of attributes for a state variable."""
313313
if isinstance(var_name, StateVal):
314314
attrs = var_name.__dict__.copy()
@@ -323,10 +323,10 @@ async def getattr(cls, var_name):
323323
return value.attributes.copy()
324324

325325
@classmethod
326-
async def get_attr(cls, var_name):
326+
def get_attr(cls, var_name):
327327
"""Return a dict of attributes for a state variable - deprecated."""
328328
_LOGGER.warning("state.get_attr() is deprecated: use state.getattr() instead")
329-
return await cls.getattr(var_name)
329+
return cls.getattr(var_name)
330330

331331
@classmethod
332332
def completions(cls, root):

docs/new_features.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ Planned new features post 1.0.0 include:
2424
The new features since 1.0.0 in master include:
2525

2626
- None so far
27+
28+
Bug fixes since 1.0.0 in master include:
29+
30+
- the deprecated function ``state.get_attr`` was missing an ``await``, which caused an exception; in 1.0.0 use ``state.getattr`` instead (#88).

0 commit comments

Comments
 (0)