UNPKG

matterbridge

Version:
135 lines 8.37 kB
/** * @description This file contains the Refrigerator class. * @file src/devices/refrigerator.ts * @author Luca Liguori * @created 2025-05-25 * @version 1.1.0 * @license Apache-2.0 * * Copyright 2025, 2026, 2027 Luca Liguori. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { ModeBase } from '@matter/main/clusters/mode-base'; import { RefrigeratorAndTemperatureControlledCabinetModeServer } from '@matter/main/behaviors/refrigerator-and-temperature-controlled-cabinet-mode'; import { RefrigeratorAndTemperatureControlledCabinetMode } from '@matter/main/clusters/refrigerator-and-temperature-controlled-cabinet-mode'; // Matterbridge import { oven, powerSource, temperatureControlledCabinetCooler } from '../matterbridgeDeviceTypes.js'; import { MatterbridgeEndpoint } from '../matterbridgeEndpoint.js'; import { MatterbridgeServer } from '../matterbridgeBehaviors.js'; import { createLevelTemperatureControlClusterServer } from './temperatureControl.js'; export class Refrigerator extends MatterbridgeEndpoint { /** * Creates an instance of the Refrigerator class. * * @param {string} name - The name of the refrigerator. * @param {string} serial - The serial number of the refrigerator. * * @remarks * 13.2 A refrigerator represents a device that contains one or more cabinets that are capable of chilling or * freezing food. Examples of consumer products that MAY make use of this device type include refrigerators, * freezers, and wine coolers. * A refrigerator is always defined via endpoint composition. * - Use `addCabinet` to add one or more cabinets to the refrigerator. */ constructor(name, serial) { super([oven, powerSource], { uniqueStorageKey: `${name.replaceAll(' ', '')}-${serial.replaceAll(' ', '')}` }, true); this.createDefaultIdentifyClusterServer(); this.createDefaultBasicInformationClusterServer(name, serial, 0xfff1, 'Matterbridge', 0x8000, 'Refrigerator'); this.createDefaultPowerSourceWiredClusterServer(); this.addFixedLabel('composed', 'Refrigerator'); } /** * Adds a Level Temperature Controlled Cabinet Cooler to the refrigerator. * * @param {string} name - The name of the cabinet. * @param {Semtag[]} tagList - The tagList associated with the cabinet. * @param {number} currentMode - The current mode of the cabinet. Defaults to 1 (which corresponds to 'Auto'). * @param {RefrigeratorAndTemperatureControlledCabinetMode.ModeOption[]} supportedModes - The supported modes for the cabinet. Defaults to 'Auto', 'RapidCool', and 'RapidFreeze'. * @param {number} selectedTemperatureLevel - The selected temperature level as an index of the supportedTemperatureLevels array. Defaults to 2 (which corresponds to 'Level 3'). * @param {string[]} supportedTemperatureLevels - The list of supported temperature levels for the cabinet. Defaults to ['Level 1', 'Level 2', 'Level 3', 'Level 4', 'Level 5']. * @param {number} currentTemperature - The current temperature of the cabinet in degrees Celsius. Defaults to 1000 (which corresponds to 10.00 degrees Celsius). * * @returns {MatterbridgeEndpoint} The MatterbridgeEndpoint instance representing the cabinet. * * @remarks * 13.4.1 A Temperature Controlled Cabinet Cooler is a device that provides a cooled space for chilling food. * It is typically installed within a refrigerator. * * Example usage with specific namespace tags: * ``` * refrigerator.addCabinet('Refrigerator Top', [ * { mfgCode: null, namespaceId: PositionTag.Top.namespaceId, tag: PositionTag.Top.tag, label: 'Refrigerator Top' }, * { mfgCode: null, namespaceId: RefrigeratorTag.Refrigerator.namespaceId, tag: RefrigeratorTag.Refrigerator.tag, label: RefrigeratorTag.Refrigerator.label }, * ]); * refrigerator.addCabinet('Freezer Bottom', [ * { mfgCode: null, namespaceId: PositionTag.Bottom.namespaceId, tag: PositionTag.Bottom.tag, label: 'Freezer Bottom' }, * { mfgCode: null, namespaceId: RefrigeratorTag.Freezer.namespaceId, tag: RefrigeratorTag.Freezer.tag, label: RefrigeratorTag.Freezer.label }, * ]); * ``` */ addCabinet(name, tagList, currentMode = 1, supportedModes = [ { label: 'Auto', mode: 1, modeTags: [{ value: RefrigeratorAndTemperatureControlledCabinetMode.ModeTag.Auto }] }, { label: 'RapidCool', mode: 2, modeTags: [{ value: RefrigeratorAndTemperatureControlledCabinetMode.ModeTag.RapidCool }] }, { label: 'RapidFreeze', mode: 3, modeTags: [{ value: RefrigeratorAndTemperatureControlledCabinetMode.ModeTag.RapidFreeze }] }, ], selectedTemperatureLevel = 2, supportedTemperatureLevels = ['Level 1', 'Level 2', 'Level 3', 'Level 4', 'Level 5'], currentTemperature = 1000) { const cabinet = this.addChildDeviceType(name, temperatureControlledCabinetCooler, { tagList }, true); cabinet.log.logName = name; cabinet.createDefaultIdentifyClusterServer(); createLevelTemperatureControlClusterServer(cabinet, selectedTemperatureLevel, supportedTemperatureLevels); this.createDefaultRefrigeratorAndTemperatureControlledCabinetModeClusterServer(cabinet, currentMode, supportedModes); cabinet.createDefaultTemperatureMeasurementClusterServer(currentTemperature); return cabinet; } /** * Creates a default RefrigeratorAndTemperatureControlledCabinetMode Cluster Server. * * @param {MatterbridgeEndpoint} endpoint - The Matterbridge endpoint instance. * @param {number} currentMode - The current mode of the oven. * @param {RefrigeratorAndTemperatureControlledCabinetMode.ModeOption[]} supportedModes - The supported modes for the refrigerator and temperature controlled cabinet. * * @returns {MatterbridgeEndpoint} The current MatterbridgeEndpoint instance for chaining. * * @remarks * - supportedModes is a fixed attribute. It cannot be changed at runtime. * - currentMode persists across reboots. */ createDefaultRefrigeratorAndTemperatureControlledCabinetModeClusterServer(endpoint, currentMode, supportedModes) { endpoint.behaviors.require(MatterbridgeRefrigeratorAndTemperatureControlledCabinetModeServer, { supportedModes, currentMode, }); return endpoint; } } // Server for RefrigeratorAndTemperatureControlledCabinetMode export class MatterbridgeRefrigeratorAndTemperatureControlledCabinetModeServer extends RefrigeratorAndTemperatureControlledCabinetModeServer { initialize() { const device = this.endpoint.stateOf(MatterbridgeServer); device.log.info('MatterbridgeRefrigeratorAndTemperatureControlledCabinetModeServer initialized'); } changeToMode(request) { const device = this.endpoint.stateOf(MatterbridgeServer); const supportedMode = this.state.supportedModes.find((supportedMode) => supportedMode.mode === request.newMode); if (supportedMode) { device.log.info(`MatterbridgeRefrigeratorAndTemperatureControlledCabinetModeServer: changeToMode (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber}) called with mode ${supportedMode.mode} = ${supportedMode.label}`); this.state.currentMode = request.newMode; return { status: ModeBase.ModeChangeStatus.Success, statusText: 'Success' }; } else { device.log.error(`MatterbridgeRefrigeratorAndTemperatureControlledCabinetModeServer: changeToMode (endpoint ${this.endpoint.maybeId}.${this.endpoint.maybeNumber}) called with invalid mode ${request.newMode}`); return { status: ModeBase.ModeChangeStatus.InvalidInMode, statusText: 'Invalid mode' }; } } } //# sourceMappingURL=refrigerator.js.map