Skip to content

Commit

Permalink
Fix doorbell bug
Browse files Browse the repository at this point in the history
  • Loading branch information
aneisch committed Nov 1, 2023
1 parent 9ae6b4b commit cc3cabb
Show file tree
Hide file tree
Showing 26 changed files with 271 additions and 136 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Also using Grafana/Influx for graphing, both running in Docker containers on NUC
Description | value
-- | --
Lines of ESPHome YAML | 2467
Lines of Home Assistant YAML | 8142
Lines of Home Assistant YAML | 8150
[Integrations](https://www.home-assistant.io/integrations/) in use | 52
Zigbee devices in [`zha`](https://www.home-assistant.io/integrations/zha/) | 26
Z-Wave devices in [`zwave_js`](https://www.home-assistant.io/integrations/zwave_js/) | 37
Expand All @@ -82,15 +82,15 @@ Entities in the [`input_select`](https://www.home-assistant.io/components/input_
Entities in the [`input_text`](https://www.home-assistant.io/components/input_text) domain | 8
Entities in the [`light`](https://www.home-assistant.io/components/light) domain | 39
Entities in the [`lock`](https://www.home-assistant.io/components/lock) domain | 3
Entities in the [`media_player`](https://www.home-assistant.io/components/media_player) domain | 12
Entities in the [`media_player`](https://www.home-assistant.io/components/media_player) domain | 13
Entities in the [`number`](https://www.home-assistant.io/components/number) domain | 5
Entities in the [`person`](https://www.home-assistant.io/components/person) domain | 2
Entities in the [`plant`](https://www.home-assistant.io/components/plant) domain | 1
Entities in the [`remote`](https://www.home-assistant.io/components/remote) domain | 1
Entities in the [`scene`](https://www.home-assistant.io/components/scene) domain | 2
Entities in the [`script`](https://www.home-assistant.io/components/script) domain | 40
Entities in the [`select`](https://www.home-assistant.io/components/select) domain | 2
Entities in the [`sensor`](https://www.home-assistant.io/components/sensor) domain | 384
Entities in the [`sensor`](https://www.home-assistant.io/components/sensor) domain | 385
Entities in the [`siren`](https://www.home-assistant.io/components/siren) domain | 1
Entities in the [`sun`](https://www.home-assistant.io/components/sun) domain | 1
Entities in the [`switch`](https://www.home-assistant.io/components/switch) domain | 157
Expand All @@ -100,7 +100,7 @@ Entities in the [`update`](https://www.home-assistant.io/components/update) doma
Entities in the [`vacuum`](https://www.home-assistant.io/components/vacuum) domain | 1
Entities in the [`weather`](https://www.home-assistant.io/components/weather) domain | 2
Entities in the [`zone`](https://www.home-assistant.io/components/zone) domain | 6
**Total state objects** | **1068**
**Total state objects** | **1070**
## The HACS integrations/plugins that I use:
**Appdaemon**:<br>
[aneisch/follow_me_appdaemon](https://github.com/aneisch/follow_me_appdaemon)<br>
Expand Down
1 change: 0 additions & 1 deletion automations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,6 @@
data:
message: >
Rain started!
- delay: 300
- alias: "Frozen Refrigerator"
id: frozen_refrigerator
Expand Down
13 changes: 6 additions & 7 deletions custom_components/frigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"""
from __future__ import annotations

from collections.abc import Callable
from datetime import timedelta
import logging
import re
from typing import Any, Callable, Final
from typing import Any, Final

from awesomeversion import AwesomeVersion

Expand Down Expand Up @@ -71,8 +72,7 @@ def get_frigate_device_identifier(
"""Get a device identifier."""
if camera_name:
return (DOMAIN, f"{entry.entry_id}:{slugify(camera_name)}")
else:
return (DOMAIN, entry.entry_id)
return (DOMAIN, entry.entry_id)


def get_frigate_entity_unique_id(
Expand Down Expand Up @@ -168,10 +168,9 @@ async def async_setup(hass: HomeAssistant, config: Config) -> bool:
"""Set up this integration using YAML is not supported."""
integration = await async_get_integration(hass, DOMAIN)
_LOGGER.info(
STARTUP_MESSAGE.format(
title=NAME,
integration_version=integration.version,
)
STARTUP_MESSAGE,
NAME,
integration.version,
)

hass.data.setdefault(DOMAIN, {})
Expand Down
12 changes: 12 additions & 0 deletions custom_components/frigate/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ async def async_get_config(self) -> dict[str, Any]:
await self.api_wrapper("get", str(URL(self._host) / "api/config")),
)

async def async_get_ptz_info(
self,
camera: str,
decode_json: bool = True,
) -> Any:
"""Get PTZ info."""
return await self.api_wrapper(
"get",
str(URL(self._host) / "api" / camera / "ptz/info"),
decode_json=decode_json,
)

async def async_get_path(self, path: str) -> Any:
"""Get data from the API."""
return await self.api_wrapper("get", str(URL(self._host) / f"{path}/"))
Expand Down
60 changes: 40 additions & 20 deletions custom_components/frigate/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from yarl import URL

from custom_components.frigate.api import FrigateApiClient
from homeassistant.components.camera import Camera, CameraEntityFeature
from homeassistant.components.camera import Camera, CameraEntityFeature, StreamType
from homeassistant.components.mqtt import async_publish
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_URL
Expand Down Expand Up @@ -44,6 +44,7 @@
ATTR_PTZ_ACTION,
ATTR_PTZ_ARGUMENT,
ATTR_START_TIME,
CONF_ENABLE_WEBRTC,
CONF_RTMP_URL_TEMPLATE,
CONF_RTSP_URL_TEMPLATE,
DEVICE_CLASS_CAMERA,
Expand Down Expand Up @@ -177,6 +178,7 @@ def __init__(
# The device_class is used to filter out regular camera entities
# from motion camera entities on selectors
self._attr_device_class = DEVICE_CLASS_CAMERA
self._stream_source = None
self._attr_is_streaming = (
self._camera_config.get("rtmp", {}).get("enabled")
or self._cam_name
Expand All @@ -197,24 +199,28 @@ def __init__(
self._cam_name
in self._frigate_config.get("go2rtc", {}).get("streams", {}).keys()
):
self._restream_type = "rtsp"
streaming_template = config_entry.options.get(
CONF_RTSP_URL_TEMPLATE, ""
).strip()

if streaming_template:
# Can't use homeassistant.helpers.template as it requires hass which
# is not available in the constructor, so use direct jinja2
# template instead. This means templates cannot access HomeAssistant
# state, but rather only the camera config.
self._stream_source = Template(streaming_template).render(
**self._camera_config
)
if config_entry.options.get(CONF_ENABLE_WEBRTC, False):
self._restream_type = "webrtc"
self._attr_frontend_stream_type = StreamType.WEB_RTC
else:
self._stream_source = (
f"rtsp://{URL(self._url).host}:8554/{self._cam_name}"
)

self._restream_type = "rtsp"
self._attr_frontend_stream_type = StreamType.HLS
streaming_template = config_entry.options.get(
CONF_RTSP_URL_TEMPLATE, ""
).strip()

if streaming_template:
# Can't use homeassistant.helpers.template as it requires hass which
# is not available in the constructor, so use direct jinja2
# template instead. This means templates cannot access HomeAssistant
# state, but rather only the camera config.
self._stream_source = Template(streaming_template).render(
**self._camera_config
)
else:
self._stream_source = (
f"rtsp://{URL(self._url).host}:8554/{self._cam_name}"
)
elif self._camera_config.get("rtmp", {}).get("enabled"):
self._restream_type = "rtmp"
streaming_template = config_entry.options.get(
Expand Down Expand Up @@ -252,7 +258,12 @@ def _motion_message_received(self, msg: ReceiveMessage) -> None:
def available(self) -> bool:
"""Signal when frigate loses connection to camera."""
if self.coordinator.data:
if self.coordinator.data.get(self._cam_name, {}).get("camera_fps", 0) == 0:
if (
self.coordinator.data.get("cameras", {})
.get(self._cam_name, {})
.get("camera_fps", 0)
== 0
):
return False
return super().available

Expand Down Expand Up @@ -318,6 +329,15 @@ async def stream_source(self) -> str | None:
return None
return self._stream_source

async def async_handle_web_rtc_offer(self, offer_sdp: str) -> str | None:
"""Handle the WebRTC offer and return an answer."""
websession = cast(aiohttp.ClientSession, async_get_clientsession(self.hass))
url = f"{self._url}/api/go2rtc/webrtc?src={self._cam_name}"
payload = {"type": "offer", "sdp": offer_sdp}
async with websession.post(url, json=payload) as resp:
answer = await resp.json()
return cast(str, answer["sdp"])

async def async_enable_motion_detection(self) -> None:
"""Enable motion detection for this camera."""
await async_publish(
Expand Down Expand Up @@ -526,7 +546,7 @@ async def async_camera_image(
return self._last_image

@property
def state(self) -> str:
def state(self) -> str: # pylint: disable=overridden-final-method
"""Return the camera state."""
if self._last_image is None:
return STATE_IDLE
Expand Down
11 changes: 10 additions & 1 deletion custom_components/frigate/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .api import FrigateApiClient, FrigateApiClientError
from .const import (
CONF_ENABLE_WEBRTC,
CONF_MEDIA_BROWSER_ENABLE,
CONF_NOTIFICATION_PROXY_ENABLE,
CONF_NOTIFICATION_PROXY_EXPIRE_AFTER_SECONDS,
Expand Down Expand Up @@ -83,7 +84,7 @@ def _show_config_form(
self,
user_input: dict[str, Any] | None = None,
errors: dict[str, Any] | None = None,
) -> dict[str, Any]: # pylint: disable=unused-argument
) -> dict[str, Any]:
"""Show the configuration form."""
if user_input is None:
user_input = {}
Expand Down Expand Up @@ -134,6 +135,14 @@ async def async_step_init(
)

schema: dict[Any, Any] = {
# Whether to enable webrtc as the medium for camera streaming
vol.Optional(
CONF_ENABLE_WEBRTC,
default=self._config_entry.options.get(
CONF_ENABLE_WEBRTC,
False,
),
): bool,
# The input URL is not validated as being a URL to allow for the
# possibility the template input won't be a valid URL until after
# it's rendered.
Expand Down
7 changes: 4 additions & 3 deletions custom_components/frigate/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Base component constants
NAME = "Frigate"
DOMAIN = "frigate"
FRIGATE_VERSION_ERROR_CUTOFF = "0.8.4"
FRIGATE_VERSION_ERROR_CUTOFF = "0.12.1"
FRIGATE_RELEASES_URL = "https://github.com/blakeblackshear/frigate/releases"
FRIGATE_RELEASE_TAG_URL = f"{FRIGATE_RELEASES_URL}/tag"

Expand Down Expand Up @@ -51,6 +51,7 @@
CONF_PATH = "path"
CONF_RTMP_URL_TEMPLATE = "rtmp_url_template"
CONF_RTSP_URL_TEMPLATE = "rtsp_url_template"
CONF_ENABLE_WEBRTC = "enable_webrtc"
CONF_NOTIFICATION_PROXY_EXPIRE_AFTER_SECONDS = "notification_proxy_expire_after_seconds"

# Defaults
Expand All @@ -60,8 +61,8 @@

STARTUP_MESSAGE = """
-------------------------------------------------------------------
{title}
Integration Version: {integration_version}
%s
Integration Version: %s
This is a custom integration!
If you have any issues with this you need to open an issue here:
https://github.com/blakeblackshear/frigate-hass-integration/issues
Expand Down
8 changes: 5 additions & 3 deletions custom_components/frigate/diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Diagnostics support for Frigate."""

from typing import Any, Dict
from __future__ import annotations

from typing import Any

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
Expand All @@ -11,15 +13,15 @@
REDACT_CONFIG = {CONF_PASSWORD, CONF_PATH}


def get_redacted_data(data: Dict[str, Any]) -> Any:
def get_redacted_data(data: dict[str, Any]) -> Any:
"""Redact sensitive vales from data."""
return async_redact_data(data, REDACT_CONFIG)


async def async_get_config_entry_diagnostics(
hass: HomeAssistant,
entry: ConfigEntry,
) -> Dict[str, Any]:
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""

config = hass.data[DOMAIN][entry.entry_id][ATTR_CONFIG]
Expand Down
2 changes: 1 addition & 1 deletion custom_components/frigate/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
"iot_class": "local_push",
"issue_tracker": "https://github.com/blakeblackshear/frigate-hass-integration/issues",
"requirements": ["pytz==2022.7"],
"version": "5.0.0-beta.2"
"version": "5.0.0-beta.3"
}
Loading

0 comments on commit cc3cabb

Please sign in to comment.