1 """Component providing basic support for Foscam IP cameras."""
3 from __future__
import annotations
7 import voluptuous
as vol
24 from .coordinator
import FoscamCoordinator
25 from .entity
import FoscamEntity
32 DIR_TOPLEFT =
"top_left"
33 DIR_TOPRIGHT =
"top_right"
34 DIR_BOTTOMLEFT =
"bottom_left"
35 DIR_BOTTOMRIGHT =
"bottom_right"
38 DIR_UP:
"ptz_move_up",
39 DIR_DOWN:
"ptz_move_down",
40 DIR_LEFT:
"ptz_move_left",
41 DIR_RIGHT:
"ptz_move_right",
42 DIR_TOPLEFT:
"ptz_move_top_left",
43 DIR_TOPRIGHT:
"ptz_move_top_right",
44 DIR_BOTTOMLEFT:
"ptz_move_bottom_left",
45 DIR_BOTTOMRIGHT:
"ptz_move_bottom_right",
48 DEFAULT_TRAVELTIME = 0.125
50 ATTR_MOVEMENT =
"movement"
51 ATTR_TRAVELTIME =
"travel_time"
52 ATTR_PRESET_NAME =
"preset_name"
54 PTZ_GOTO_PRESET_COMMAND =
"ptz_goto_preset"
59 config_entry: ConfigEntry,
60 async_add_entities: AddEntitiesCallback,
62 """Add a Foscam IP camera from a config entry."""
63 platform = entity_platform.async_get_current_platform()
64 platform.async_register_entity_service(
67 vol.Required(ATTR_MOVEMENT): vol.In(
79 vol.Optional(ATTR_TRAVELTIME, default=DEFAULT_TRAVELTIME): cv.small_float,
84 platform.async_register_entity_service(
87 vol.Required(ATTR_PRESET_NAME): cv.string,
89 "async_perform_ptz_preset",
92 coordinator: FoscamCoordinator = hass.data[DOMAIN][config_entry.entry_id]
98 """An implementation of a Foscam IP camera."""
100 _attr_has_entity_name =
True
105 coordinator: FoscamCoordinator,
106 config_entry: ConfigEntry,
108 """Initialize a Foscam camera."""
109 super().
__init__(coordinator, config_entry.entry_id)
110 Camera.__init__(self)
113 self.
_username_username = config_entry.data[CONF_USERNAME]
114 self.
_password_password = config_entry.data[CONF_PASSWORD]
115 self.
_stream_stream = config_entry.data[CONF_STREAM]
117 self.
_rtsp_port_rtsp_port = config_entry.data[CONF_RTSP_PORT]
122 """Handle entity addition to hass."""
127 ret, response = await self.
hasshasshass.async_add_executor_job(
134 "Can't get motion detection status, camera %s configured with"
142 "Error getting motion detection status of %s: %s", self.
namenamename, ret
149 self, width: int |
None =
None, height: int |
None =
None
151 """Return a still image response from the camera."""
154 result, response = self.
_foscam_session_foscam_session.snap_picture_2()
161 """Return the stream source."""
163 return f
"rtsp://{self._username}:{self._password}@{self._foscam_session.host}:{self._rtsp_port}/video{self._stream}"
168 """Enable motion detection in camera."""
176 "Can't set motion detection status, camera %s configured"
177 " with non-admin user"
187 "Failed enabling motion detection on '%s'. Is it supported by the"
194 """Disable motion detection."""
202 "Can't set motion detection status, camera %s configured"
203 " with non-admin user"
213 "Failed disabling motion detection on '%s'. Is it supported by the"
220 """Perform a PTZ action on the camera."""
221 LOGGER.debug(
"PTZ action '%s' on %s", movement, self.
namenamename)
223 movement_function = getattr(self.
_foscam_session_foscam_session, MOVEMENT_ATTRS[movement])
225 ret, _ = await self.
hasshasshass.async_add_executor_job(movement_function)
228 LOGGER.error(
"Error moving %s '%s': %s", movement, self.
namenamename, ret)
231 await asyncio.sleep(travel_time)
233 ret, _ = await self.
hasshasshass.async_add_executor_job(
238 LOGGER.error(
"Error stopping movement on '%s': %s", self.
namenamename, ret)
242 """Perform a PTZ preset action on the camera."""
243 LOGGER.debug(
"PTZ preset '%s' on %s", preset_name, self.
namenamename)
245 preset_function = getattr(self.
_foscam_session_foscam_session, PTZ_GOTO_PRESET_COMMAND)
247 ret, _ = await self.
hasshasshass.async_add_executor_job(preset_function, preset_name)
251 "Error moving to preset %s on '%s': %s", preset_name, self.
namenamename, ret
def async_perform_ptz_preset(self, preset_name)
None disable_motion_detection(self)
bytes|None camera_image(self, int|None width=None, int|None height=None)
_attr_motion_detection_enabled
None async_added_to_hass(self)
str|None stream_source(self)
None __init__(self, FoscamCoordinator coordinator, ConfigEntry config_entry)
None enable_motion_detection(self)
def async_perform_ptz(self, movement, travel_time)
str|UndefinedType|None name(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)