Compare commits

...

7 Commits

2 changed files with 58 additions and 20 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,28 +217,56 @@ 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:
# Assume that the current position is the same as the target
# position when the current position is not defined in the device's
# MIoT-Spec-V2.
return None if (self._prop_target_position
is None) else self.get_prop_value(
prop=self._prop_target_position)
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)
@property @property
def is_opening(self) -> Optional[bool]: def is_opening(self) -> Optional[bool]:
"""Return if the cover is opening.""" """Return if the cover is opening."""
if self._prop_status is None: if self._prop_status and self._prop_status_opening is not None:
return None return self.get_prop_value(
return self.get_prop_value( prop=self._prop_status) == self._prop_status_opening
prop=self._prop_status) == self._prop_status_opening # The status is prior to the numerical relationship of the current
# position and the target position when determining whether the cover
# is opening.
if (self._prop_target_position and
self.current_cover_position is not None):
return (self.current_cover_position
< self.get_prop_value(prop=self._prop_target_position))
return None
@property @property
def is_closing(self) -> Optional[bool]: def is_closing(self) -> Optional[bool]:
"""Return if the cover is closing.""" """Return if the cover is closing."""
if self._prop_status is None: if self._prop_status and self._prop_status_closing is not None:
return None return self.get_prop_value(
return self.get_prop_value( prop=self._prop_status) == self._prop_status_closing
prop=self._prop_status) == self._prop_status_closing # The status is prior to the numerical relationship of the current
# position and the target position when determining whether the cover
# is closing.
if (self._prop_target_position and
self.current_cover_position is not None):
return (self.current_cover_position
> self.get_prop_value(prop=self._prop_target_position))
return None
@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.current_cover_position is not None:
return self.current_cover_position == 0
# The current position is prior to the status when determining
# whether the cover is closed.
if self._prop_status and self._prop_status_closed is not None:
return (self.get_prop_value(
prop=self._prop_status) == self._prop_status_closed)
return 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