Skip to content

Commit 104953b

Browse files
authored
Adding Area default values for parameters that will be required in future versions (#322)
* adding default values for parameters that will be required in future versions * correct labels from label and changed default from None to set() as expected * added introspection so we can make defaults available before the function in HA has the parameter available
1 parent d92a164 commit 104953b

File tree

1 file changed

+28
-8
lines changed
  • custom_components/magic_areas

1 file changed

+28
-8
lines changed

custom_components/magic_areas/util.py

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import inspect
23
from collections.abc import Iterable
34

45
from homeassistant.helpers.area_registry import AreaEntry
@@ -78,11 +79,30 @@ async def load_entities(event):
7879

7980
def get_meta_area_object(name):
8081

81-
return AreaEntry(
82-
name=name,
83-
normalized_name=slugify(name),
84-
aliases=set(),
85-
id=slugify(name),
86-
picture=None,
87-
icon=None,
88-
)
82+
area_slug = slugify(name)
83+
84+
params = {
85+
'name': name,
86+
'normalized_name': area_slug,
87+
'aliases': set(),
88+
'id': area_slug,
89+
'picture': None,
90+
'icon': None,
91+
'floor_id': None,
92+
'labels': set()
93+
}
94+
95+
# We have to introspect the AreaEntry constructor
96+
# to know if a given param is available because usually
97+
# Home Assistant updates this object with new parameters in
98+
# the constructor without defaults and breaks this function
99+
# in particular.
100+
101+
available_params = {}
102+
constructor_params = inspect.signature(AreaEntry.__init__).parameters
103+
104+
for k, v in params.items():
105+
if k in constructor_params:
106+
available_params[k] = v
107+
108+
return AreaEntry(**available_params)

0 commit comments

Comments
 (0)