Repository: dermotduffy/frigate-hass-card
Pull request #1537 (2024-09-15 17:03:40)

feat: Add `microphone_connect` action
~To be merged after #1536.~


dermotduffy: I'm not sure if this will work in the normal case, as connecting the microphone is obviously a highly sensitive action (unlike disconnecting). I am guessing the browser would reject this?
felipecrs: > I'm not sure if this will work in the normal case, as connecting the microphone is obviously a highly sensitive action (unlike disconnecting). I am guessing the browser would reject this?

Hm... for what I was wondering, it is working fine. I used it in the green call button demonstrated in: https://github.com/dermotduffy/frigate-hass-card/pull/1536#issuecomment-2351682720

Maybe however it would not work as a URL query parameter (I did not test).
dermotduffy: > Hm... for what I was wondering, it is working fine. I used it in the green call button demonstrated in: https://github.com/dermotduffy/frigate-hass-card/pull/1536#issuecomment-2351682720

Actual, on thinking about it, worst case the browser would just use ask the user as usual. 

Ready for me to look at this one?
felipecrs: > Actual, on thinking about it, worst case the browser would just use ask the user as usual.

That's correct. Anyway, this is certainly less sensitive than `microphone_unmute` (which causes `connect` as well), which is already implemented and work well.[](url)

> Ready for me to look at this one?

