From 03ed552ca9ea9dcd85cd26f62ff0bd1a1134a562 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 22 Aug 2022 20:23:15 +0200 Subject: [PATCH] Improve type hint in foscam camera entity (#77166) --- homeassistant/components/foscam/camera.py | 52 ++++++++--------------- 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/homeassistant/components/foscam/camera.py b/homeassistant/components/foscam/camera.py index cf2740ab9cc..c0b6661a392 100644 --- a/homeassistant/components/foscam/camera.py +++ b/homeassistant/components/foscam/camera.py @@ -94,18 +94,17 @@ async def async_setup_entry( class HassFoscamCamera(Camera): """An implementation of a Foscam IP camera.""" - def __init__(self, camera, config_entry): + def __init__(self, camera: FoscamCamera, config_entry: ConfigEntry) -> None: """Initialize a Foscam camera.""" super().__init__() self._foscam_session = camera - self._name = config_entry.title + self._attr_name = config_entry.title self._username = config_entry.data[CONF_USERNAME] self._password = config_entry.data[CONF_PASSWORD] self._stream = config_entry.data[CONF_STREAM] - self._unique_id = config_entry.entry_id + self._attr_unique_id = config_entry.entry_id self._rtsp_port = config_entry.data[CONF_RTSP_PORT] - self._motion_status = False if self._rtsp_port: self._attr_supported_features = CameraEntityFeature.STREAM @@ -119,21 +118,16 @@ class HassFoscamCamera(Camera): if ret == -3: LOGGER.info( "Can't get motion detection status, camera %s configured with non-admin user", - self._name, + self.name, ) elif ret != 0: LOGGER.error( - "Error getting motion detection status of %s: %s", self._name, ret + "Error getting motion detection status of %s: %s", self.name, ret ) else: - self._motion_status = response == 1 - - @property - def unique_id(self): - """Return the entity unique ID.""" - return self._unique_id + self._attr_motion_detection_enabled = response == 1 def camera_image( self, width: int | None = None, height: int | None = None @@ -147,18 +141,13 @@ class HassFoscamCamera(Camera): return response - async def stream_source(self): + async def stream_source(self) -> str | None: """Return the stream source.""" if self._rtsp_port: return f"rtsp://{self._username}:{self._password}@{self._foscam_session.host}:{self._rtsp_port}/video{self._stream}" return None - @property - def motion_detection_enabled(self): - """Camera Motion Detection Status.""" - return self._motion_status - def enable_motion_detection(self) -> None: """Enable motion detection in camera.""" try: @@ -168,15 +157,15 @@ class HassFoscamCamera(Camera): if ret == -3: LOGGER.info( "Can't set motion detection status, camera %s configured with non-admin user", - self._name, + self.name, ) return - self._motion_status = True + self._attr_motion_detection_enabled = True except TypeError: LOGGER.debug( "Failed enabling motion detection on '%s'. Is it supported by the device?", - self._name, + self.name, ) def disable_motion_detection(self) -> None: @@ -188,27 +177,27 @@ class HassFoscamCamera(Camera): if ret == -3: LOGGER.info( "Can't set motion detection status, camera %s configured with non-admin user", - self._name, + self.name, ) return - self._motion_status = False + self._attr_motion_detection_enabled = False except TypeError: LOGGER.debug( "Failed disabling motion detection on '%s'. Is it supported by the device?", - self._name, + self.name, ) async def async_perform_ptz(self, movement, travel_time): """Perform a PTZ action on the camera.""" - LOGGER.debug("PTZ action '%s' on %s", movement, self._name) + LOGGER.debug("PTZ action '%s' on %s", movement, self.name) movement_function = getattr(self._foscam_session, MOVEMENT_ATTRS[movement]) ret, _ = await self.hass.async_add_executor_job(movement_function) if ret != 0: - LOGGER.error("Error moving %s '%s': %s", movement, self._name, ret) + LOGGER.error("Error moving %s '%s': %s", movement, self.name, ret) return await asyncio.sleep(travel_time) @@ -218,12 +207,12 @@ class HassFoscamCamera(Camera): ) if ret != 0: - LOGGER.error("Error stopping movement on '%s': %s", self._name, ret) + LOGGER.error("Error stopping movement on '%s': %s", self.name, ret) return async def async_perform_ptz_preset(self, preset_name): """Perform a PTZ preset action on the camera.""" - LOGGER.debug("PTZ preset '%s' on %s", preset_name, self._name) + LOGGER.debug("PTZ preset '%s' on %s", preset_name, self.name) preset_function = getattr(self._foscam_session, PTZ_GOTO_PRESET_COMMAND) @@ -231,11 +220,6 @@ class HassFoscamCamera(Camera): if ret != 0: LOGGER.error( - "Error moving to preset %s on '%s': %s", preset_name, self._name, ret + "Error moving to preset %s on '%s': %s", preset_name, self.name, ret ) return - - @property - def name(self): - """Return the name of this camera.""" - return self._name