Add Foscam sleep switch (#109491)
* Add sleep switch * Replace awake with sleep switchpull/122848/head
parent
d78acd480a
commit
53a59412bb
|
@ -17,7 +17,7 @@ from .config_flow import DEFAULT_RTSP_PORT
|
|||
from .const import CONF_RTSP_PORT, DOMAIN, LOGGER, SERVICE_PTZ, SERVICE_PTZ_PRESET
|
||||
from .coordinator import FoscamCoordinator
|
||||
|
||||
PLATFORMS = [Platform.CAMERA]
|
||||
PLATFORMS = [Platform.CAMERA, Platform.SWITCH]
|
||||
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
|
|
|
@ -44,4 +44,9 @@ class FoscamCoordinator(DataUpdateCoordinator[dict[str, Any]]):
|
|||
self.session.get_product_all_info
|
||||
)
|
||||
data["product_info"] = all_info[1]
|
||||
|
||||
ret, is_asleep = await self.hass.async_add_executor_job(
|
||||
self.session.is_asleep
|
||||
)
|
||||
data["is_asleep"] = {"supported": ret == 0, "status": is_asleep}
|
||||
return data
|
||||
|
|
|
@ -25,6 +25,13 @@
|
|||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"switch": {
|
||||
"sleep_switch": {
|
||||
"name": "Sleep"
|
||||
}
|
||||
}
|
||||
},
|
||||
"services": {
|
||||
"ptz": {
|
||||
"name": "PTZ",
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
"""Component provides support for the Foscam Switch."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.components.switch import SwitchEntity
|
||||
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 . import FoscamCoordinator
|
||||
from .const import DOMAIN, LOGGER
|
||||
from .entity import FoscamEntity
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up foscam switch from a config entry."""
|
||||
|
||||
coordinator: FoscamCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||
|
||||
await coordinator.async_config_entry_first_refresh()
|
||||
|
||||
if coordinator.data["is_asleep"]["supported"]:
|
||||
async_add_entities([FoscamSleepSwitch(coordinator, config_entry)])
|
||||
|
||||
|
||||
class FoscamSleepSwitch(FoscamEntity, SwitchEntity):
|
||||
"""An implementation for Sleep Switch."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: FoscamCoordinator,
|
||||
config_entry: ConfigEntry,
|
||||
) -> None:
|
||||
"""Initialize a Foscam Sleep Switch."""
|
||||
super().__init__(coordinator, config_entry.entry_id)
|
||||
|
||||
self._attr_unique_id = "sleep_switch"
|
||||
self._attr_translation_key = "sleep_switch"
|
||||
self._attr_has_entity_name = True
|
||||
|
||||
self.is_asleep = self.coordinator.data["is_asleep"]["status"]
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if camera is asleep."""
|
||||
return self.is_asleep
|
||||
|
||||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Wake camera."""
|
||||
LOGGER.debug("Wake camera")
|
||||
|
||||
ret, _ = await self.hass.async_add_executor_job(
|
||||
self.coordinator.session.wake_up
|
||||
)
|
||||
|
||||
if ret != 0:
|
||||
raise HomeAssistantError(f"Error waking up: {ret}")
|
||||
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""But camera is sleep."""
|
||||
LOGGER.debug("Sleep camera")
|
||||
|
||||
ret, _ = await self.hass.async_add_executor_job(self.coordinator.session.sleep)
|
||||
|
||||
if ret != 0:
|
||||
raise HomeAssistantError(f"Error sleeping: {ret}")
|
||||
|
||||
await self.coordinator.async_request_refresh()
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
|
||||
self.is_asleep = self.coordinator.data["is_asleep"]["status"]
|
||||
|
||||
self.async_write_ha_state()
|
Loading…
Reference in New Issue