diff --git a/homeassistant/components/foscam/__init__.py b/homeassistant/components/foscam/__init__.py index aed3ed637ae..f8708a589ce 100644 --- a/homeassistant/components/foscam/__init__.py +++ b/homeassistant/components/foscam/__init__.py @@ -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: diff --git a/homeassistant/components/foscam/coordinator.py b/homeassistant/components/foscam/coordinator.py index 063d5235c04..e7a8abf7d30 100644 --- a/homeassistant/components/foscam/coordinator.py +++ b/homeassistant/components/foscam/coordinator.py @@ -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 diff --git a/homeassistant/components/foscam/strings.json b/homeassistant/components/foscam/strings.json index 285f0f5a780..2784e541809 100644 --- a/homeassistant/components/foscam/strings.json +++ b/homeassistant/components/foscam/strings.json @@ -25,6 +25,13 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" } }, + "entity": { + "switch": { + "sleep_switch": { + "name": "Sleep" + } + } + }, "services": { "ptz": { "name": "PTZ", diff --git a/homeassistant/components/foscam/switch.py b/homeassistant/components/foscam/switch.py new file mode 100644 index 00000000000..9eae211881f --- /dev/null +++ b/homeassistant/components/foscam/switch.py @@ -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()