|
| 1 | +-- Copyright 2025 SmartThings |
| 2 | +-- |
| 3 | +-- Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +-- you may not use this file except in compliance with the License. |
| 5 | +-- You may obtain a copy of the License at |
| 6 | +-- |
| 7 | +-- http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +-- |
| 9 | +-- Unless required by applicable law or agreed to in writing, software |
| 10 | +-- distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +-- See the License for the specific language governing permissions and |
| 13 | +-- limitations under the License. |
| 14 | + |
| 15 | +local stDevice = require "st.device" |
| 16 | +local configurations = require "configurations" |
| 17 | + |
| 18 | + |
| 19 | +local FINGERPRINTS = { |
| 20 | + { mfr = "JNL", model = "Y-K003-001", switches = 3 } |
| 21 | +} |
| 22 | + |
| 23 | +local function can_handle_Yanmi(opts, driver, device, ...) |
| 24 | + for _, fingerprint in ipairs(FINGERPRINTS) do |
| 25 | + if device:get_manufacturer() == fingerprint.mfr and device:get_model() == fingerprint.model then |
| 26 | + local subdriver = require("Yanmi") |
| 27 | + return true, subdriver |
| 28 | + end |
| 29 | + end |
| 30 | + return false |
| 31 | +end |
| 32 | + |
| 33 | +local function get_children_info(device) |
| 34 | + for _, fingerprint in ipairs(FINGERPRINTS) do |
| 35 | + if device:get_model() == fingerprint.model then |
| 36 | + return fingerprint.switches |
| 37 | + end |
| 38 | + end |
| 39 | +end |
| 40 | + |
| 41 | +local function find_child(parent, ep_id) |
| 42 | + return parent:get_child_by_parent_assigned_key(string.format("%02X", ep_id)) |
| 43 | +end |
| 44 | + |
| 45 | +local function create_child_devices(driver, device) |
| 46 | + local switch_amount = get_children_info(device) |
| 47 | + local base_name = string.sub(device.label, 0, -2) |
| 48 | + -- Create Switch 2-3 |
| 49 | + for i = 2, switch_amount, 1 do |
| 50 | + if find_child(device, i) == nil then |
| 51 | + local metadata = { |
| 52 | + type = "EDGE_CHILD", |
| 53 | + parent_assigned_child_key = string.format("%02X", i), |
| 54 | + label = base_name .. i, |
| 55 | + profile = "basic-switch", |
| 56 | + parent_device_id = device.id, |
| 57 | + vendor_provided_label = base_name .. i, |
| 58 | + } |
| 59 | + driver:try_create_device(metadata) |
| 60 | + end |
| 61 | + end |
| 62 | + device:refresh() |
| 63 | +end |
| 64 | + |
| 65 | +local function device_added(driver, device) |
| 66 | + if device.network_type ~= stDevice.NETWORK_TYPE_CHILD then |
| 67 | + create_child_devices(driver, device) |
| 68 | + end |
| 69 | +end |
| 70 | + |
| 71 | +local function device_init(driver, device, event) |
| 72 | + device:set_find_child(find_child) |
| 73 | +end |
| 74 | + |
| 75 | +local Yanmi_switch = { |
| 76 | + NAME = "Zigbee Yanmi Switch", |
| 77 | + lifecycle_handlers = { |
| 78 | + added = device_added, |
| 79 | + init = configurations.power_reconfig_wrapper(device_init) |
| 80 | + }, |
| 81 | + can_handle = can_handle_Yanmi |
| 82 | +} |
| 83 | + |
| 84 | +return Yanmi_switch |
0 commit comments