Skip to content

Commit

Permalink
Update PR based on comments
Browse files Browse the repository at this point in the history
  • Loading branch information
hcarter-775 committed Sep 27, 2024
1 parent 03d038e commit 93a326f
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 66 deletions.
4 changes: 2 additions & 2 deletions drivers/SmartThings/matter-appliance/src/matter-oven/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ local function temperature_setpoint_attr_handler(driver, device, ib, response)
maximum = max,
}

-- Only emit the capability for RPC version >= 5 (unit conversion for
-- range capabilities is only supported for RPC >= 5)
-- Only emit the capability for RPC version >= 5, since unit conversion for
-- range capabilities is only supported in that case.
if version.rpc >= 5 then
device:emit_event_for_endpoint(ib.endpoint_id,
capabilities.temperatureSetpoint.temperatureSetpointRange({value = range, unit = unit},{visibility = {displayed = false}}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,4 @@ test.register_message_test(
}
)

test.run_registered_tests()
test.run_registered_tests()
58 changes: 25 additions & 33 deletions drivers/SmartThings/matter-evse/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ local function tbl_contains(array, value)
end

-- MAPS --
local EVSE_STATE_ENUM_TO_CAPABILITY_MAP = {
local EVSE_STATE_ENUM_MAP = {
-- Since PLUGGED_IN_DISCHARGING is not to be supported, it is not checked.
[clusters.EnergyEvse.types.StateEnum.NOT_PLUGGED_IN] = capabilities.evseState.state.notPluggedIn,
[clusters.EnergyEvse.types.StateEnum.PLUGGED_IN_NO_DEMAND] = capabilities.evseState.state.pluggedInNoDemand,
Expand All @@ -126,15 +126,15 @@ local EVSE_STATE_ENUM_TO_CAPABILITY_MAP = {
[clusters.EnergyEvse.types.StateEnum.FAULT] = capabilities.evseState.state.fault,
}

local EVSE_SUPPLY_STATE_ENUM_TO_CAPABILITY_MAP = {
local EVSE_SUPPLY_STATE_ENUM_MAP = {
[clusters.EnergyEvse.types.SupplyStateEnum.DISABLED] = capabilities.evseState.supplyState.disabled,
[clusters.EnergyEvse.types.SupplyStateEnum.CHARGING_ENABLED] = capabilities.evseState.supplyState.chargingEnabled,
[clusters.EnergyEvse.types.SupplyStateEnum.DISCHARGING_ENABLED] = capabilities.evseState.supplyState.dischargingEnabled,
[clusters.EnergyEvse.types.SupplyStateEnum.DISABLED_ERROR] = capabilities.evseState.supplyState.disabledError,
[clusters.EnergyEvse.types.SupplyStateEnum.DISABLED_DIAGNOSTICS] = capabilities.evseState.supplyState.disabledDiagnostics,
}

local EVSE_FAULTY_STATE_ENUM_TO_CAPABILITY_MAP = {
local EVSE_FAULT_STATE_ENUM_MAP = {
[clusters.EnergyEvse.types.FaultStateEnum.NO_ERROR] = capabilities.evseState.faultState.noError,
[clusters.EnergyEvse.types.FaultStateEnum.METER_FAILURE] = capabilities.evseState.faultState.meterFailure,
[clusters.EnergyEvse.types.FaultStateEnum.OVER_VOLTAGE] = capabilities.evseState.faultState.overVoltage,
Expand All @@ -155,12 +155,10 @@ local EVSE_FAULTY_STATE_ENUM_TO_CAPABILITY_MAP = {
}

local function read_cumulative_energy_imported(device)
local electrical_energy_measurement_eps = embedded_cluster_utils.get_endpoints(device,
clusters.ElectricalEnergyMeasurement.ID) or {}
if #electrical_energy_measurement_eps > 0 then
local read_req = clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:read(device,
electrical_energy_measurement_eps[1])
for i, ep in ipairs(electrical_energy_measurement_eps) do
local electrical_energy_meas_eps = embedded_cluster_utils.get_endpoints(device, clusters.ElectricalEnergyMeasurement.ID)
if electrical_energy_meas_eps and #electrical_energy_meas_eps > 0 then
local read_req = clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:read(device, electrical_energy_meas_eps[1])
for i, ep in ipairs(electrical_energy_meas_eps) do
if i > 1 then
read_req:merge(clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported:read(device, ep))
end
Expand Down Expand Up @@ -254,7 +252,7 @@ local function device_init(driver, device)
create_poll_schedules_for_cumulative_energy_imported(device)
local current_time = os.time()
local current_time_iso8601 = epoch_to_iso8601(current_time)
--emit current time by default
-- emit current time by default
device:emit_event(capabilities.evseChargingSession.targetEndTime(current_time_iso8601))
end

Expand All @@ -276,7 +274,7 @@ local function do_configure(driver, device)
local device_energy_mgmt_eps = embedded_cluster_utils.get_endpoints(device, clusters.DeviceEnergyManagementMode) or {}
local profile_name = "evse"

--As per spec, atleast one of electrical energy measurement or electrical power measurement clusters are to be supported.
-- As per spec, at least one of the electrical energy measurement or electrical power measurement clusters are to be supported.
if #energy_meas_eps > 0 then
profile_name = profile_name .. "-energy-meas"
end
Expand All @@ -288,7 +286,7 @@ local function do_configure(driver, device)
profile_name = profile_name .. "-energy-mgmt-mode"
end

log.info_with({ hub_logs = true }, "Updating device profile to " .. profile_name)
device.log.info_with({ hub_logs = true }, string.format("Updating device profile to %s.", profile_name))
device:try_update_metadata({ profile = profile_name })
end

Expand Down Expand Up @@ -328,7 +326,7 @@ local function evse_state_handler(driver, device, ib, response)
capabilities.evseState.ID,
capabilities.evseState.supplyState.NAME
)
local event = EVSE_STATE_ENUM_TO_CAPABILITY_MAP[evse_state]
local event = EVSE_STATE_ENUM_MAP[evse_state]
if event then
device:emit_event_for_endpoint(ib.endpoint_id, event())
charging_readiness_state_handler(driver, device, event, {NAME=latest_supply_state})
Expand All @@ -345,7 +343,7 @@ local function evse_supply_state_handler(driver, device, ib, response)
capabilities.evseState.ID,
capabilities.evseState.state.NAME
)
local event = EVSE_SUPPLY_STATE_ENUM_TO_CAPABILITY_MAP[evse_supply_state]
local event = EVSE_SUPPLY_STATE_ENUM_MAP[evse_supply_state]
if event then
device:emit_event_for_endpoint(ib.endpoint_id, event())
charging_readiness_state_handler(driver, device, {NAME=latest_evse_state}, event)
Expand All @@ -354,14 +352,14 @@ local function evse_supply_state_handler(driver, device, ib, response)
end
end

local function evse_faulty_state_handler(driver, device, ib, response)
local evse_faulty_state = ib.data.value
local event = EVSE_FAULTY_STATE_ENUM_TO_CAPABILITY_MAP[evse_faulty_state]
local function evse_fault_state_handler(driver, device, ib, response)
local evse_fault_state = ib.data.value
local event = EVSE_FAULT_STATE_ENUM_MAP[evse_fault_state]
if event then
device:emit_event_for_endpoint(ib.endpoint_id, event())
return
end
log.warn("evse_faulty_state_handler invalid EVSE Faulty State: " .. evse_faulty_state)
log.warn("Invalid EVSE fault state received: " .. evse_fault_state)
end

local function evse_charging_enabled_until_handler(driver, device, ib, response)
Expand All @@ -375,7 +373,7 @@ local function evse_charging_enabled_until_handler(driver, device, ib, response)
device:emit_event_for_endpoint(ep, capabilities.evseChargingSession.targetEndTime(targetEndTime))
return
end
log.warn("evse_charging_enabled_until_handler invalid EVSE Target End TIme, not reporting!")
log.warn("Charging enabled handler received an invalid target end time, not reporting")
end

local function evse_current_limit_handler(event)
Expand Down Expand Up @@ -439,8 +437,7 @@ local function energy_evse_supported_modes_attr_handler(driver, device, ib, resp
end

local function energy_evse_mode_attr_handler(driver, device, ib, response)
device.log.info_with({ hub_logs = true },
string.format("energy_evse_modes_attr_handler currentMode: %s", ib.data.value))
device.log.info(string.format("energy_evse_modes_attr_handler currentMode: %s", ib.data.value))

local supportedEvseModesMap = device:get_field(SUPPORTED_EVSE_MODES_MAP) or {}
local supportedEvseModes = supportedEvseModesMap[ib.endpoint_id] or {}
Expand Down Expand Up @@ -469,8 +466,7 @@ local function device_energy_mgmt_supported_modes_attr_handler(driver, device, i
end

local function device_energy_mgmt_mode_attr_handler(driver, device, ib, response)
device.log.info_with({ hub_logs = true },
string.format("device_energy_mgmt_mode_attr_handler currentMode: %s", ib.data.value))
device.log.info(string.format("device_energy_mgmt_mode_attr_handler currentMode: %s", ib.data.value))

local supportedDeviceEnergyMgmtModesMap = device:get_field(SUPPORTED_DEVICE_ENERGY_MANAGEMENT_MODES_MAP) or {}
local supportedDeviceEnergyMgmtModes = supportedDeviceEnergyMgmtModesMap[ib.endpoint_id] or {}
Expand Down Expand Up @@ -571,7 +567,7 @@ local handle_set_charging_parameters = function(cap, arg)
log.warn_with({hub_logs=true}, "Clipping Max Current as it cannot be greater than 80A")
end
local capability_event = cap(cmd.args[arg])
log.info_with({hub_logs=true}, "Setting value " .. (cmd.args[arg]) .. " for".. (cap.NAME))
log.info("Setting value " .. (cmd.args[arg]) .. " for " .. (cap.NAME))
device:emit_event(capability_event)
end
end
Expand Down Expand Up @@ -625,7 +621,7 @@ matter_driver_template = {
[clusters.EnergyEvse.ID] = {
[clusters.EnergyEvse.attributes.State.ID] = evse_state_handler,
[clusters.EnergyEvse.attributes.SupplyState.ID] = evse_supply_state_handler,
[clusters.EnergyEvse.attributes.FaultState.ID] = evse_faulty_state_handler,
[clusters.EnergyEvse.attributes.FaultState.ID] = evse_fault_state_handler,
[clusters.EnergyEvse.attributes.ChargingEnabledUntil.ID] = evse_charging_enabled_until_handler,
[clusters.EnergyEvse.attributes.MinimumChargeCurrent.ID] = evse_current_limit_handler(capabilities
.evseChargingSession.minCurrent),
Expand All @@ -642,15 +638,12 @@ matter_driver_template = {
[clusters.EnergyEvseMode.attributes.CurrentMode.ID] = energy_evse_mode_attr_handler,
},
[clusters.DeviceEnergyManagementMode.ID] = {
[clusters.DeviceEnergyManagementMode.attributes.SupportedModes.ID] =
device_energy_mgmt_supported_modes_attr_handler,
[clusters.DeviceEnergyManagementMode.attributes.SupportedModes.ID] = device_energy_mgmt_supported_modes_attr_handler,
[clusters.DeviceEnergyManagementMode.attributes.CurrentMode.ID] = device_energy_mgmt_mode_attr_handler,
},
[clusters.ElectricalEnergyMeasurement.ID] = {
[clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported.ID] =
cumulative_energy_imported_handler,
[clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported.ID] =
periodic_energy_imported_handler
[clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyImported.ID] = cumulative_energy_imported_handler,
[clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyImported.ID] = periodic_energy_imported_handler
},
},
},
Expand Down Expand Up @@ -702,6 +695,5 @@ matter_driver_template = {
}

local matter_driver = MatterDriver("matter-evse", matter_driver_template)
log.info_with({ hub_logs = true },
string.format("Starting %s driver, with dispatcher: %s", matter_driver.NAME, matter_driver.matter_dispatcher))
log.info(string.format("Starting %s driver, with dispatcher: %s", matter_driver.NAME, matter_driver.matter_dispatcher))
matter_driver:run()
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ test.register_coroutine_test(
)

test.register_coroutine_test(
"Ensure the total accumulated powerConsumption for both endpoins is reported every 15 minutes",
"Ensure the total accumulated powerConsumption for both endpoints is reported every 15 minutes",
function()
test.socket.matter:__set_channel_ordering("relaxed")
test.socket.capability:__set_channel_ordering("relaxed")
Expand All @@ -189,6 +189,7 @@ test.register_coroutine_test(
ELECTRICAL_SENSOR_EP_TWO,
clusters.ElectricalEnergyMeasurement.types.EnergyMeasurementStruct({ energy = 150000, start_timestamp = 0, end_timestamp = 0, start_systime = 0, end_systime = 0 })) }) --150Wh

test.wait_for_events()
test.mock_time.advance_time(60 * 15)

test.socket.capability:__expect_send(
Expand Down
21 changes: 14 additions & 7 deletions drivers/SmartThings/matter-switch/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ local detect_matter_thing

local CUMULATIVE_REPORTS_NOT_SUPPORTED = "__cumulative_reports_not_supported"
local FIRST_EXPORT_REPORT_TIMESTAMP = "__first_export_report_timestamp"
local EXPORT_POLL_TIMER_IS_SET = "__export_poll_timer_is_set"
local EXPORT_POLL_TIMER_SETTING_ATTEMPTED = "__export_poll_timer_setting_attempted"
local EXPORT_REPORT_TIMEOUT = "__export_report_timeout"
local TOTAL_EXPORTED_ENERGY = "__total_exported_energy"
local LAST_EXPORTED_REPORT_TIMESTAMP = "__last_exported_report_timestamp"
local RECURRING_EXPORT_REPORT_POLL_TIMER = "__recurring_export_report_poll_timer"
local MINIMUM_ST_ENERGY_REPORT_INTERVAL = (15 * 60) -- 15 minutes, reported in seconds
local SUBSCIPTION_REPORT_OCCURRED = "__subscription_report_occurred"
local SUBSCRIPTION_REPORT_OCCURRED = "__subscription_report_occurred"

local embedded_cluster_utils = require "embedded-cluster-utils"

Expand All @@ -179,7 +179,7 @@ local function delete_export_poll_schedule(device)
if export_poll_timer then
device.thread:cancel_timer(export_poll_timer)
device:set_field(RECURRING_EXPORT_REPORT_POLL_TIMER, nil)
device:set_field(EXPORT_POLL_TIMER_IS_SET, nil)
device:set_field(EXPORT_POLL_TIMER_SETTING_ATTEMPTED, nil)
end
end

Expand Down Expand Up @@ -223,8 +223,8 @@ local function set_poll_report_timer_and_schedule(device, is_cumulative_report)
end
if #cumul_eps > 0 and not is_cumulative_report then
return
elseif not device:get_field(SUBSCIPTION_REPORT_OCCURRED) then
device:set_field(SUBSCIPTION_REPORT_OCCURRED, true)
elseif not device:get_field(SUBSCRIPTION_REPORT_OCCURRED) then
device:set_field(SUBSCRIPTION_REPORT_OCCURRED, true)
elseif not device:get_field(FIRST_EXPORT_REPORT_TIMESTAMP) then
device:set_field(FIRST_EXPORT_REPORT_TIMESTAMP, os.time())
else
Expand All @@ -236,7 +236,7 @@ local function set_poll_report_timer_and_schedule(device, is_cumulative_report)
if device:supports_capability(capabilities.powerConsumptionReport) then
create_poll_report_schedule(device)
end
device:set_field(EXPORT_POLL_TIMER_IS_SET, true)
device:set_field(EXPORT_POLL_TIMER_SETTING_ATTEMPTED, true)
end
end

Expand Down Expand Up @@ -934,7 +934,7 @@ end

local function energy_report_handler_factory(is_cumulative_report)
return function(driver, device, ib, response)
if not device:get_field(EXPORT_POLL_TIMER_IS_SET) then
if not device:get_field(EXPORT_POLL_TIMER_SETTING_ATTEMPTED) then
set_poll_report_timer_and_schedule(device, is_cumulative_report)
end
if is_cumulative_report then
Expand Down Expand Up @@ -1167,6 +1167,13 @@ local matter_driver_template = {
[capabilities.battery.ID] = {
clusters.PowerSource.attributes.BatPercentRemaining,
},
[capabilities.energyMeter.ID] = {
clusters.ElectricalEnergyMeasurement.attributes.CumulativeEnergyExported,
clusters.ElectricalEnergyMeasurement.attributes.PeriodicEnergyExported
},
[capabilities.powerMeter.ID] = {
clusters.ElectricalPowerMeasurement.attributes.ActivePower
}
},
subscribed_events = {
[capabilities.button.ID] = {
Expand Down
Loading

0 comments on commit 93a326f

Please sign in to comment.