Add Shelly gen2 cover support (#67705)

* Add Shelly gen2 cover support

* Make status property
This commit is contained in:
Shay Levy 2022-03-08 11:58:08 +02:00 committed by GitHub
parent 0b7b1baf30
commit 2d4d18ab90
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 207 additions and 14 deletions

View file

@ -18,15 +18,28 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import BlockDeviceWrapper
from .const import BLOCK, DATA_CONFIG_ENTRY, DOMAIN
from .entity import ShellyBlockEntity
from . import BlockDeviceWrapper, RpcDeviceWrapper
from .const import BLOCK, DATA_CONFIG_ENTRY, DOMAIN, RPC
from .entity import ShellyBlockEntity, ShellyRpcEntity
from .utils import get_device_entry_gen, get_rpc_key_ids
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up switches for device."""
if get_device_entry_gen(config_entry) == 2:
return await async_setup_rpc_entry(hass, config_entry, async_add_entities)
return await async_setup_block_entry(hass, config_entry, async_add_entities)
async def async_setup_block_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up cover for device."""
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][BLOCK]
@ -35,16 +48,32 @@ async def async_setup_entry(
if not blocks:
return
async_add_entities(ShellyCover(wrapper, block) for block in blocks)
async_add_entities(BlockShellyCover(wrapper, block) for block in blocks)
class ShellyCover(ShellyBlockEntity, CoverEntity):
"""Switch that controls a cover block on Shelly devices."""
async def async_setup_rpc_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entities for RPC device."""
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][RPC]
cover_key_ids = get_rpc_key_ids(wrapper.device.status, "cover")
if not cover_key_ids:
return
async_add_entities(RpcShellyCover(wrapper, id_) for id_ in cover_key_ids)
class BlockShellyCover(ShellyBlockEntity, CoverEntity):
"""Entity that controls a cover on block based Shelly devices."""
_attr_device_class = CoverDeviceClass.SHUTTER
def __init__(self, wrapper: BlockDeviceWrapper, block: Block) -> None:
"""Initialize light."""
"""Initialize block cover."""
super().__init__(wrapper, block)
self.control_result: dict[str, Any] | None = None
self._attr_supported_features: int = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
@ -110,3 +139,61 @@ class ShellyCover(ShellyBlockEntity, CoverEntity):
"""When device updates, clear control result that overrides state."""
self.control_result = None
super()._update_callback()
class RpcShellyCover(ShellyRpcEntity, CoverEntity):
"""Entity that controls a cover on RPC based Shelly devices."""
_attr_device_class = CoverDeviceClass.SHUTTER
def __init__(self, wrapper: RpcDeviceWrapper, id_: int) -> None:
"""Initialize rpc cover."""
super().__init__(wrapper, f"cover:{id_}")
self._id = id_
self._attr_supported_features: int = SUPPORT_OPEN | SUPPORT_CLOSE | SUPPORT_STOP
if self.status["pos_control"]:
self._attr_supported_features |= SUPPORT_SET_POSITION
@property
def is_closed(self) -> bool | None:
"""If cover is closed."""
if not self.status["pos_control"]:
return None
return cast(bool, self.status["state"] == "closed")
@property
def current_cover_position(self) -> int | None:
"""Position of the cover."""
if not self.status["pos_control"]:
return None
return cast(int, self.status["current_pos"])
@property
def is_closing(self) -> bool:
"""Return if the cover is closing."""
return cast(bool, self.status["state"] == "closing")
@property
def is_opening(self) -> bool:
"""Return if the cover is opening."""
return cast(bool, self.status["state"] == "opening")
async def async_close_cover(self, **kwargs: Any) -> None:
"""Close cover."""
await self.call_rpc("Cover.Close", {"id": self._id})
async def async_open_cover(self, **kwargs: Any) -> None:
"""Open cover."""
await self.call_rpc("Cover.Open", {"id": self._id})
async def async_set_cover_position(self, **kwargs: Any) -> None:
"""Move the cover to a specific position."""
await self.call_rpc(
"Cover.GoToPosition", {"id": self._id, "pos": kwargs[ATTR_POSITION]}
)
async def async_stop_cover(self, **_kwargs: Any) -> None:
"""Stop the cover."""
await self.call_rpc("Cover.Stop", {"id": self._id})