Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow defining id for descriptor decorators #1724

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions miio/devicestatus.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ def __getattr__(self, item):
return getattr(self._embedded[embed], prop)


def sensor(name: str, *, unit: Optional[str] = None, **kwargs):
def sensor(
name: str, *, id: Optional[str] = None, unit: Optional[str] = None, **kwargs
):
"""Syntactic sugar to create SensorDescriptor objects.

The information can be used by users of the library to programmatically find out what
Expand All @@ -193,7 +195,7 @@ def sensor(name: str, *, unit: Optional[str] = None, **kwargs):

def decorator_sensor(func):
property_name = str(func.__name__)
qualified_name = str(func.__qualname__)
qualified_name = id or str(func.__qualname__)

def _sensor_type_for_return_type(func):
rtype = get_type_hints(func).get("return")
Expand Down Expand Up @@ -221,6 +223,7 @@ def _sensor_type_for_return_type(func):
def setting(
name: str,
*,
id: Optional[str] = None,
setter: Optional[Callable] = None,
setter_name: Optional[str] = None,
unit: Optional[str] = None,
Expand All @@ -247,7 +250,7 @@ def setting(

def decorator_setting(func):
property_name = str(func.__name__)
qualified_name = str(func.__qualname__)
qualified_name = id or str(func.__qualname__)

if setter is None and setter_name is None:
raise Exception("setter_name needs to be defined")
Expand Down Expand Up @@ -290,7 +293,7 @@ def decorator_setting(func):
return decorator_setting


def action(name: str, **kwargs):
def action(name: str, *, id: Optional[str] = None, **kwargs):
"""Syntactic sugar to create ActionDescriptor objects.

The information can be used by users of the library to programmatically find out what
Expand All @@ -303,7 +306,7 @@ def action(name: str, **kwargs):

def decorator_action(func):
property_name = str(func.__name__)
qualified_name = str(func.__qualname__)
qualified_name = id or str(func.__qualname__)

descriptor = ActionDescriptor(
id=qualified_name,
Expand Down