style: code style

This commit is contained in:
LiShuzhen 2025-03-07 09:27:34 +08:00
parent 6d63975eba
commit 7777c4f596

View File

@ -69,9 +69,9 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry,
device_list: list[MIoTDevice] = hass.data[DOMAIN]['devices'][
config_entry.entry_id]
# 读取配置参数
close_threshold = config_entry.options.get('close_threshold', 3)
open_threshold = config_entry.options.get('open_threshold', 95)
# Get the cover config params
close_threshold = config_entry.options.get('close_threshold', None)
open_threshold = config_entry.options.get('open_threshold', None)
new_entities = []
for miot_device in device_list:
@ -84,10 +84,11 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry,
data.spec.device_class = CoverDeviceClass.SHUTTER
elif data.spec.name == 'airer':
data.spec.device_class = CoverDeviceClass.BLIND
new_entities.append(Cover(miot_device=miot_device,
entity_data=data,
close_threshold=close_threshold,
open_threshold=open_threshold))
new_entities.append(
Cover(miot_device=miot_device,
entity_data=data,
close_threshold=close_threshold,
open_threshold=open_threshold))
if new_entities:
async_add_entities(new_entities)
@ -112,11 +113,13 @@ class Cover(MIoTServiceEntity, CoverEntity):
_prop_position_value_range: Optional[int]
_prop_pos_closing: bool
_prop_pos_opening: bool
_close_threshold: Optional[int]
_open_threshold: Optional[int]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData,
close_threshold: int = 3,
open_threshold: int = 95) -> None:
close_threshold: int,
open_threshold: int) -> None:
"""Initialize the Cover."""
super().__init__(miot_device=miot_device, entity_data=entity_data)
self._attr_device_class = entity_data.spec.device_class
@ -140,6 +143,11 @@ class Cover(MIoTServiceEntity, CoverEntity):
self._prop_pos_closing = False
self._prop_pos_opening = False
# Check the validity of the input parameters.
if close_threshold is not None and open_threshold is not None:
if close_threshold >= open_threshold:
close_threshold = None
open_threshold = None
self._close_threshold = close_threshold
self._open_threshold = open_threshold
@ -274,8 +282,16 @@ class Cover(MIoTServiceEntity, CoverEntity):
self._prop_pos_closing = False
return self.get_prop_value(prop=self._prop_target_position)
pos = self.get_prop_value(prop=self._prop_current_position)
return None if pos is None else round(pos * 100 /
self._prop_position_value_range)
if pos is None:
return None
percentage = round(pos * 100 / self._prop_position_value_range)
if (self._close_threshold is not None) and (percentage
<= self._close_threshold):
return 0
if (self._open_threshold is not None) and (percentage
>= self._open_threshold):
return 100
return percentage
@property
def is_opening(self) -> Optional[bool]: