-- Copyright 2022 SmartThings
--
-- 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.

local capabilities = require "st.capabilities"
--- @type st.zwave.CommandClass
local cc = require "st.zwave.CommandClass"
--- @type st.zwave.CommandClass.Association
local Association = (require "st.zwave.CommandClass.Association")({version=2})
--- @type st.zwave.CommandClass.WakeUp
local WakeUp = (require "st.zwave.CommandClass.WakeUp")({version=1})

--local ENERWAVE_MFR = 0x011A

local function can_handle_enerwave_motion_sensor(opts, driver, device, cmd, ...)
  if device.network_type == "DEVICE_EDGE_CHILD" then return false end --is child device
  if device.zwave_manufacturer_id == 0x011A then
    local subdriver = require("enerwave-motion-sensor")
    --return device.zwave_manufacturer_id == ENERWAVE_MFR
    return true, subdriver
  end
  return false
end

local function wakeup_notification(driver, device, cmd)
  local current_motion_status = device:get_latest_state("main", capabilities.motionSensor.ID, capabilities.motionSensor.motion.NAME)
  if current_motion_status == nil then
    device:send(Association:Set({grouping_identifier = 1, node_ids = {driver.environment_info.hub_zwave_id}}))
  end
  device:refresh()
end

local function do_configure(driver, device)
  device:refresh()
  device:send(Association:Set({grouping_identifier = 1, node_ids = {driver.environment_info.hub_zwave_id}}))
end

local enerwave_motion_sensor = {
  zwave_handlers = {
    [cc.WAKE_UP] = {
      [WakeUp.NOTIFICATION] = wakeup_notification
    }
  },
  lifecycle_handlers = {
    doConfigure = do_configure
  },
  NAME = "enerwave_motion_sensor",
  can_handle = can_handle_enerwave_motion_sensor
}

return enerwave_motion_sensor
