mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Add refresh after turning switch on or off and type annotations to ezviz (#52469)
This commit is contained in:
parent
8d9345c407
commit
f3d95501d9
@ -1,26 +1,34 @@
|
|||||||
"""Support for Ezviz Switch sensors."""
|
"""Support for Ezviz Switch sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
from pyezviz.constants import DeviceSwitchType
|
from pyezviz.constants import DeviceSwitchType
|
||||||
|
from pyezviz.exceptions import HTTPError, PyEzvizError
|
||||||
|
|
||||||
from homeassistant.components.switch import DEVICE_CLASS_SWITCH, SwitchEntity
|
from homeassistant.components.switch import DEVICE_CLASS_SWITCH, SwitchEntity
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
|
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
|
from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
|
||||||
|
from .coordinator import EzvizDataUpdateCoordinator
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, entry, async_add_entities):
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
||||||
|
) -> None:
|
||||||
"""Set up Ezviz switch based on a config entry."""
|
"""Set up Ezviz switch based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
|
||||||
|
DATA_COORDINATOR
|
||||||
|
]
|
||||||
switch_entities = []
|
switch_entities = []
|
||||||
supported_switches = []
|
supported_switches = {switches.value for switches in DeviceSwitchType}
|
||||||
|
|
||||||
for switches in DeviceSwitchType:
|
|
||||||
supported_switches.append(switches.value)
|
|
||||||
|
|
||||||
supported_switches = set(supported_switches)
|
|
||||||
|
|
||||||
for idx, camera in enumerate(coordinator.data):
|
for idx, camera in enumerate(coordinator.data):
|
||||||
if not camera.get("switches"):
|
if not camera.get("switches"):
|
||||||
@ -36,7 +44,11 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
class EzvizSwitch(CoordinatorEntity, SwitchEntity):
|
class EzvizSwitch(CoordinatorEntity, SwitchEntity):
|
||||||
"""Representation of a Ezviz sensor."""
|
"""Representation of a Ezviz sensor."""
|
||||||
|
|
||||||
def __init__(self, coordinator, idx, switch):
|
coordinator: EzvizDataUpdateCoordinator
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, coordinator: EzvizDataUpdateCoordinator, idx: int, switch: str
|
||||||
|
) -> None:
|
||||||
"""Initialize the switch."""
|
"""Initialize the switch."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self._idx = idx
|
self._idx = idx
|
||||||
@ -47,34 +59,48 @@ class EzvizSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
self._device_class = DEVICE_CLASS_SWITCH
|
self._device_class = DEVICE_CLASS_SWITCH
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return the name of the Ezviz switch."""
|
"""Return the name of the Ezviz switch."""
|
||||||
return f"{self._camera_name}.{DeviceSwitchType(self._name).name}"
|
return f"{DeviceSwitchType(self._name).name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self) -> bool:
|
||||||
"""Return the state of the switch."""
|
"""Return the state of the switch."""
|
||||||
return self.coordinator.data[self._idx]["switches"][self._name]
|
return self.coordinator.data[self._idx]["switches"][self._name]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self) -> str:
|
||||||
"""Return the unique ID of this switch."""
|
"""Return the unique ID of this switch."""
|
||||||
return f"{self._serial}_{self._sensor_name}"
|
return f"{self._serial}_{self._sensor_name}"
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||||
"""Change a device switch on the camera."""
|
"""Change a device switch on the camera."""
|
||||||
_LOGGER.debug("Set EZVIZ Switch '%s' to on", self._name)
|
try:
|
||||||
|
update_ok = await self.hass.async_add_executor_job(
|
||||||
|
self.coordinator.ezviz_client.switch_status, self._serial, self._name, 1
|
||||||
|
)
|
||||||
|
|
||||||
self.coordinator.ezviz_client.switch_status(self._serial, self._name, 1)
|
except (HTTPError, PyEzvizError) as err:
|
||||||
|
raise PyEzvizError("Failed to turn on switch {self._name}") from err
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
if update_ok:
|
||||||
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||||
"""Change a device switch on the camera."""
|
"""Change a device switch on the camera."""
|
||||||
_LOGGER.debug("Set EZVIZ Switch '%s' to off", self._name)
|
try:
|
||||||
|
update_ok = await self.hass.async_add_executor_job(
|
||||||
|
self.coordinator.ezviz_client.switch_status, self._serial, self._name, 0
|
||||||
|
)
|
||||||
|
|
||||||
self.coordinator.ezviz_client.switch_status(self._serial, self._name, 0)
|
except (HTTPError, PyEzvizError) as err:
|
||||||
|
raise PyEzvizError(f"Failed to turn off switch {self._name}") from err
|
||||||
|
|
||||||
|
if update_ok:
|
||||||
|
await self.coordinator.async_request_refresh()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return the device_info of the device."""
|
"""Return the device_info of the device."""
|
||||||
return {
|
return {
|
||||||
"identifiers": {(DOMAIN, self._serial)},
|
"identifiers": {(DOMAIN, self._serial)},
|
||||||
@ -85,6 +111,6 @@ class EzvizSwitch(CoordinatorEntity, SwitchEntity):
|
|||||||
}
|
}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self) -> str:
|
||||||
"""Device class for the sensor."""
|
"""Device class for the sensor."""
|
||||||
return self._device_class
|
return self._device_class
|
||||||
|
Loading…
x
Reference in New Issue
Block a user