Skip to content

Commit

Permalink
Adding Area default values for parameters that will be required in fu…
Browse files Browse the repository at this point in the history
…ture 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
  • Loading branch information
jseidl committed Feb 29, 2024
1 parent d92a164 commit 104953b
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions custom_components/magic_areas/util.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import inspect
from collections.abc import Iterable

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

def get_meta_area_object(name):

return AreaEntry(
name=name,
normalized_name=slugify(name),
aliases=set(),
id=slugify(name),
picture=None,
icon=None,
)
area_slug = slugify(name)

params = {
'name': name,
'normalized_name': area_slug,
'aliases': set(),
'id': area_slug,
'picture': None,
'icon': None,
'floor_id': None,
'labels': set()
}

# We have to introspect the AreaEntry constructor
# to know if a given param is available because usually
# Home Assistant updates this object with new parameters in
# the constructor without defaults and breaks this function
# in particular.

available_params = {}
constructor_params = inspect.signature(AreaEntry.__init__).parameters

for k, v in params.items():
if k in constructor_params:
available_params[k] = v

return AreaEntry(**available_params)

0 comments on commit 104953b

Please sign in to comment.