From 9942b52bd4029a52078094144188d00d0baba808 Mon Sep 17 00:00:00 2001 From: seojune Date: Thu, 18 Apr 2024 14:05:29 +0900 Subject: [PATCH] 1. add new error status event 2. fix abnormal feeding logs in history tab and life cycle issues --- drivers/Aqara/aqara-feeder/src/init.lua | 32 ++++++++++++------- .../src/test/test_aqara_pet_feeder.lua | 4 +-- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/drivers/Aqara/aqara-feeder/src/init.lua b/drivers/Aqara/aqara-feeder/src/init.lua index a3af166fc9..fba75a42c2 100644 --- a/drivers/Aqara/aqara-feeder/src/init.lua +++ b/drivers/Aqara/aqara-feeder/src/init.lua @@ -17,7 +17,12 @@ local PRIVATE_CLUSTER_ID = 0xFCC0 local PRIVATE_ATTR_ID = 0xFFF1 local MFG_CODE = 0x115F -local callback_timer = function(driver, device, cmd) +local function reload_portion(device) + local lastPortion = device:get_latest_state("main", capabilities.feederPortion.ID, + capabilities.feederPortion.feedPortion.NAME) or 0 + device:emit_event(capabilities.feederPortion.feedPortion({ value = lastPortion, unit = "servings" })) +end +local callback_feed = function(device) return function() device:emit_event(capabilities.feederOperatingState.feederOperatingState("idle")) end @@ -64,10 +69,7 @@ end -- [[ capability_handlers ]] local function do_refresh(driver, device) -- refresh - local lastPortion = device:get_latest_state("main", capabilities.feederPortion.ID, - capabilities.feederPortion.feedPortion.NAME) or 0 - device:emit_event(capabilities.feederPortion.feedPortion({ value = lastPortion, unit = "servings" }, - { state_change = true })) + reload_portion(device) do_payload(device, 8, 0, 2001, OP_REPORT, 1, 0) device:emit_event(capabilities.feederOperatingState.feederOperatingState("idle")) end @@ -104,23 +106,25 @@ local function petFeeder_handler(driver, device, value, zb_rx) elseif funcID == "14.92.85" then -- feed portion device:emit_event(capabilities.feederPortion.feedPortion({ value = conv_data(param), unit = "servings" })) - elseif funcID == "13.104.85" then - local feed_source = device:get_field(FEED_SOURCE) + elseif funcID == "13.104.85" and conv_data(param) ~= 0 then + local feed_source = device:get_field(FEED_SOURCE) or 0 if feed_source == 0 then device:emit_event(capabilities.feederOperatingState.feederOperatingState("feeding")) end device:set_field(FEED_SOURCE, 0, { persist = true }) delete_timer(device) - device:set_field(FEED_TIMER, device.thread:call_with_delay(FEED_TIME, callback_timer(driver, device))) + device:set_field(FEED_TIMER, device.thread:call_with_delay(FEED_TIME, callback_feed(device))) + elseif funcID == "13.11.85" then + -- error + delete_timer(device) + local evt = "idle" + if conv_data(param) == 1 then evt = "error" end + device:emit_event(capabilities.feederOperatingState.feederOperatingState(evt)) end end -- [[ lifecycle_handlers ]] local function device_added(driver, device) - -- private protocol enable - device:send(cluster_base.write_manufacturer_specific_attribute(device, - PRIVATE_CLUSTER_ID, 0x0009, MFG_CODE, data_types.Uint8, 1)) - -- init do_payload(device, 4, 24, 85, OP_WRITE, 1, 0) device:emit_event(capabilities.feederOperatingState.feederOperatingState("idle")) device:emit_event(capabilities.feederPortion.feedPortion({ value = 1, unit = "servings" })) @@ -143,6 +147,10 @@ local function device_info_changed(driver, device, event, args) end local function device_configure(driver, device) + -- private protocol enable + device:send(cluster_base.write_manufacturer_specific_attribute(device, + PRIVATE_CLUSTER_ID, 0x0009, MFG_CODE, data_types.Uint8, 1)) + do_payload(device, 4, 24, 85, OP_WRITE, 1, 0) end -- [[ Registration ]] diff --git a/drivers/Aqara/aqara-feeder/src/test/test_aqara_pet_feeder.lua b/drivers/Aqara/aqara-feeder/src/test/test_aqara_pet_feeder.lua index 9b896584ab..93ac2392c1 100644 --- a/drivers/Aqara/aqara-feeder/src/test/test_aqara_pet_feeder.lua +++ b/drivers/Aqara/aqara-feeder/src/test/test_aqara_pet_feeder.lua @@ -51,8 +51,6 @@ test.register_coroutine_test( test.socket.zigbee:__set_channel_ordering("relaxed") test.socket.capability:__set_channel_ordering("relaxed") test.socket.device_lifecycle:__queue_receive({ mock_device.id, "added" }) - test.socket.zigbee:__expect_send({ mock_device.id, - cluster_base.write_manufacturer_specific_attribute(mock_device, PRIVATE_CLUSTER_ID, 0x0009, MFG_CODE, data_types.Uint8, 1) }) test.socket.zigbee:__expect_send({ mock_device.id, cluster_base.write_manufacturer_specific_attribute(mock_device, PRIVATE_CLUSTER_ID, PRIVATE_ATTRIBUTE_ID, MFG_CODE, data_types.OctetString, "\x00\x02\x01\x04\x18\x00\x55\x01\x00") }) test.socket.capability:__expect_send(mock_device:generate_test_message("main", @@ -69,7 +67,7 @@ test.register_coroutine_test( function() test.socket.capability:__queue_receive({ mock_device.id, { capability = "refresh", component = "main", command = "refresh", args = {} } }) test.socket.capability:__expect_send(mock_device:generate_test_message("main", - capabilities.feederPortion.feedPortion({value=0, unit="servings"}, {state_change=true}))) + capabilities.feederPortion.feedPortion({value=0, unit="servings"}))) test.socket.zigbee:__expect_send({ mock_device.id, cluster_base.write_manufacturer_specific_attribute(mock_device, PRIVATE_CLUSTER_ID, PRIVATE_ATTRIBUTE_ID, MFG_CODE, data_types.OctetString, "\x00\x05\x01\x08\x00\x07\xD1\x01\x00") }) test.socket.capability:__expect_send(mock_device:generate_test_message("main",