mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Use attr** in somfy-mylink (#62381)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
afc42ff835
commit
07e1e174ac
@ -1,12 +1,7 @@
|
|||||||
"""Cover Platform for the Somfy MyLink component."""
|
"""Cover Platform for the Somfy MyLink component."""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.cover import (
|
from homeassistant.components.cover import CoverDeviceClass, CoverEntity
|
||||||
DEVICE_CLASS_BLIND,
|
|
||||||
DEVICE_CLASS_SHUTTER,
|
|
||||||
DEVICE_CLASS_WINDOW,
|
|
||||||
CoverEntity,
|
|
||||||
)
|
|
||||||
from homeassistant.const import STATE_CLOSED, STATE_OPEN
|
from homeassistant.const import STATE_CLOSED, STATE_OPEN
|
||||||
from homeassistant.helpers.entity import DeviceInfo
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.restore_state import RestoreEntity
|
from homeassistant.helpers.restore_state import RestoreEntity
|
||||||
@ -21,7 +16,10 @@ from .const import (
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
MYLINK_COVER_TYPE_TO_DEVICE_CLASS = {0: DEVICE_CLASS_BLIND, 1: DEVICE_CLASS_SHUTTER}
|
MYLINK_COVER_TYPE_TO_DEVICE_CLASS = {
|
||||||
|
0: CoverDeviceClass.BLIND,
|
||||||
|
1: CoverDeviceClass.SHUTTER,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
@ -38,7 +36,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
"target_id": cover["targetID"],
|
"target_id": cover["targetID"],
|
||||||
"name": cover["name"],
|
"name": cover["name"],
|
||||||
"device_class": MYLINK_COVER_TYPE_TO_DEVICE_CLASS.get(
|
"device_class": MYLINK_COVER_TYPE_TO_DEVICE_CLASS.get(
|
||||||
cover.get("type"), DEVICE_CLASS_WINDOW
|
cover.get("type"), CoverDeviceClass.WINDOW
|
||||||
),
|
),
|
||||||
"reverse": reversed_target_ids.get(cover["targetID"], False),
|
"reverse": reversed_target_ids.get(cover["targetID"], False),
|
||||||
}
|
}
|
||||||
@ -57,76 +55,33 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
class SomfyShade(RestoreEntity, CoverEntity):
|
class SomfyShade(RestoreEntity, CoverEntity):
|
||||||
"""Object for controlling a Somfy cover."""
|
"""Object for controlling a Somfy cover."""
|
||||||
|
|
||||||
|
_attr_should_poll = False
|
||||||
|
_attr_assumed_state = True
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
somfy_mylink,
|
somfy_mylink,
|
||||||
target_id,
|
target_id,
|
||||||
name="SomfyShade",
|
name="SomfyShade",
|
||||||
reverse=False,
|
reverse=False,
|
||||||
device_class=DEVICE_CLASS_WINDOW,
|
device_class=CoverDeviceClass.WINDOW,
|
||||||
):
|
):
|
||||||
"""Initialize the cover."""
|
"""Initialize the cover."""
|
||||||
self.somfy_mylink = somfy_mylink
|
self.somfy_mylink = somfy_mylink
|
||||||
self._target_id = target_id
|
self._target_id = target_id
|
||||||
self._name = name
|
self._attr_unique_id = target_id
|
||||||
|
self._attr_name = name
|
||||||
self._reverse = reverse
|
self._reverse = reverse
|
||||||
self._closed = None
|
self._attr_device_class = device_class
|
||||||
self._is_opening = None
|
self._attr_device_info = DeviceInfo(
|
||||||
self._is_closing = None
|
|
||||||
self._device_class = device_class
|
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self):
|
|
||||||
"""No polling since assumed state."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return the unique ID of this cover."""
|
|
||||||
return self._target_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the cover."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def assumed_state(self):
|
|
||||||
"""Let HA know the integration is assumed state."""
|
|
||||||
return True
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
|
||||||
return self._device_class
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_opening(self):
|
|
||||||
"""Return if the cover is opening."""
|
|
||||||
return self._is_opening
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_closing(self):
|
|
||||||
"""Return if the cover is closing."""
|
|
||||||
return self._is_closing
|
|
||||||
|
|
||||||
@property
|
|
||||||
def is_closed(self) -> bool:
|
|
||||||
"""Return if the cover is closed."""
|
|
||||||
return self._closed
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_info(self) -> DeviceInfo:
|
|
||||||
"""Return the device_info of the device."""
|
|
||||||
return DeviceInfo(
|
|
||||||
identifiers={(DOMAIN, self._target_id)},
|
identifiers={(DOMAIN, self._target_id)},
|
||||||
manufacturer=MANUFACTURER,
|
manufacturer=MANUFACTURER,
|
||||||
name=self._name,
|
name=name,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_close_cover(self, **kwargs):
|
async def async_close_cover(self, **kwargs):
|
||||||
"""Close the cover."""
|
"""Close the cover."""
|
||||||
self._is_closing = True
|
self._attr_is_closing = True
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
try:
|
try:
|
||||||
# Blocks until the close command is sent
|
# Blocks until the close command is sent
|
||||||
@ -134,14 +89,14 @@ class SomfyShade(RestoreEntity, CoverEntity):
|
|||||||
await self.somfy_mylink.move_down(self._target_id)
|
await self.somfy_mylink.move_down(self._target_id)
|
||||||
else:
|
else:
|
||||||
await self.somfy_mylink.move_up(self._target_id)
|
await self.somfy_mylink.move_up(self._target_id)
|
||||||
self._closed = True
|
self._attr_is_closed = True
|
||||||
finally:
|
finally:
|
||||||
self._is_closing = None
|
self._attr_is_closing = None
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_open_cover(self, **kwargs):
|
async def async_open_cover(self, **kwargs):
|
||||||
"""Open the cover."""
|
"""Open the cover."""
|
||||||
self._is_opening = True
|
self._attr_is_opening = True
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
try:
|
try:
|
||||||
# Blocks until the open command is sent
|
# Blocks until the open command is sent
|
||||||
@ -149,9 +104,9 @@ class SomfyShade(RestoreEntity, CoverEntity):
|
|||||||
await self.somfy_mylink.move_up(self._target_id)
|
await self.somfy_mylink.move_up(self._target_id)
|
||||||
else:
|
else:
|
||||||
await self.somfy_mylink.move_down(self._target_id)
|
await self.somfy_mylink.move_down(self._target_id)
|
||||||
self._closed = False
|
self._attr_is_closed = False
|
||||||
finally:
|
finally:
|
||||||
self._is_opening = None
|
self._attr_is_opening = None
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_stop_cover(self, **kwargs):
|
async def async_stop_cover(self, **kwargs):
|
||||||
@ -168,4 +123,4 @@ class SomfyShade(RestoreEntity, CoverEntity):
|
|||||||
STATE_OPEN,
|
STATE_OPEN,
|
||||||
STATE_CLOSED,
|
STATE_CLOSED,
|
||||||
):
|
):
|
||||||
self._closed = last_state.state == STATE_CLOSED
|
self._attr_is_closed = last_state.state == STATE_CLOSED
|
||||||
|
Loading…
x
Reference in New Issue
Block a user