mirror of
https://github.com/legop3/MultiRoombaRover.git
synced 2026-09-16 01:21:20 -04:00
add mode buttons
This commit is contained in:
@@ -43,10 +43,20 @@ homeAssistant:
|
|||||||
buttons:
|
buttons:
|
||||||
# Legacy action entities only (for example sensor.<button>_action from Zigbee2MQTT).
|
# Legacy action entities only (for example sensor.<button>_action from Zigbee2MQTT).
|
||||||
- entityId: "sensor.basement_rover_buttons_action"
|
- entityId: "sensor.basement_rover_buttons_action"
|
||||||
# Optional; if omitted, any state change triggers.
|
# Human alert button
|
||||||
stateEquals: "on"
|
stateEquals: "on"
|
||||||
cooldownMs: 15000
|
cooldownMs: 15000
|
||||||
action: "humanAlert"
|
action: "humanAlert"
|
||||||
|
- entityId: "sensor.basement_rover_buttons_action"
|
||||||
|
# Mode button: turns
|
||||||
|
stateEquals: "double"
|
||||||
|
cooldownMs: 2000
|
||||||
|
action: "modeTurns"
|
||||||
|
- entityId: "sensor.basement_rover_buttons_action"
|
||||||
|
# Mode button: admin
|
||||||
|
stateEquals: "hold"
|
||||||
|
cooldownMs: 2000
|
||||||
|
action: "modeAdmin"
|
||||||
roomCameras:
|
roomCameras:
|
||||||
- id: "lobby"
|
- id: "lobby"
|
||||||
name: "Lobby Camera"
|
name: "Lobby Camera"
|
||||||
|
|||||||
@@ -1,23 +1,49 @@
|
|||||||
const sharp = require('sharp');
|
const sharp = require('sharp');
|
||||||
const logger = require('../globals/logger').child('humanAlertButton');
|
const logger = require('../globals/logger').child('humanAlertButton');
|
||||||
const { subscribe, publishEvent } = require('./eventBus');
|
const { subscribe, publishEvent } = require('./eventBus');
|
||||||
const { getMode, MODES } = require('./modeManager');
|
const { getMode, MODES, setMode } = require('./modeManager');
|
||||||
|
const { issueCommand } = require('./commandService');
|
||||||
|
const roverManager = require('./roverManager');
|
||||||
const { getRoomCameras } = require('./roomCameraService');
|
const { getRoomCameras } = require('./roomCameraService');
|
||||||
const { getRoomCameraState } = require('./roomCameraSnapshotService');
|
const { getRoomCameraState } = require('./roomCameraSnapshotService');
|
||||||
|
|
||||||
const HA_BUTTON_EVENT_TYPE = 'ha.button.action';
|
const HA_BUTTON_EVENT_TYPE = 'ha.button.action';
|
||||||
const HUMAN_ALERT_ACTION = 'humanAlert';
|
const HUMAN_ALERT_ACTION = 'humanAlert';
|
||||||
|
const MODE_TURNS_ACTION = 'modeTurns';
|
||||||
|
const MODE_ADMIN_ACTION = 'modeAdmin';
|
||||||
const HUMAN_ALERT_MESSAGE = 'Human alert button pressed.';
|
const HUMAN_ALERT_MESSAGE = 'Human alert button pressed.';
|
||||||
|
const MODE_TURNS_TTS = 'Server mode is now turns.';
|
||||||
|
const MODE_ADMIN_TTS = 'Server mode is now admin.';
|
||||||
const TILE_WIDTH = 480;
|
const TILE_WIDTH = 480;
|
||||||
const TILE_HEIGHT = 270;
|
const TILE_HEIGHT = 270;
|
||||||
|
|
||||||
logger.info('Human alert button service enabled', { action: HUMAN_ALERT_ACTION });
|
logger.info('HA button actions enabled', {
|
||||||
|
actions: [HUMAN_ALERT_ACTION, MODE_TURNS_ACTION, MODE_ADMIN_ACTION],
|
||||||
|
});
|
||||||
|
|
||||||
function isModeAllowed() {
|
function isModeAllowed() {
|
||||||
const mode = getMode();
|
const mode = getMode();
|
||||||
return mode !== MODES.ADMIN && mode !== MODES.LOCKDOWN;
|
return mode !== MODES.ADMIN && mode !== MODES.LOCKDOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sendTtsToNonPrivateRovers(text) {
|
||||||
|
const roster = roverManager.getRoster();
|
||||||
|
roster.forEach((entry) => {
|
||||||
|
if (entry?.private?.enabled) return;
|
||||||
|
try {
|
||||||
|
issueCommand(String(entry.id), {
|
||||||
|
type: 'tts',
|
||||||
|
tts: {
|
||||||
|
text,
|
||||||
|
speak: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
logger.warn('Failed to send mode TTS', { roverId: entry?.id, error: err.message });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function buildTile(camera, state) {
|
async function buildTile(camera, state) {
|
||||||
const base = sharp({
|
const base = sharp({
|
||||||
create: {
|
create: {
|
||||||
@@ -74,7 +100,29 @@ async function buildHorizontalMosaic() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function handleTrigger(event = {}) {
|
async function handleTrigger(event = {}) {
|
||||||
if (String(event?.payload?.action || '') !== HUMAN_ALERT_ACTION) {
|
const action = String(event?.payload?.action || '');
|
||||||
|
if (!action) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (action === MODE_TURNS_ACTION) {
|
||||||
|
setMode(
|
||||||
|
MODES.TURNS,
|
||||||
|
{ data: { user: { username: 'ha-button:modeTurns' } } },
|
||||||
|
{ force: true },
|
||||||
|
);
|
||||||
|
sendTtsToNonPrivateRovers(MODE_TURNS_TTS);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (action === MODE_ADMIN_ACTION) {
|
||||||
|
setMode(
|
||||||
|
MODES.ADMIN,
|
||||||
|
{ data: { user: { username: 'ha-button:modeAdmin' } } },
|
||||||
|
{ force: true },
|
||||||
|
);
|
||||||
|
sendTtsToNonPrivateRovers(MODE_ADMIN_TTS);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (action !== HUMAN_ALERT_ACTION) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|||||||
Reference in New Issue
Block a user