Yes, please. Very straightforward implementation thanks to your [instructions](https://github.com/dermotduffy/frigate-hass-card/issues/1457#issuecomment-2345258607).

Patch:
diff --git a/docs/configuration/actions/custom/README.md b/docs/configuration/actions/custom/README.md
index c269922d..5dabe356 100644
--- a/docs/configuration/actions/custom/README.md
+++ b/docs/configuration/actions/custom/README.md
@@ -248,6 +248,15 @@ action: custom:frigate-card-action
 frigate_card_action: menu_toggle
 ```
 
+## `microphone_connect`
+
+Connect the microphone for [2-way audio](../../../usage/2-way-audio.md).
+
+```yaml
+action: custom:frigate-card-action
+frigate_card_action: microphone_connect
+```
+
 ## `microphone_disconnect`
 
 Disconnect the microphone during [2-way audio](../../../usage/2-way-audio.md).
diff --git a/docs/usage/url-actions.md b/docs/usage/url-actions.md
index 8595d030..291a41da 100644
--- a/docs/usage/url-actions.md
+++ b/docs/usage/url-actions.md
@@ -46,31 +46,31 @@ unless the action is targeted with a `CARD_ID` as shown above.
 
 Only a subset of all [actions](../configuration/actions/README.md) are supported in URL form.
 
-| Action                                                          | Supported in URL         | Explanation                                                                                                         |
-| --------------------------------------------------------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------- |
-| `camera_select`                                                 | :white_check_mark:       |                                                                                                                     |
-| `camera_ui`                                                     | :white_check_mark:       |                                                                                                                     |
-| `clip`                                                          | :white_check_mark:       |                                                                                                                     |
-| `clips`                                                         | :white_check_mark:       |                                                                                                                     |
-| `default`                                                       | :white_check_mark:       |                                                                                                                     |
-| `download`                                                      | :heavy_multiplication_x: | Latest media information is not available on initial render.                                                        |
-| `expand`                                                        | :white_check_mark:       |                                                                                                                     |
-| `fullscreen`                                                    | :heavy_multiplication_x: | Javascript does not support activating fullscreen without direct human interaction. Use `expand` as an alternative. |
-| `image`                                                         | :white_check_mark:       |                                                                                                                     |
-| `live_substream_select`                                         | :white_check_mark:       |                                                                                                                     |
-| `live`                                                          | :white_check_mark:       |                                                                                                                     |
-| `media_player`                                                  | :heavy_multiplication_x: | Please [request](https://github.com/dermotduffy/frigate-hass-card/issues) if you need this.                         |
-| `menu_toggle`                                                   | :white_check_mark:       |                                                                                                                     |
-| `microphone_disconnect`, `microphone_mute`, `microphone_unmute` | :heavy_multiplication_x: |                                                                                                                     |
-| `mute`, `unmute`                                                | :heavy_multiplication_x: |                                                                                                                     |
-| `play`, `pause`                                                 | :heavy_multiplication_x: |                                                                                                                     |
-| `ptz`                                                           | :heavy_multiplication_x: | Please [request](https://github.com/dermotduffy/frigate-hass-card/issues) if you need this.                         |
-| `recording`                                                     | :white_check_mark:       |                                                                                                                     |
-| `recordings`                                                    | :white_check_mark:       |                                                                                                                     |
-| `screenshot`                                                    | :heavy_multiplication_x: | Latest media information is not available on initial render.                                                        |
-| `ptz_controls`                                                  | :heavy_multiplication_x: | Please [request](https://github.com/dermotduffy/frigate-hass-card/issues) if you need this.                         |
-| `snapshot`                                                      | :white_check_mark:       |                                                                                                                     |
-| `snapshots`                                                     | :white_check_mark:       |                                                                                                                     |
+| Action                                                                                | Supported in URL         | Explanation                                                                                                         |
+| ------------------------------------------------------------------------------------- | ------------------------ | ------------------------------------------------------------------------------------------------------------------- |
+| `camera_select`                                                                       | :white_check_mark:       |                                                                                                                     |
+| `camera_ui`                                                                           | :white_check_mark:       |                                                                                                                     |
+| `clip`                                                                                | :white_check_mark:       |                                                                                                                     |
+| `clips`                                                                               | :white_check_mark:       |                                                                                                                     |
+| `default`                                                                             | :white_check_mark:       |                                                                                                                     |
+| `download`                                                                            | :heavy_multiplication_x: | Latest media information is not available on initial render.                                                        |
+| `expand`                                                                              | :white_check_mark:       |                                                                                                                     |
+| `fullscreen`                                                                          | :heavy_multiplication_x: | Javascript does not support activating fullscreen without direct human interaction. Use `expand` as an alternative. |
+| `image`                                                                               | :white_check_mark:       |                                                                                                                     |
+| `live_substream_select`                                                               | :white_check_mark:       |                                                                                                                     |
+| `live`                                                                                | :white_check_mark:       |                                                                                                                     |
+| `media_player`                                                                        | :heavy_multiplication_x: | Please [request](https://github.com/dermotduffy/frigate-hass-card/issues) if you need this.                         |
+| `menu_toggle`                                                                         | :white_check_mark:       |                                                                                                                     |
+| `microphone_connect`, `microphone_disconnect`, `microphone_mute`, `microphone_unmute` | :heavy_multiplication_x: |                                                                                                                     |
+| `mute`, `unmute`                                                                      | :heavy_multiplication_x: |                                                                                                                     |
+| `play`, `pause`                                                                       | :heavy_multiplication_x: |                                                                                                                     |
+| `ptz`                                                                                 | :heavy_multiplication_x: | Please [request](https://github.com/dermotduffy/frigate-hass-card/issues) if you need this.                         |
+| `recording`                                                                           | :white_check_mark:       |                                                                                                                     |
+| `recordings`                                                                          | :white_check_mark:       |                                                                                                                     |
+| `screenshot`                                                                          | :heavy_multiplication_x: | Latest media information is not available on initial render.                                                        |
+| `ptz_controls`                                                                        | :heavy_multiplication_x: | Please [request](https://github.com/dermotduffy/frigate-hass-card/issues) if you need this.                         |
+| `snapshot`                                                                            | :white_check_mark:       |                                                                                                                     |
+| `snapshots`                                                                           | :white_check_mark:       |                                                                                                                     |
 
 ## Examples
 
diff --git a/src/card-controller/actions/actions/microphone-connect.ts b/src/card-controller/actions/actions/microphone-connect.ts
new file mode 100644
index 00000000..1be48dd1
--- /dev/null
+++ b/src/card-controller/actions/actions/microphone-connect.ts
@@ -0,0 +1,9 @@
+import { GeneralActionConfig } from '../../../config/types';
+import { CardActionsAPI } from '../../types';
+import { FrigateCardAction } from './base';
+
+export class MicrophoneConnectAction extends FrigateCardAction<GeneralActionConfig> {
+  public async execute(api: CardActionsAPI): Promise<void> {
+    await api.getMicrophoneManager().connect();
+  }
+}
diff --git a/src/card-controller/actions/factory.ts b/src/card-controller/actions/factory.ts
index e67d831b..2cf522bc 100644
--- a/src/card-controller/actions/factory.ts
+++ b/src/card-controller/actions/factory.ts
@@ -13,6 +13,7 @@ import { GenericAction } from './actions/generic';
 import { LogAction } from './actions/log';
 import { MediaPlayerAction } from './actions/media-player';
 import { MenuToggleAction } from './actions/menu-toggle';
+import { MicrophoneConnectAction } from './actions/microphone-connect';
 import { MicrophoneDisconnectAction } from './actions/microphone-disconnect';
 import { MicrophoneMuteAction } from './actions/microphone-mute';
 import { MicrophoneUnmuteAction } from './actions/microphone-unmute';
@@ -97,6 +98,8 @@ export class ActionFactory {
         return new SubstreamOnAction(context, frigateCardAction, options?.config);
       case 'media_player':
         return new MediaPlayerAction(context, frigateCardAction, options?.config);
+      case 'microphone_connect':
+        return new MicrophoneConnectAction(context, frigateCardAction, options?.config);
       case 'microphone_disconnect':
         return new MicrophoneDisconnectAction(
           context,
diff --git a/src/config/types.ts b/src/config/types.ts
index 0250a9b9..2e9b1f1c 100644
--- a/src/config/types.ts
+++ b/src/config/types.ts
@@ -270,6 +270,7 @@ const FRIGATE_CARD_GENERAL_ACTIONS = [
   'live_substream_off',
   'live_substream_on',
   'menu_toggle',
+  'microphone_connect',
   'microphone_disconnect',
   'microphone_mute',
   'microphone_unmute',


Test patch:
diff --git a/tests/card-controller/actions/actions/microphone-connect.test.ts b/tests/card-controller/actions/actions/microphone-connect.test.ts
new file mode 100644
index 00000000..ad252ae7
--- /dev/null
+++ b/tests/card-controller/actions/actions/microphone-connect.test.ts
@@ -0,0 +1,18 @@
+import { expect, it } from 'vitest';
+import { MicrophoneConnectAction } from '../../../../src/card-controller/actions/actions/microphone-connect';
+import { createCardAPI } from '../../../test-utils';
+
+it('should handle microphone_connect action', async () => {
+  const api = createCardAPI();
+  const action = new MicrophoneConnectAction(
+    {},
+    {
+      action: 'fire-dom-event',
+      frigate_card_action: 'microphone_connect',
+    },
+  );
+
+  await action.execute(api);
+
+  expect(api.getMicrophoneManager().connect).toBeCalled();
+});
diff --git a/tests/card-controller/actions/factory.test.ts b/tests/card-controller/actions/factory.test.ts
index 9b2cc022..becc9e7f 100644
--- a/tests/card-controller/actions/factory.test.ts
+++ b/tests/card-controller/actions/factory.test.ts
@@ -10,6 +10,7 @@ import { GenericAction } from '../../../src/card-controller/actions/actions/gene
 import { LogAction } from '../../../src/card-controller/actions/actions/log';
 import { MediaPlayerAction } from '../../../src/card-controller/actions/actions/media-player';
 import { MenuToggleAction } from '../../../src/card-controller/actions/actions/menu-toggle';
+import { MicrophoneConnectAction } from '../../../src/card-controller/actions/actions/microphone-connect';
 import { MicrophoneDisconnectAction } from '../../../src/card-controller/actions/actions/microphone-disconnect';
 import { MicrophoneMuteAction } from '../../../src/card-controller/actions/actions/microphone-mute';
 import { MicrophoneUnmuteAction } from '../../../src/card-controller/actions/actions/microphone-unmute';
@@ -104,6 +105,7 @@ describe('ActionFactory', () => {
         MediaPlayerAction,
       ],
       [{ frigate_card_action: 'menu_toggle' as const }, MenuToggleAction],
+      [{ frigate_card_action: 'microphone_connect' as const }, MicrophoneConnectAction],
       [
         { frigate_card_action: 'microphone_disconnect' as const },
         MicrophoneDisconnectAction,