This commit is contained in:
Li Shuzhen 2025-01-23 11:27:16 +00:00 committed by GitHub
commit f7c7754f1f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 12 deletions

View File

@ -60,16 +60,16 @@ from homeassistant.components.cover import (
) )
from .miot.miot_spec import MIoTSpecProperty from .miot.miot_spec import MIoTSpecProperty
from .miot.miot_device import MIoTDevice, MIoTEntityData, MIoTServiceEntity from .miot.miot_device import MIoTDevice, MIoTEntityData, MIoTServiceEntity
from .miot.const import DOMAIN from .miot.const import DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_setup_entry( async def async_setup_entry(
hass: HomeAssistant, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback
) -> None: ) -> None:
"""Set up a config entry.""" """Set up a config entry."""
device_list: list[MIoTDevice] = hass.data[DOMAIN]['devices'][ device_list: list[MIoTDevice] = hass.data[DOMAIN]['devices'][
@ -82,6 +82,10 @@ async def async_setup_entry(
data.spec.device_class = CoverDeviceClass.CURTAIN data.spec.device_class = CoverDeviceClass.CURTAIN
elif data.spec.name == 'window-opener': elif data.spec.name == 'window-opener':
data.spec.device_class = CoverDeviceClass.WINDOW data.spec.device_class = CoverDeviceClass.WINDOW
elif data.spec.name == 'motor-controller':
data.spec.device_class = CoverDeviceClass.SHUTTER
elif data.spec.name == 'airer':
data.spec.device_class = CoverDeviceClass.BLIND
new_entities.append( new_entities.append(
Cover(miot_device=miot_device, entity_data=data)) Cover(miot_device=miot_device, entity_data=data))
@ -100,6 +104,7 @@ class Cover(MIoTServiceEntity, CoverEntity):
_prop_status_opening: Optional[int] _prop_status_opening: Optional[int]
_prop_status_closing: Optional[int] _prop_status_closing: Optional[int]
_prop_status_stop: Optional[int] _prop_status_stop: Optional[int]
_prop_status_closed: Optional[int]
_prop_current_position: Optional[MIoTSpecProperty] _prop_current_position: Optional[MIoTSpecProperty]
_prop_target_position: Optional[MIoTSpecProperty] _prop_target_position: Optional[MIoTSpecProperty]
_prop_position_value_min: Optional[int] _prop_position_value_min: Optional[int]
@ -123,6 +128,7 @@ class Cover(MIoTServiceEntity, CoverEntity):
self._prop_status_opening = None self._prop_status_opening = None
self._prop_status_closing = None self._prop_status_closing = None
self._prop_status_stop = None self._prop_status_stop = None
self._prop_status_closed = None
self._prop_current_position = None self._prop_current_position = None
self._prop_target_position = None self._prop_target_position = None
self._prop_position_value_min = None self._prop_position_value_min = None
@ -137,15 +143,15 @@ class Cover(MIoTServiceEntity, CoverEntity):
'motor-control value_list is None, %s', self.entity_id) 'motor-control value_list is None, %s', self.entity_id)
continue continue
for item in prop.value_list.items: for item in prop.value_list.items:
if item.name in {'open'}: if item.name in {'open', 'up'}:
self._attr_supported_features |= ( self._attr_supported_features |= (
CoverEntityFeature.OPEN) CoverEntityFeature.OPEN)
self._prop_motor_value_open = item.value self._prop_motor_value_open = item.value
elif item.name in {'close'}: elif item.name in {'close', 'down'}:
self._attr_supported_features |= ( self._attr_supported_features |= (
CoverEntityFeature.CLOSE) CoverEntityFeature.CLOSE)
self._prop_motor_value_close = item.value self._prop_motor_value_close = item.value
elif item.name in {'pause'}: elif item.name in {'pause', 'stop'}:
self._attr_supported_features |= ( self._attr_supported_features |= (
CoverEntityFeature.STOP) CoverEntityFeature.STOP)
self._prop_motor_value_pause = item.value self._prop_motor_value_pause = item.value
@ -156,12 +162,14 @@ class Cover(MIoTServiceEntity, CoverEntity):
'status value_list is None, %s', self.entity_id) 'status value_list is None, %s', self.entity_id)
continue continue
for item in prop.value_list.items: for item in prop.value_list.items:
if item.name in {'opening', 'open'}: if item.name in {'opening', 'open', 'up'}:
self._prop_status_opening = item.value self._prop_status_opening = item.value
elif item.name in {'closing', 'close'}: elif item.name in {'closing', 'close', 'down'}:
self._prop_status_closing = item.value self._prop_status_closing = item.value
elif item.name in {'stop', 'pause'}: elif item.name in {'stop', 'pause'}:
self._prop_status_stop = item.value self._prop_status_stop = item.value
elif item.name in {'closed'}:
self._prop_status_closed = item.value
self._prop_status = prop self._prop_status = prop
elif prop.name == 'current-position': elif prop.name == 'current-position':
self._prop_current_position = prop self._prop_current_position = prop
@ -209,8 +217,10 @@ class Cover(MIoTServiceEntity, CoverEntity):
0: the cover is closed, 100: the cover is fully opened, None: unknown. 0: the cover is closed, 100: the cover is fully opened, None: unknown.
""" """
if self._prop_current_position is None:
return None
pos = self.get_prop_value(prop=self._prop_current_position) pos = self.get_prop_value(prop=self._prop_current_position)
if pos is None: if pos is None or self._prop_position_value_range is None:
return None return None
return round(pos*100/self._prop_position_value_range) return round(pos*100/self._prop_position_value_range)
@ -233,4 +243,11 @@ class Cover(MIoTServiceEntity, CoverEntity):
@property @property
def is_closed(self) -> Optional[bool]: def is_closed(self) -> Optional[bool]:
"""Return if the cover is closed.""" """Return if the cover is closed."""
return self.get_prop_value(prop=self._prop_current_position) == 0 if self._prop_current_position:
return self.get_prop_value(prop=self._prop_current_position) == 0
# The current position is prior to the status when determining
# whether the cover is closed.
return (self.get_prop_value(
prop=self._prop_status) == self._prop_status_closed) if (
self._prop_status
and self._prop_status_closed is not None) else None

View File

@ -324,7 +324,9 @@ SPEC_SERVICE_TRANS_MAP: dict = {
}, },
'entity': 'cover' 'entity': 'cover'
}, },
'window-opener': 'curtain' 'window-opener': 'curtain',
'motor-controller': 'curtain',
'airer': 'curtain'
} }
"""SPEC_PROP_TRANS_MAP """SPEC_PROP_TRANS_MAP