Skip to content

Commit

Permalink
Rename SettingDescriptor's type to setting_type (#1715)
Browse files Browse the repository at this point in the history
This makes the type of 'type' consistent among all descriptors.
  • Loading branch information
rytilahti committed Feb 6, 2023
1 parent 7ad548a commit 39bf909
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
20 changes: 10 additions & 10 deletions miio/descriptors.py
Expand Up @@ -31,6 +31,8 @@ class Descriptor:

id: str
name: str
type: Optional[type] = None
extras: Dict = attr.ib(factory=dict, repr=False)


@attr.s(auto_attribs=True)
Expand All @@ -40,10 +42,9 @@ class ActionDescriptor(Descriptor):
method_name: Optional[str] = attr.ib(default=None, repr=False)
method: Optional[Callable] = attr.ib(default=None, repr=False)
inputs: Optional[List[Any]] = attr.ib(default=None, repr=True)
extras: Dict = attr.ib(factory=dict, repr=False)


@attr.s(auto_attribs=True)
@attr.s(auto_attribs=True, kw_only=True)
class SensorDescriptor(Descriptor):
"""Describes a sensor exposed by the device.
Expand All @@ -54,9 +55,7 @@ class SensorDescriptor(Descriptor):
"""

property: str
type: type
unit: Optional[str] = None
extras: Dict = attr.ib(factory=dict, repr=False)


class SettingType(Enum):
Expand All @@ -72,10 +71,9 @@ class SettingDescriptor(Descriptor):

property: str
unit: Optional[str] = None
type = SettingType.Undefined
setting_type = SettingType.Undefined
setter: Optional[Callable] = attr.ib(default=None, repr=False)
setter_name: Optional[str] = attr.ib(default=None, repr=False)
extras: Dict = attr.ib(factory=dict, repr=False)

def cast_value(self, value: int):
"""Casts value to the expected type."""
Expand All @@ -84,21 +82,22 @@ def cast_value(self, value: int):
SettingType.Enum: int,
SettingType.Number: int,
}
return cast_map[self.type](int(value))
return cast_map[self.setting_type](int(value))


@attr.s(auto_attribs=True, kw_only=True)
class BooleanSettingDescriptor(SettingDescriptor):
"""Presents a settable boolean value."""

type: SettingType = SettingType.Boolean
type: type = bool
setting_type: SettingType = SettingType.Boolean


@attr.s(auto_attribs=True, kw_only=True)
class EnumSettingDescriptor(SettingDescriptor):
"""Presents a settable, enum-based value."""

type: SettingType = SettingType.Enum
setting_type: SettingType = SettingType.Enum
choices_attribute: Optional[str] = attr.ib(default=None, repr=False)
choices: Optional[Type[Enum]] = attr.ib(default=None, repr=False)

Expand All @@ -115,4 +114,5 @@ class NumberSettingDescriptor(SettingDescriptor):
max_value: int
step: int
range_attribute: Optional[str] = attr.ib(default=None)
type: SettingType = SettingType.Number
type: type = int
setting_type: SettingType = SettingType.Number
24 changes: 16 additions & 8 deletions miio/device.py
Expand Up @@ -191,21 +191,29 @@ def _setting_descriptors_from_status(
raise Exception(
f"Neither setter or setter_name was defined for {setting}"
)
setting = cast(EnumSettingDescriptor, setting)
if (
setting.type == SettingType.Enum
and setting.choices_attribute is not None
):
retrieve_choices_function = getattr(self, setting.choices_attribute)
setting.choices = retrieve_choices_function()
if setting.type == SettingType.Number:

if setting.setting_type == SettingType.Enum:
setting = cast(EnumSettingDescriptor, setting)
if setting.choices_attribute is not None:
retrieve_choices_function = getattr(self, setting.choices_attribute)
setting.choices = retrieve_choices_function()

elif setting.setting_type == SettingType.Number:
setting = cast(NumberSettingDescriptor, setting)
if setting.range_attribute is not None:
range_def = getattr(self, setting.range_attribute)
setting.min_value = range_def.min_value
setting.max_value = range_def.max_value
setting.step = range_def.step

elif setting.setting_type == SettingType.Boolean:
pass # just to exhaust known types

else:
raise NotImplementedError(
"Unknown setting type: %s" % setting.setting_type
)

return settings

def _sensor_descriptors_from_status(
Expand Down
4 changes: 4 additions & 0 deletions miio/miot_models.py
Expand Up @@ -82,6 +82,7 @@ def convert_type(cls, input: str):
"bool": bool,
"string": str,
"float": float,
"none": None,
}
return type_map[input]

Expand Down Expand Up @@ -303,6 +304,7 @@ def _descriptor_for_choices(self) -> Union[SensorDescriptor, EnumSettingDescript
unit=self.unit,
choices=choices,
extras=self.extras,
type=self.format,
)
return desc
else:
Expand All @@ -322,6 +324,7 @@ def _descriptor_for_ranged(
step=self.range[2],
unit=self.unit,
extras=self.extras,
type=self.format,
)
return desc
else:
Expand All @@ -335,6 +338,7 @@ def _create_boolean_setting(self) -> BooleanSettingDescriptor:
property=self.name,
unit=self.unit,
extras=self.extras,
type=bool,
)

def _create_sensor(self) -> SensorDescriptor:
Expand Down

0 comments on commit 39bf909

Please sign in to comment.