Refactor Shelly wrapper to coordinator (#79628)

This commit is contained in:
Shay Levy 2022-10-05 15:39:58 +03:00 committed by GitHub
parent 4d3d22320f
commit 22c68b95bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 336 additions and 321 deletions

View file

@ -15,8 +15,8 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import BlockDeviceWrapper, RpcDeviceWrapper
from .const import BLOCK, DATA_CONFIG_ENTRY, DOMAIN, RPC
from .coordinator import ShellyBlockCoordinator, ShellyRpcCoordinator
from .entity import ShellyBlockEntity, ShellyRpcEntity
from .utils import get_device_entry_gen, get_rpc_key_ids
@ -40,13 +40,13 @@ def async_setup_block_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up cover for device."""
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][BLOCK]
blocks = [block for block in wrapper.device.blocks if block.type == "roller"]
coordinator = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][BLOCK]
blocks = [block for block in coordinator.device.blocks if block.type == "roller"]
if not blocks:
return
async_add_entities(BlockShellyCover(wrapper, block) for block in blocks)
async_add_entities(BlockShellyCover(coordinator, block) for block in blocks)
@callback
@ -56,14 +56,14 @@ def async_setup_rpc_entry(
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up entities for RPC device."""
wrapper = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][RPC]
coordinator = hass.data[DOMAIN][DATA_CONFIG_ENTRY][config_entry.entry_id][RPC]
cover_key_ids = get_rpc_key_ids(wrapper.device.status, "cover")
cover_key_ids = get_rpc_key_ids(coordinator.device.status, "cover")
if not cover_key_ids:
return
async_add_entities(RpcShellyCover(wrapper, id_) for id_ in cover_key_ids)
async_add_entities(RpcShellyCover(coordinator, id_) for id_ in cover_key_ids)
class BlockShellyCover(ShellyBlockEntity, CoverEntity):
@ -71,14 +71,14 @@ class BlockShellyCover(ShellyBlockEntity, CoverEntity):
_attr_device_class = CoverDeviceClass.SHUTTER
def __init__(self, wrapper: BlockDeviceWrapper, block: Block) -> None:
def __init__(self, coordinator: ShellyBlockCoordinator, block: Block) -> None:
"""Initialize block cover."""
super().__init__(wrapper, block)
super().__init__(coordinator, block)
self.control_result: dict[str, Any] | None = None
self._attr_supported_features: int = (
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP
)
if self.wrapper.device.settings["rollers"][0]["positioning"]:
if self.coordinator.device.settings["rollers"][0]["positioning"]:
self._attr_supported_features |= CoverEntityFeature.SET_POSITION
@property
@ -147,9 +147,9 @@ class RpcShellyCover(ShellyRpcEntity, CoverEntity):
_attr_device_class = CoverDeviceClass.SHUTTER
def __init__(self, wrapper: RpcDeviceWrapper, id_: int) -> None:
def __init__(self, coordinator: ShellyRpcCoordinator, id_: int) -> None:
"""Initialize rpc cover."""
super().__init__(wrapper, f"cover:{id_}")
super().__init__(coordinator, f"cover:{id_}")
self._id = id_
self._attr_supported_features: int = (
CoverEntityFeature.OPEN | CoverEntityFeature.CLOSE | CoverEntityFeature.STOP