Skip to content

Commit 033ded5

Browse files
Add Zigbee fan
Add Zigbee fan
1 parent c8038a4 commit 033ded5

File tree

8 files changed

+341
-0
lines changed

8 files changed

+341
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# EDGE-Driver---Fan-Light
2+
# update code complete
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: 'ITM Fan Light 231201'
2+
packageKey: 'itm-fan-light'
3+
description: 'EDGE Driver, ITM Fan Light'
4+
vendorSupportInformation:
5+
permissions:
6+
zigbee: {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
zigbeeManufacturer:
2+
# FANS
3+
- id: "Samsung/SAMSUNG-ITM-Z-003"
4+
deviceLabel: ITM Fan Light
5+
manufacturer: Samsung Electronics
6+
model: SAMSUNG-ITM-Z-003
7+
deviceProfileName: itm-fan-light
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: itm-fan-light
2+
components:
3+
- id: main
4+
label: Fan
5+
capabilities:
6+
- id: switch
7+
version: 1
8+
- id: fanSpeed
9+
version: 1
10+
- id: firmwareUpdate
11+
version: 1
12+
- id: refresh
13+
version: 1
14+
categories:
15+
- name: Fan
16+
- id: light
17+
label: Light
18+
capabilities:
19+
- id: switch
20+
version: 1
21+
- id: switchLevel
22+
version: 1
23+
- id: refresh
24+
version: 1
25+
categories:
26+
- name: Light
27+
preferences:
28+
- title: "Child Device"
29+
name: childLight2
30+
description: "Create Child Device : Light"
31+
required: true
32+
preferenceType: boolean
33+
definition:
34+
default: false
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: switch-level
2+
components:
3+
- id: main
4+
capabilities:
5+
- id: switch
6+
version: 1
7+
- id: switchLevel
8+
version: 1
9+
- id: refresh
10+
version: 1
11+
categories:
12+
- name: Light
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- Copyright 2022 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 clusters = require "st.zigbee.zcl.clusters"
16+
17+
local OnOff = clusters.OnOff
18+
local Level = clusters.Level
19+
local FanControl = clusters.FanControl
20+
21+
local devices = {
22+
ITM_FAN_LIGHT = {
23+
FINGERPRINTS = {
24+
{ mfr = "Samsung Electronics", model = "SAMSUNG-ITM-Z-003" },
25+
},
26+
CONFIGURATION = {
27+
{
28+
cluster = OnOff.ID,
29+
attribute = OnOff.attributes.OnOff.ID,
30+
minimum_interval = 0,
31+
maximum_interval = 600,
32+
data_type = OnOff.attributes.OnOff.base_type
33+
},
34+
{
35+
cluster = Level.ID,
36+
attribute = Level.attributes.CurrentLevel.ID,
37+
minimum_interval = 1,
38+
maximum_interval = 600,
39+
data_type = Level.attributes.CurrentLevel.base_type,
40+
reportable_change = 1
41+
},
42+
{
43+
cluster = FanControl.ID,
44+
attribute = FanControl.attributes.FanMode.ID,
45+
minimum_interval = 1,
46+
maximum_interval = 600,
47+
data_type = FanControl.attributes.FanMode.base_type
48+
}
49+
}
50+
},
51+
}
52+
53+
local configurations = {}
54+
55+
configurations.get_device_configuration = function(zigbee_device)
56+
for _, device in pairs(devices) do
57+
for _, fingerprint in pairs(device.FINGERPRINTS) do
58+
if zigbee_device:get_manufacturer() == fingerprint.mfr and zigbee_device:get_model() == fingerprint.model then
59+
return device.CONFIGURATION
60+
end
61+
end
62+
end
63+
return nil
64+
end
65+
66+
return configurations
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
-- Copyright 2022 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 capabilities = require "st.capabilities"
16+
local ZigbeeDriver = require "st.zigbee"
17+
local defaults = require "st.zigbee.defaults"
18+
local configurationMap = require "configurations"
19+
20+
local device_init = function(self, device)
21+
local configuration = configurationMap.get_device_configuration(device)
22+
if configuration ~= nil then
23+
for _, attribute in ipairs(configuration) do
24+
device:add_configured_attribute(attribute)
25+
device:add_monitored_attribute(attribute)
26+
end
27+
end
28+
end
29+
30+
local zigbee_switch_driver_template = {
31+
supported_capabilities = {
32+
capabilities.switch,
33+
capabilities.switchLevel,
34+
capabilities.colorControl,
35+
capabilities.colorTemperature,
36+
capabilities.fanspeed
37+
},
38+
sub_drivers = {
39+
require("itm-fan-light")
40+
},
41+
lifecycle_handlers = {
42+
init = device_init
43+
}
44+
}
45+
46+
zigbee_switch:run()
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
-- Copyright 2023 philh30
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 st_device = require "st.device"
16+
local clusters = require "st.zigbee.zcl.clusters"
17+
local capabilities = require "st.capabilities"
18+
local FanControl = clusters.FanControl
19+
local Level = clusters.Level
20+
local OnOff = clusters.OnOff
21+
22+
local IFL_FINGERPRINTS = {
23+
{ mfr = "Samsung Electronics", model = "SAMSUNG-ITM-Z-003" },
24+
}
25+
26+
local is_ifl = function(opts, driver, device)
27+
for _, fingerprint in ipairs(IFL_FINGERPRINTS) do
28+
if device:get_manufacturer() == fingerprint.mfr and device:get_model() == fingerprint.model then
29+
return true
30+
end
31+
end
32+
return false
33+
end
34+
35+
-- Create child device
36+
37+
local function add_child(driver,parent,profile,child_type)
38+
local child_metadata = {
39+
type = "EDGE_CHILD",
40+
label = string.format("%s %s", parent.label, child_type),
41+
profile = profile,
42+
parent_device_id = parent.id,
43+
parent_assigned_child_key = child_type,
44+
vendor_provided_label = string.format("%s %s", parent.label, child_type)
45+
}
46+
driver:try_create_device(child_metadata)
47+
end
48+
49+
local function info_changed(driver, device, event, args)
50+
if (device.preferences or {}).childLight2 then
51+
if not device:get_child_by_parent_assigned_key('light') then
52+
add_child(driver,device,'switch-level','light')
53+
end
54+
end
55+
end
56+
57+
-- CAPABILITY HANDLERS
58+
59+
local function on_handler(driver, device, command)
60+
local dev = device
61+
if device.network_type == st_device.NETWORK_TYPE_CHILD then
62+
command.component = 'light'
63+
dev = device:get_parent_device()
64+
end
65+
if command.component == 'light' then
66+
dev:send(OnOff.server.commands.On(dev))
67+
else
68+
local speed = dev:get_field('LAST_FAN_SPD') or 1
69+
dev:send(FanControl.attributes.FanMode:write(dev,speed))
70+
dev:send(FanControl.attributes.FanMode:read(dev,speed))
71+
end
72+
end
73+
74+
local function off_handler(driver, device, command)
75+
local dev = device
76+
if device.network_type == st_device.NETWORK_TYPE_CHILD then
77+
command.component = 'light'
78+
dev = device:get_parent_device()
79+
end
80+
if command.component == 'light' then
81+
dev:send(OnOff.server.commands.Off(dev))
82+
else
83+
dev:send(FanControl.attributes.FanMode:write(dev,FanControl.attributes.FanMode.OFF))
84+
end
85+
dev:send(FanControl.attributes.FanMode:read(dev,FanControl.attributes.FanMode.OFF))
86+
end
87+
88+
local function switch_level_handler(driver, device, command)
89+
local dev = device
90+
if device.network_type == st_device.NETWORK_TYPE_CHILD then
91+
command.component = 'light'
92+
dev = device:get_parent_device()
93+
end
94+
if command.component == 'light' then
95+
local level = math.floor(command.args.level/100.0 * 254)
96+
dev:send(Level.server.commands.MoveToLevelWithOnOff(dev, level, command.args.rate or 0xFFFF))
97+
end
98+
end
99+
100+
local function fan_speed_handler(driver, device, command)
101+
device:send(FanControl.attributes.FanMode:write(device,command.args.speed))
102+
device:send(FanControl.attributes.FanMode:read(device,command.args.speed))
103+
end
104+
105+
-- ZIGBEE HANDLERS
106+
107+
local function zb_fan_control_handler(driver, device, value, zb_rx)
108+
device:emit_event(capabilities.fanSpeed.fanSpeed(value.value))
109+
local evt = capabilities.switch.switch(value.value > 0 and 'on' or 'off', { visibility = { displayed = false } })
110+
device:emit_component_event(device.profile.components.main,evt)
111+
if value.value > 0 then
112+
device:set_field('LAST_FAN_SPD', value.value, {persist = true})
113+
end
114+
end
115+
116+
local function zb_level_handler(driver, device, value, zb_rx)
117+
local evt = capabilities.switchLevel.level(math.floor((value.value / 254.0 * 100) + 0.5))
118+
device:emit_component_event(device.profile.components.light,evt)
119+
local child = device:get_child_by_parent_assigned_key('light')
120+
if child ~= nil then
121+
child:emit_event(evt)
122+
end
123+
end
124+
125+
local function zb_onoff_handler(driver, device, value, zb_rx)
126+
local attr = capabilities.switch.switch
127+
local evt = value.value and attr.on() or attr.off()
128+
device:emit_component_event(device.profile.components.light,evt)
129+
local child = device:get_child_by_parent_assigned_key('light')
130+
if child ~= nil then
131+
child:emit_event(evt)
132+
end
133+
end
134+
135+
local itm_fan_light = {
136+
NAME = "ITM Fan Light",
137+
zigbee_handlers = {
138+
attr = {
139+
[FanControl.ID] = {
140+
[FanControl.attributes.FanMode.ID] = zb_fan_control_handler
141+
},
142+
[Level.ID] = {
143+
[Level.attributes.CurrentLevel.ID] = zb_level_handler
144+
},
145+
[OnOff.ID] = {
146+
[OnOff.attributes.OnOff.ID] = zb_onoff_handler
147+
}
148+
}
149+
},
150+
capability_handlers = {
151+
[capabilities.switch.ID] = {
152+
[capabilities.switch.commands.on.NAME] = on_handler,
153+
[capabilities.switch.commands.off.NAME] = off_handler,
154+
},
155+
[capabilities.switchLevel.ID] = {
156+
[capabilities.switchLevel.commands.setLevel.NAME] = switch_level_handler
157+
},
158+
[capabilities.fanSpeed.ID] = {
159+
[capabilities.fanSpeed.commands.setFanSpeed.NAME] = fan_speed_handler
160+
}
161+
},
162+
lifecycle_handlers = {
163+
infoChanged = info_changed,
164+
},
165+
can_handle = is_ifl
166+
}
167+
168+
return itm_fan_light

0 commit comments

Comments
 (0)