fix: Improve 2-way audio detection (#2293)

- For Frigate cameras, you must be running at least [integration
v5.12.0](https://github.com/blakeblackshear/frigate-hass-integration/releases/tag/v5.12.0).
 - Closes: #2191 

Includes a significant refactor of cameras, and the introduction of a
`2-way-audio` capability that is dynamically fetched from `go2rtc`. One
gotcha is if you previously had a substream with 2-way audio, you may
need to modify

```yaml
 - camera_entity: camera.foo
    capabilities:
      disable_except:
        - substream
```

... to ...

```yaml
 - camera_entity: camera.foo
    capabilities:
      disable_except:
        - substream
        - 2-way-audio
```
This commit is contained in:
Dermot Duffy
2025-12-20 13:06:09 -08:00
committed by GitHub
parent 588ed6c98a
commit 1e821f09c6
47 changed files with 2240 additions and 958 deletions
+159
View File
@@ -2,6 +2,7 @@ import { describe, expect, it } from 'vitest';
import { CameraProxyConfig } from '../../src/camera-manager/types.js';
import {
addDynamicProxyURL,
createProxiedEndpointIfNecessary,
getWebProxiedURL,
shouldUseWebProxy,
} from '../../src/ha/web-proxy.js';
@@ -115,3 +116,161 @@ describe('addDynamicProxyURL', () => {
);
});
});
describe('createProxiedEndpointIfNecessary', () => {
const createProxyConfig = (
config: Partial<CameraProxyConfig> = {},
): CameraProxyConfig => ({
media: true,
live: true,
ssl_verification: true,
ssl_ciphers: 'default',
dynamic: true,
...config,
});
const testEndpoint = { endpoint: 'http://example.com/stream', sign: false };
it('should return original endpoint when proxyConfig is undefined', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(hass, testEndpoint);
expect(result).toBe(testEndpoint);
});
it('should return original endpoint when proxy is not available', async () => {
const hass = createHASS();
hass.config.components = [];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig(),
);
expect(result).toBe(testEndpoint);
});
it('should return original endpoint when context is not enabled', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig({ media: false }),
{ context: 'media' },
);
expect(result).toBe(testEndpoint);
});
it('should return proxied endpoint with dynamic registration', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig(),
{ context: 'media', ttl: 300, openLimit: 5 },
);
expect(hass.callService).toHaveBeenCalledWith(
'hass_web_proxy',
'create_proxied_url',
expect.objectContaining({
url_pattern: 'http://example.com/stream',
ttl: 300,
open_limit: 5,
}),
);
expect(result).toEqual({
endpoint: '/api/hass_web_proxy/v0/?url=http%3A%2F%2Fexample.com%2Fstream',
sign: true,
});
});
it('should strip hash fragment when registering dynamic proxy', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const endpointWithHash = {
endpoint: 'http://example.com/stream#fragment',
sign: false,
};
await createProxiedEndpointIfNecessary(hass, endpointWithHash, createProxyConfig());
expect(hass.callService).toHaveBeenCalledWith(
'hass_web_proxy',
'create_proxied_url',
expect.objectContaining({
url_pattern: 'http://example.com/stream',
}),
);
});
it('should return proxied endpoint without dynamic registration', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig({ dynamic: false }),
);
expect(hass.callService).not.toHaveBeenCalled();
expect(result).toEqual({
endpoint: '/api/hass_web_proxy/v0/?url=http%3A%2F%2Fexample.com%2Fstream',
sign: true,
});
});
it('should return websocket proxied endpoint', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig({ dynamic: false }),
{ websocket: true },
);
expect(result).toEqual({
endpoint: '/api/hass_web_proxy/v0/ws?url=http%3A%2F%2Fexample.com%2Fstream',
sign: true,
});
});
it('should use live context when specified', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
const result = await createProxiedEndpointIfNecessary(
hass,
testEndpoint,
createProxyConfig({ media: false, live: true }),
{ context: 'live' },
);
expect(result.endpoint).toContain('/api/hass_web_proxy/');
});
it('should default openLimit to 0 when not specified', async () => {
const hass = createHASS();
hass.config.components = ['hass_web_proxy'];
await createProxiedEndpointIfNecessary(hass, testEndpoint, createProxyConfig());
expect(hass.callService).toHaveBeenCalledWith(
'hass_web_proxy',
'create_proxied_url',
expect.objectContaining({
open_limit: 0,
}),
);
});
});