Skip to content

Commit 7c087e7

Browse files
committed
replaced entity_ids() with state.names()
1 parent 7b2d737 commit 7c087e7

File tree

8 files changed

+32
-11
lines changed

8 files changed

+32
-11
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ In cases where you need to compute the name of the state variable dynamically, o
265265
set the state attributes, you can use the built-in functions `state.get(name)` and
266266
`state.set(name, value, attr=None)`; see below.
267267

268-
The function `entity_ids(domain=None)` returns a list of all `entity_id`s of a domain. If domain
269-
is not specified, it returns all entities.
268+
The function `state.names(domain=None)` returns a list of all state variable names (ie, `entity_id`s)
269+
of a domain. If `domain` is not specified, it returns all HASS state variable (entity) names.
270270

271271
Also, service names (which are called as functions) take priority over state variable names,
272272
so if a component has a state variable name that collides with one of its services, you'll
@@ -582,6 +582,10 @@ set the attributes, which you can't do if you are directly assigning to the vari
582582

583583
`state.get(name)` returns the value of the state variable, or `None` if it doesn't exist
584584

585+
`state.names(domain=None)` returns a list of all state variable names (ie, `entity_id`s)
586+
of a domain. If `domain` is not specified, it returns all HASS state variable (`entity_id`)
587+
names.
588+
585589
`state.set(name, value, attr=None)` sets the state variable to the given value, with the optional attributes.
586590

587591
Note that in HASS, all state variable values are coerced into strings. For example, if a state variable

custom_components/pyscript/eval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,8 @@ async def recurse_assign(self, lhs, val):
625625
f"too few values to unpack (expected at least {len(lhs.elts) - 1})"
626626
)
627627
star_name = lhs_elt.value.id
628-
for lhs_idx, lhs_elt in enumerate(lhs.elts[lhs_idx + 1 :]):
629-
await self.recurse_assign(lhs_elt, star_lhs[star_len + lhs_idx])
628+
for lhs_idx2, lhs_elt in enumerate(lhs.elts[lhs_idx + 1 :]):
629+
await self.recurse_assign(lhs_elt, star_lhs[star_len + lhs_idx2])
630630
await self.recurse_assign(
631631
ast.Name(id=star_name, ctx=ast.Store()), star_lhs[0:star_len]
632632
)

custom_components/pyscript/handler.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def init(hass):
3434
"task.unique": Handler.task_unique,
3535
"service.call": Handler.service_call,
3636
"service.has_service": Handler.service_has_service,
37-
"entity_ids": Handler.entity_ids,
3837
}
3938

4039
#

custom_components/pyscript/state.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,15 @@ def completions(root):
140140
words.add(name.entity_id)
141141
return words
142142

143+
async def entity_ids(domain=None):
144+
"""Implement entity_ids."""
145+
return State.hass.states.async_entity_ids(domain)
146+
143147
def register_functions():
144148
"""Register state functions."""
145149
functions = {
146150
"state.get": State.get,
147151
"state.set": State.set,
152+
"state.names": State.entity_ids,
148153
}
149154
Handler.register(functions)

tests/custom_components/pyscript/test_func.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ async def setup_script(hass, notify_q, now, source):
3333
)
3434

3535
with patch(
36-
"homeassistant.loader.async_get_integration", return_value=integration,
36+
"homeassistant.loader.async_get_integration",
37+
return_value=integration,
3738
), patch(
3839
"config.custom_components.pyscript.os.path.isdir", return_value=True
3940
), patch(

tests/custom_components/pyscript/test_init.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ async def setup_script(hass, notify_q, now, source):
3434
)
3535

3636
with patch(
37-
"homeassistant.loader.async_get_integration", return_value=integration,
37+
"homeassistant.loader.async_get_integration",
38+
return_value=integration,
3839
), patch(
3940
"config.custom_components.pyscript.os.path.isdir", return_value=True
4041
), patch(
@@ -86,7 +87,8 @@ async def test_setup_fails_on_no_dir(hass, caplog):
8687
)
8788

8889
with patch(
89-
"homeassistant.loader.async_get_integration", return_value=integration,
90+
"homeassistant.loader.async_get_integration",
91+
return_value=integration,
9092
), patch("config.custom_components.pyscript.os.path.isdir", return_value=False):
9193
res = await async_setup_component(hass, "pyscript", {DOMAIN: {}})
9294

@@ -432,7 +434,8 @@ def func5(var_name=None, value=None):
432434
)
433435

434436
with patch(
435-
"homeassistant.loader.async_get_integration", return_value=integration,
437+
"homeassistant.loader.async_get_integration",
438+
return_value=integration,
436439
), patch(
437440
"config.custom_components.pyscript.os.path.isdir", return_value=True
438441
), patch(

tests/custom_components/pyscript/test_unique.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ async def setup_script(hass, notify_q, now, source):
3232
)
3333

3434
with patch(
35-
"homeassistant.loader.async_get_integration", return_value=integration,
35+
"homeassistant.loader.async_get_integration",
36+
return_value=integration,
3637
), patch(
3738
"config.custom_components.pyscript.os.path.isdir", return_value=True
3839
), patch(

tests/custom_components/pyscript/test_unit_eval.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@
172172
"Foo = [type('Foo', (), {'x': [0, [[100, 101, 102, 103]]]})]; Foo[0].x[1][0][1:2] = [11, 12]; Foo[0].x[1]",
173173
[[100, 11, 12, 102, 103]],
174174
],
175+
[
176+
"pyscript.var1 = 1; pyscript.var2 = 2; set(state.names('pyscript'))",
177+
{"pyscript.var1", "pyscript.var2"},
178+
],
175179
["eval('1+2')", 3],
176180
["x = 5; eval('2 * x')", 10],
177181
["x = 5; exec('x = 2 * x'); x", 10],
@@ -654,7 +658,11 @@ def get(self):
654658
async def run_one_test(test_data):
655659
"""Run one interpreter test."""
656660
source, expect = test_data
657-
global_ctx = GlobalContext("test", None, global_sym_table={},)
661+
global_ctx = GlobalContext(
662+
"test",
663+
None,
664+
global_sym_table={},
665+
)
658666
ast = AstEval("test", global_ctx=global_ctx)
659667
ast.parse(source)
660668
if ast.get_exception() is not None:

0 commit comments

Comments
 (0)