mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
Improve myq error handling for opening/closing cover (#54724)
This commit is contained in:
parent
6da83b90f7
commit
67e9035e4e
@ -18,6 +18,7 @@ from homeassistant.components.cover import (
|
|||||||
CoverEntity,
|
CoverEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.const import STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING
|
from homeassistant.const import STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING
|
||||||
|
from homeassistant.exceptions import HomeAssistantError
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DOMAIN, MYQ_COORDINATOR, MYQ_GATEWAY, MYQ_TO_HASS
|
from .const import DOMAIN, MYQ_COORDINATOR, MYQ_GATEWAY, MYQ_TO_HASS
|
||||||
@ -43,14 +44,11 @@ class MyQDevice(CoordinatorEntity, CoverEntity):
|
|||||||
"""Initialize with API object, device id."""
|
"""Initialize with API object, device id."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._device = device
|
self._device = device
|
||||||
|
if device.device_type == MYQ_DEVICE_TYPE_GATE:
|
||||||
@property
|
self._attr_device_class = DEVICE_CLASS_GATE
|
||||||
def device_class(self):
|
else:
|
||||||
"""Define this cover as a garage door."""
|
self._attr_device_class = DEVICE_CLASS_GARAGE
|
||||||
device_type = self._device.device_type
|
self._attr_unique_id = device.device_id
|
||||||
if device_type is not None and device_type == MYQ_DEVICE_TYPE_GATE:
|
|
||||||
return DEVICE_CLASS_GATE
|
|
||||||
return DEVICE_CLASS_GARAGE
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
@ -60,11 +58,8 @@ class MyQDevice(CoordinatorEntity, CoverEntity):
|
|||||||
@property
|
@property
|
||||||
def available(self):
|
def available(self):
|
||||||
"""Return if the device is online."""
|
"""Return if the device is online."""
|
||||||
if not self.coordinator.last_update_success:
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Not all devices report online so assume True if its missing
|
# Not all devices report online so assume True if its missing
|
||||||
return self._device.device_json[MYQ_DEVICE_STATE].get(
|
return super().available and self._device.device_json[MYQ_DEVICE_STATE].get(
|
||||||
MYQ_DEVICE_STATE_ONLINE, True
|
MYQ_DEVICE_STATE_ONLINE, True
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -93,11 +88,6 @@ class MyQDevice(CoordinatorEntity, CoverEntity):
|
|||||||
"""Flag supported features."""
|
"""Flag supported features."""
|
||||||
return SUPPORT_OPEN | SUPPORT_CLOSE
|
return SUPPORT_OPEN | SUPPORT_CLOSE
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return a unique, Home Assistant friendly identifier for this entity."""
|
|
||||||
return self._device.device_id
|
|
||||||
|
|
||||||
async def async_close_cover(self, **kwargs):
|
async def async_close_cover(self, **kwargs):
|
||||||
"""Issue close command to cover."""
|
"""Issue close command to cover."""
|
||||||
if self.is_closing or self.is_closed:
|
if self.is_closing or self.is_closed:
|
||||||
@ -106,23 +96,21 @@ class MyQDevice(CoordinatorEntity, CoverEntity):
|
|||||||
try:
|
try:
|
||||||
wait_task = await self._device.close(wait_for_state=False)
|
wait_task = await self._device.close(wait_for_state=False)
|
||||||
except MyQError as err:
|
except MyQError as err:
|
||||||
_LOGGER.error(
|
raise HomeAssistantError(
|
||||||
"Closing of cover %s failed with error: %s", self._device.name, str(err)
|
f"Closing of cover {self._device.name} failed with error: {err}"
|
||||||
)
|
) from err
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
# Write closing state to HASS
|
# Write closing state to HASS
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
result = wait_task if isinstance(wait_task, bool) else await wait_task
|
result = wait_task if isinstance(wait_task, bool) else await wait_task
|
||||||
|
|
||||||
if not result:
|
|
||||||
_LOGGER.error("Closing of cover %s failed", self._device.name)
|
|
||||||
|
|
||||||
# Write final state to HASS
|
# Write final state to HASS
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
if not result:
|
||||||
|
raise HomeAssistantError(f"Closing of cover {self._device.name} failed")
|
||||||
|
|
||||||
async def async_open_cover(self, **kwargs):
|
async def async_open_cover(self, **kwargs):
|
||||||
"""Issue open command to cover."""
|
"""Issue open command to cover."""
|
||||||
if self.is_opening or self.is_open:
|
if self.is_opening or self.is_open:
|
||||||
@ -131,22 +119,21 @@ class MyQDevice(CoordinatorEntity, CoverEntity):
|
|||||||
try:
|
try:
|
||||||
wait_task = await self._device.open(wait_for_state=False)
|
wait_task = await self._device.open(wait_for_state=False)
|
||||||
except MyQError as err:
|
except MyQError as err:
|
||||||
_LOGGER.error(
|
raise HomeAssistantError(
|
||||||
"Opening of cover %s failed with error: %s", self._device.name, str(err)
|
f"Opening of cover {self._device.name} failed with error: {err}"
|
||||||
)
|
) from err
|
||||||
return
|
|
||||||
|
|
||||||
# Write opening state to HASS
|
# Write opening state to HASS
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
result = wait_task if isinstance(wait_task, bool) else await wait_task
|
result = wait_task if isinstance(wait_task, bool) else await wait_task
|
||||||
|
|
||||||
if not result:
|
|
||||||
_LOGGER.error("Opening of cover %s failed", self._device.name)
|
|
||||||
|
|
||||||
# Write final state to HASS
|
# Write final state to HASS
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
|
if not result:
|
||||||
|
raise HomeAssistantError(f"Opening of cover {self._device.name} failed")
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return the device_info of the device."""
|
"""Return the device_info of the device."""
|
||||||
|
@ -90,7 +90,7 @@ class MyQLight(CoordinatorEntity, LightEntity):
|
|||||||
f"Turning light {self._device.name} off failed with error: {err}"
|
f"Turning light {self._device.name} off failed with error: {err}"
|
||||||
) from err
|
) from err
|
||||||
|
|
||||||
# Write opening state to HASS
|
# Write new state to HASS
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
Loading…
x
Reference in New Issue
Block a user