Lights Off Timer: Jellyfin sleep timer that turns off Home Assistant lights
Jellyfin 12.2 plugin that adds a sleep-timer button next to the volume control in the web player (end of this episode/movie, or N minutes). When playback stops with the timer on, it calls a Home Assistant webhook that turns the lights off after a cooldown; starting playback cancels it. The repository doubles as a Jellyfin plugin repository: manifest.json at the root points at the release zips in dist/. build.sh builds in the .NET 10 SDK container and adds new versions to the manifest. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
8d456442e6
15 files changed
+934
No files matched your search
@@ -0,0 +1,147 @@
|
||||
// Lights Off Timer: sleep-timer button in the Jellyfin Web video player, next to the volume control.
|
||||
(function () {
|
||||
'use strict';
|
||||
if (window.__lightsOffTimer) { return; }
|
||||
window.__lightsOffTimer = true;
|
||||
|
||||
const ICON = '<svg viewBox="0 0 24 24" width="1em" height="1em" aria-hidden="true" style="fill:currentColor;vertical-align:middle">'
|
||||
+ '<path d="M17.75,4.09L15.22,6.03L16.13,9.09L13.5,7.28L10.87,9.09L11.78,6.03L9.25,4.09L12.44,4L13.5,1L14.56,4L17.75,4.09M21.25,11L19.61,12.25L20.2,14.23L18.5,13.06L16.8,14.23L17.39,12.25L15.75,11L17.81,10.95L18.5,9L19.19,10.95L21.25,11M18.97,15.95C19.8,15.87 20.69,17.05 20.16,17.8C19.84,18.25 19.5,18.67 19.08,19.07C15.17,23 8.84,23 4.94,19.07C1.03,15.17 1.03,8.83 4.94,4.93C5.34,4.53 5.76,4.17 6.21,3.85C6.96,3.32 8.14,4.21 8.06,5.04C7.79,7.9 8.75,10.87 10.95,13.06C13.14,15.26 16.1,16.22 18.97,15.95M17.33,17.97C14.5,17.81 11.7,16.64 9.53,14.5C7.36,12.31 6.2,9.5 6.04,6.68C3.23,9.82 3.34,14.64 6.35,17.66C9.37,20.67 14.19,20.78 17.33,17.97Z"/></svg>';
|
||||
|
||||
const css = document.createElement('style');
|
||||
css.textContent = `
|
||||
.btnLightsOffTimer { position: relative; margin-left: .8em; overflow: visible !important; }
|
||||
.btnLightsOffTimer .lotIcon { font-size: 1.66em; display: inline-flex; }
|
||||
.btnLightsOffTimer.lotActive { color: #ffcc66; }
|
||||
.btnLightsOffTimer .lotBadge { position: absolute; left: 50%; bottom: -0.15em; transform: translateX(-50%); font-size: 0.55em; font-weight: 700;
|
||||
letter-spacing: .02em; white-space: nowrap; background: #ffcc66; color: #111; border-radius: 0.6em; padding: 0 0.4em; line-height: 1.45em; pointer-events: none; }
|
||||
.lotMenu { position: fixed; z-index: 99999; min-width: 17em; white-space: nowrap; background: rgba(20,20,20,.97); color: #fff;
|
||||
border-radius: .5em; box-shadow: 0 .4em 1.6em rgba(0,0,0,.6); padding: .4em 0; font-size: .95em; }
|
||||
.lotMenu .lotHead { padding: .35em 1em .45em; opacity: .7; font-size: .85em; }
|
||||
.lotMenu button { display: flex; justify-content: space-between; width: 100%; padding: .55em 1em; border: 0;
|
||||
background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; }
|
||||
.lotMenu button:hover, .lotMenu button:focus { background: rgba(255,255,255,.12); outline: none; }
|
||||
.lotMenu button.lotOn { color: #ffcc66; }
|
||||
.lotMenu .lotHint { opacity: .55; font-size: .85em; margin-left: 1em; }
|
||||
`;
|
||||
document.head.appendChild(css);
|
||||
|
||||
let state = null;
|
||||
let menu = null;
|
||||
|
||||
function api(method, path, body) {
|
||||
const client = window.ApiClient;
|
||||
if (!client) { return Promise.reject(new Error('ApiClient not ready')); }
|
||||
const opts = { type: method, url: client.getUrl('LightsOffTimer/' + path), dataType: 'json' };
|
||||
if (body) { opts.data = JSON.stringify(body); opts.contentType = 'application/json'; }
|
||||
return client.ajax(opts);
|
||||
}
|
||||
|
||||
function endLabel() {
|
||||
return state && state.itemType === 'Episode' ? 'End of this episode' : (state && state.itemType === 'Movie' ? 'End of this movie' : 'End of this episode/movie');
|
||||
}
|
||||
|
||||
function badgeText() {
|
||||
if (!state || state.mode === 'off') { return ''; }
|
||||
if (state.mode === 'end') { return 'END'; }
|
||||
return state.remainingSeconds != null ? Math.ceil(state.remainingSeconds / 60) + 'm' : '';
|
||||
}
|
||||
|
||||
function render() {
|
||||
document.querySelectorAll('.btnLightsOffTimer').forEach(btn => {
|
||||
const on = state && state.mode !== 'off';
|
||||
btn.classList.toggle('lotActive', !!on);
|
||||
btn.title = on ? (state.mode === 'end' ? 'Sleep timer: ' + endLabel().toLowerCase() : 'Sleep timer: ' + badgeText() + ' left') : 'Sleep timer';
|
||||
let badge = btn.querySelector('.lotBadge');
|
||||
const text = badgeText();
|
||||
if (text && !badge) { badge = document.createElement('span'); badge.className = 'lotBadge'; btn.appendChild(badge); }
|
||||
if (badge) { badge.textContent = text; badge.style.display = text ? '' : 'none'; }
|
||||
});
|
||||
}
|
||||
|
||||
function refresh() {
|
||||
return api('GET', 'State').then(s => { state = s; render(); }).catch(() => { /* no session yet */ });
|
||||
}
|
||||
|
||||
function set(mode, minutes) {
|
||||
closeMenu();
|
||||
return api('POST', 'Set', { mode: mode, minutes: minutes || null }).then(s => { state = s; render(); }).catch(err => {
|
||||
console.warn('[LightsOffTimer] set failed', err);
|
||||
});
|
||||
}
|
||||
|
||||
function closeMenu() {
|
||||
if (menu) { menu.remove(); menu = null; document.removeEventListener('pointerdown', outside, true); }
|
||||
}
|
||||
|
||||
function outside(e) {
|
||||
if (menu && !menu.contains(e.target) && !e.target.closest('.btnLightsOffTimer')) { closeMenu(); }
|
||||
}
|
||||
|
||||
function openMenu(btn) {
|
||||
closeMenu();
|
||||
const cool = state ? state.cooldownMinutes : 15;
|
||||
menu = document.createElement('div');
|
||||
menu.className = 'lotMenu';
|
||||
const head = document.createElement('div');
|
||||
head.className = 'lotHead';
|
||||
head.textContent = 'Stop playback, then lights off ' + cool + ' min later';
|
||||
menu.appendChild(head);
|
||||
|
||||
const items = [{ mode: 'end', label: endLabel(), hint: 'default' }];
|
||||
((state && state.presets) || [15, 30, 45, 60, 90]).forEach(m => items.push({ mode: 'minutes', minutes: m, label: m + ' minutes' }));
|
||||
items.push({ mode: 'off', label: 'Off' });
|
||||
|
||||
items.forEach(it => {
|
||||
const b = document.createElement('button');
|
||||
const active = state && state.mode === it.mode && (it.mode !== 'minutes' || state.minutes === it.minutes);
|
||||
if (active && it.mode !== 'off') { b.classList.add('lotOn'); }
|
||||
b.innerHTML = '<span></span><span class="lotHint"></span>';
|
||||
b.firstChild.textContent = (active && it.mode !== 'off' ? '✓ ' : '') + it.label;
|
||||
b.lastChild.textContent = active && it.mode === 'minutes' && state.remainingSeconds != null
|
||||
? Math.ceil(state.remainingSeconds / 60) + ' min left' : (it.hint || '');
|
||||
b.addEventListener('click', e => { e.stopPropagation(); set(it.mode, it.minutes); });
|
||||
menu.appendChild(b);
|
||||
});
|
||||
|
||||
document.body.appendChild(menu);
|
||||
const r = btn.getBoundingClientRect();
|
||||
const w = menu.offsetWidth, h = menu.offsetHeight;
|
||||
menu.style.left = Math.max(8, Math.min(window.innerWidth - w - 8, r.left + r.width / 2 - w / 2)) + 'px';
|
||||
menu.style.top = Math.max(8, r.top - h - 8) + 'px';
|
||||
document.addEventListener('pointerdown', outside, true);
|
||||
(menu.querySelector('.lotOn') || menu.querySelector('button')).focus();
|
||||
}
|
||||
|
||||
function addButton(osd) {
|
||||
if (osd.querySelector('.btnLightsOffTimer')) { return; }
|
||||
const volume = osd.querySelector('.osdVolumeSliderContainer');
|
||||
const mute = osd.querySelector('.buttonMute');
|
||||
const anchor = volume || mute || osd.querySelector('.btnVideoOsdSettings');
|
||||
if (!anchor) { return; }
|
||||
|
||||
const btn = document.createElement('button');
|
||||
btn.type = 'button';
|
||||
btn.setAttribute('is', 'paper-icon-button-light');
|
||||
btn.className = ((mute && mute.className) || 'autoSize paper-icon-button-light').replace(/\bbuttonMute\b/, '') + ' btnLightsOffTimer';
|
||||
btn.innerHTML = '<span class="lotIcon">' + ICON + '</span>';
|
||||
btn.addEventListener('click', e => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
if (menu) { closeMenu(); return; }
|
||||
refresh().then(() => openMenu(btn));
|
||||
});
|
||||
|
||||
if (volume) { volume.insertAdjacentElement('afterend', btn); } else { anchor.insertAdjacentElement('beforebegin', btn); }
|
||||
refresh();
|
||||
}
|
||||
|
||||
function scan() {
|
||||
document.querySelectorAll('.videoOsdBottom-maincontrols, .videoOsdBottom .buttons').forEach(addButton);
|
||||
}
|
||||
|
||||
new MutationObserver(scan).observe(document.documentElement, { childList: true, subtree: true });
|
||||
scan();
|
||||
|
||||
// Keep the badge current while a player is open.
|
||||
setInterval(() => { if (document.querySelector('.btnLightsOffTimer')) { refresh(); } }, 15000);
|
||||
})();
|
||||
Reference in new issue
Block a user