Refactor android_ip_webcam (#67664)

* Add native camera platform

* Remove double mjpeg platform

* Fix docstring

* Remove not needed update
pull/67693/head
Martin Hjelmare 2022-03-05 08:37:58 +01:00 committed by GitHub
parent c5f7e7d1b0
commit cdb463ea55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 7 deletions

View File

@ -1,4 +1,6 @@
"""Support for Android IP Webcam."""
from __future__ import annotations
import asyncio
from datetime import timedelta
@ -10,7 +12,6 @@ from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_PLATFORM,
CONF_PORT,
CONF_SCAN_INTERVAL,
CONF_SENSORS,
@ -194,9 +195,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
async def async_setup_ipcamera(cam_config):
"""Set up an IP camera."""
host = cam_config[CONF_HOST]
username = cam_config.get(CONF_USERNAME)
password = cam_config.get(CONF_PASSWORD)
name = cam_config[CONF_NAME]
username: str | None = cam_config.get(CONF_USERNAME)
password: str | None = cam_config.get(CONF_PASSWORD)
name: str = cam_config[CONF_NAME]
interval = cam_config[CONF_SCAN_INTERVAL]
switches = cam_config.get(CONF_SWITCHES)
sensors = cam_config.get(CONF_SENSORS)
@ -238,17 +239,32 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
webcams[host] = cam
mjpeg_camera = {
CONF_PLATFORM: "mjpeg",
CONF_MJPEG_URL: cam.mjpeg_url,
CONF_STILL_IMAGE_URL: cam.image_url,
CONF_NAME: name,
}
if username and password:
mjpeg_camera.update({CONF_USERNAME: username, CONF_PASSWORD: password})
# Remove incorrect config entry setup via mjpeg platform discovery.
mjpeg_config_entry = next(
(
config_entry
for config_entry in hass.config_entries.async_entries("mjpeg")
if all(
config_entry.options.get(key) == val
for key, val in mjpeg_camera.items()
)
),
None,
)
if mjpeg_config_entry:
await hass.config_entries.async_remove(mjpeg_config_entry.entry_id)
mjpeg_camera[CONF_NAME] = name
hass.async_create_task(
discovery.async_load_platform(
hass, Platform.CAMERA, "mjpeg", mjpeg_camera, config
hass, Platform.CAMERA, DOMAIN, mjpeg_camera, config
)
)

View File

@ -0,0 +1,44 @@
"""Support for Android IP Webcam Cameras."""
from __future__ import annotations
from homeassistant.components.mjpeg import MjpegCamera, filter_urllib3_logging
from homeassistant.const import HTTP_BASIC_AUTHENTICATION
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the IP Webcam camera."""
if discovery_info is None:
return
filter_urllib3_logging()
async_add_entities([IPWebcamCamera(**discovery_info)])
class IPWebcamCamera(MjpegCamera):
"""Representation of a IP Webcam camera."""
def __init__(
self,
name: str,
mjpeg_url: str,
still_image_url: str,
username: str | None = None,
password: str = "",
) -> None:
"""Initialize the camera."""
super().__init__(
name=name,
mjpeg_url=mjpeg_url,
still_image_url=still_image_url,
authentication=HTTP_BASIC_AUTHENTICATION,
username=username,
password=password,
)