Add Yolink lock support (#73069)
* Add yolink lock support * Update .coveragerct * extract the commons
This commit is contained in:
parent
1744e7224b
commit
db53ab5fd0
9 changed files with 105 additions and 34 deletions
|
@ -6,7 +6,6 @@ from dataclasses import dataclass
|
|||
from typing import Any
|
||||
|
||||
from yolink.device import YoLinkDevice
|
||||
from yolink.exception import YoLinkAuthFailError, YoLinkClientError
|
||||
|
||||
from homeassistant.components.switch import (
|
||||
SwitchDeviceClass,
|
||||
|
@ -15,7 +14,6 @@ from homeassistant.components.switch import (
|
|||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .const import ATTR_COORDINATORS, ATTR_DEVICE_OUTLET, DOMAIN
|
||||
|
@ -80,8 +78,7 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
|
|||
description: YoLinkSwitchEntityDescription,
|
||||
) -> None:
|
||||
"""Init YoLink Outlet."""
|
||||
super().__init__(coordinator)
|
||||
self.config_entry = config_entry
|
||||
super().__init__(config_entry, coordinator)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.device.device_id} {self.entity_description.key}"
|
||||
|
@ -100,17 +97,7 @@ class YoLinkSwitchEntity(YoLinkEntity, SwitchEntity):
|
|||
|
||||
async def call_state_change(self, state: str) -> None:
|
||||
"""Call setState api to change outlet state."""
|
||||
try:
|
||||
# call_device_http_api will check result, fail by raise YoLinkClientError
|
||||
await self.coordinator.device.call_device_http_api(
|
||||
"setState", {"state": state}
|
||||
)
|
||||
except YoLinkAuthFailError as yl_auth_err:
|
||||
self.config_entry.async_start_reauth(self.hass)
|
||||
raise HomeAssistantError(yl_auth_err) from yl_auth_err
|
||||
except YoLinkClientError as yl_client_err:
|
||||
self.coordinator.last_update_success = False
|
||||
raise HomeAssistantError(yl_client_err) from yl_client_err
|
||||
await self.call_device_api("setState", {"state": state})
|
||||
self._attr_is_on = self.entity_description.value(state)
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue