diff --git a/README.md b/README.md index 9c193a5..7ff5c83 100644 --- a/README.md +++ b/README.md @@ -28,19 +28,20 @@ docker run \ ### Disabling sonos device discovery entirely ``` docker run \ - -e BONOB_SONOS_SEED_HOST=disabled \ + -e BONOB_SONOS_DEVICE_DISCOVERY=false \ -p 4534 \ simojenki/bonob ``` ## Configuration -item | default value | description +item | default value | description ---- | ------------- | ----------- BONOB_PORT | 4534 | Default http port for bonob to listen on BONOB_WEB_ADDRESS | http://localhost:4534 | Web address for bonob BONOB_SECRET | bonob | secret used for encrypting credentials -BONOB_SONOS_SEED_HOST | undefined | sonos device seed host for auto-discovery, or 'disabled' to turn off device discovery entirely +BONOB_SONOS_DEVICE_DISCOVERY | true | whether or not sonos device discovery should be enabled +BONOB_SONOS_SEED_HOST | undefined | sonos device seed host for discovery, or ommitted for for auto-discovery BONOB_SONOS_SERVICE_NAME | bonob | service name for sonos BONOB_SONOS_SERVICE_ID | 246 | service id for sonos BONOB_NAVIDROME_URL | http://localhost:4533 | URL for navidrome diff --git a/src/app.ts b/src/app.ts index e103e9d..210d85f 100644 --- a/src/app.ts +++ b/src/app.ts @@ -8,6 +8,10 @@ const PORT = +(process.env["BONOB_PORT"] || 4534); const WEB_ADDRESS = process.env["BONOB_WEB_ADDRESS"] || `http://localhost:${PORT}`; +const SONOS_DEVICE_DISCOVERY = + (process.env["BONOB_SONOS_DEVICE_DISCOVERY"] || "true") == "true"; +const SONOS_SEED_HOST = process.env["BONOB_SONOS_SEED_HOST"]; + const bonob = bonobService( process.env["BONOB_SONOS_SERVICE_NAME"] || "bonob", Number(process.env["BONOS_SONOS_SERVICE_ID"] || "246"), @@ -15,10 +19,13 @@ const bonob = bonobService( "AppLink" ); const app = server( - sonos(process.env["BONOB_SONOS_SEED_HOST"]), + sonos(SONOS_DEVICE_DISCOVERY, SONOS_SEED_HOST), bonob, WEB_ADDRESS, - new Navidrome(process.env["BONOB_NAVIDROME_URL"] || "http://localhost:4533", encryption(process.env["BONOB_SECRET"] || "bonob")) + new Navidrome( + process.env["BONOB_NAVIDROME_URL"] || "http://localhost:4533", + encryption(process.env["BONOB_SECRET"] || "bonob") + ) ); app.listen(PORT, () => { diff --git a/src/sonos.ts b/src/sonos.ts index 6a8594b..da7ecc5 100644 --- a/src/sonos.ts +++ b/src/sonos.ts @@ -4,8 +4,8 @@ import { parse } from "node-html-parser"; import { MusicService } from "@svrooij/sonos/lib/services"; import { head } from "underscore"; import logger from "./logger"; -import STRINGS from './strings'; -import { SOAP_PATH, STRINGS_ROUTE, PRESENTATION_MAP_ROUTE } from './smapi'; +import STRINGS from "./strings"; +import { SOAP_PATH, STRINGS_ROUTE, PRESENTATION_MAP_ROUTE } from "./smapi"; export type Device = { name: string; @@ -22,7 +22,7 @@ export type Service = { strings: { uri?: string; version?: string }; presentation: { uri?: string; version?: string }; pollInterval?: number; - authType: 'Anonymous' | 'AppLink' | 'DeviceLink' | 'UserId'; + authType: "Anonymous" | "AppLink" | "DeviceLink" | "UserId"; }; const stripTailingSlash = (url: string) => @@ -32,7 +32,7 @@ export const bonobService = ( name: string, sid: number, bonobRoot: string, - authType: 'Anonymous' | 'AppLink' | 'DeviceLink' | 'UserId' = 'AppLink' + authType: "Anonymous" | "AppLink" | "DeviceLink" | "UserId" = "AppLink" ): Service => ({ name, sid, @@ -150,9 +150,9 @@ export function autoDiscoverySonos(sonosSeedHost?: string): Sonos { const anyDevice = await sonosDevices().then((devices) => head(devices)); if (!anyDevice) { - logger.warn("Failed to find a device to register with...") - return false - }; + logger.warn("Failed to find a device to register with..."); + return false; + } const customd = `http://${anyDevice.Host}:${anyDevice.Port}/customsd`; @@ -164,9 +164,11 @@ export function autoDiscoverySonos(sonosSeedHost?: string): Sonos { ); if (!csrfToken) { - logger.warn(`Failed to find csrfToken at GET -> ${customd}, cannot register service`) - return false - }; + logger.warn( + `Failed to find csrfToken at GET -> ${customd}, cannot register service` + ); + return false; + } return axios .post(customd, new URLSearchParams(asCustomdForm(csrfToken, service)), { @@ -179,14 +181,10 @@ export function autoDiscoverySonos(sonosSeedHost?: string): Sonos { }; } -export default function sonos( +const sonos = ( + discoveryEnabled: boolean = true, sonosSeedHost: string | undefined = undefined -): Sonos { - switch (sonosSeedHost) { - case "disabled": - logger.info("Sonos device discovery disabled"); - return SONOS_DISABLED; - default: - return autoDiscoverySonos(sonosSeedHost); - } -} +): Sonos => + discoveryEnabled ? autoDiscoverySonos(sonosSeedHost) : SONOS_DISABLED; + +export default sonos; diff --git a/tests/sonos.test.ts b/tests/sonos.test.ts index c5bed86..98879ed 100644 --- a/tests/sonos.test.ts +++ b/tests/sonos.test.ts @@ -235,7 +235,7 @@ describe("sonos", () => { describe("when is disabled", () => { it("should return a disabled client", async () => { - const disabled = sonos("disabled"); + const disabled = sonos(false); expect(disabled).toEqual(SONOS_DISABLED); expect(await disabled.devices()).toEqual([]); @@ -271,7 +271,7 @@ describe("sonos", () => { ); sonosManager.InitializeWithDiscovery.mockResolvedValue(true); - const actualDevices = await sonos(undefined).devices(); + const actualDevices = await sonos(true, undefined).devices(); expect(SonosManager).toHaveBeenCalledTimes(1); expect(sonosManager.InitializeWithDiscovery).toHaveBeenCalledWith(10); @@ -292,7 +292,7 @@ describe("sonos", () => { ); sonosManager.InitializeWithDiscovery.mockResolvedValue(true); - const actualDevices = await sonos("").devices(); + const actualDevices = await sonos(true, "").devices(); expect(SonosManager).toHaveBeenCalledTimes(1); expect(sonosManager.InitializeWithDiscovery).toHaveBeenCalledWith(10); @@ -315,7 +315,7 @@ describe("sonos", () => { ); sonosManager.InitializeFromDevice.mockResolvedValue(true); - const actualDevices = await sonos(seedHost).devices(); + const actualDevices = await sonos(true, seedHost).devices(); expect(SonosManager).toHaveBeenCalledTimes(1); expect(sonosManager.InitializeFromDevice).toHaveBeenCalledWith( @@ -338,7 +338,7 @@ describe("sonos", () => { ); sonosManager.InitializeWithDiscovery.mockResolvedValue(true); - const actualDevices = await sonos(undefined).devices(); + const actualDevices = await sonos(true, undefined).devices(); expect(actualDevices).toEqual([ { @@ -369,7 +369,7 @@ describe("sonos", () => { ); sonosManager.InitializeWithDiscovery.mockResolvedValue(false); - expect(await sonos("").devices()).toEqual([]); + expect(await sonos(true, "").devices()).toEqual([]); }); }); });