Skip to content

Commit

Permalink
chore: Typechecking fixes
Browse files Browse the repository at this point in the history
- Mostly updates type hints/annotations
- Fix one place where typechecks caught a missing nil check
  • Loading branch information
dljsjr committed Dec 19, 2024
1 parent ac0ebbb commit da07ae3
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 5 deletions.
3 changes: 3 additions & 0 deletions drivers/SmartThings/philips-hue/src/disco/button.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local log = require "log"
local socket = require "cosock".socket
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local HueDeviceTypes = require "hue_device_types"

Expand Down
3 changes: 3 additions & 0 deletions drivers/SmartThings/philips-hue/src/disco/contact.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local log = require "log"
local socket = require "cosock".socket
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local HueDeviceTypes = require "hue_device_types"

Expand Down
3 changes: 3 additions & 0 deletions drivers/SmartThings/philips-hue/src/disco/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ local socket = require "cosock.socket"
local mdns = require "st.mdns"
local net_utils = require "st.net_utils"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Fields = require "fields"
local HueApi = require "hue.api"
Expand Down
3 changes: 3 additions & 0 deletions drivers/SmartThings/philips-hue/src/disco/light.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local log = require "log"
local socket = require "cosock".socket
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local HueDeviceTypes = require "hue_device_types"

Expand Down
3 changes: 3 additions & 0 deletions drivers/SmartThings/philips-hue/src/disco/motion.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local log = require "log"
local socket = require "cosock".socket
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local HueDeviceTypes = require "hue_device_types"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local capabilities = require "st.capabilities"
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: table, name: string?, multi_line: boolean?): string
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Consts = require "consts"
Expand Down
5 changes: 4 additions & 1 deletion drivers/SmartThings/philips-hue/src/handlers/commands.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Consts = require "consts"
local Fields = require "fields"
Expand All @@ -8,7 +11,7 @@ local HueColorUtils = require "utils.cie_utils"
local utils = require "utils"

-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: table, name: string?, multi_line: boolean?): string
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

---@class CommandHandlers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local capabilities = require "st.capabilities"
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local refresh_handler = require("handlers.commands").refresh_handler

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local refresh_handler = require("handlers.commands").refresh_handler

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local capabilities = require "st.capabilities"

local Discovery = require "disco"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local log = require "log"
local capabilities = require "st.capabilities"
local refresh_handler = require("handlers.commands").refresh_handler
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Consts = require "consts"
local Discovery = require "disco"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local refresh_handler = require("handlers.commands").refresh_handler

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local capabilities = require "st.capabilities"
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Discovery = require "disco"
local Fields = require "fields"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local cosock = require "cosock"
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Fields = require "fields"
local HueDeviceTypes = require "hue_device_types"
Expand Down Expand Up @@ -123,6 +126,7 @@ function RefreshHandlers.do_refresh_all_for_bridge(driver, bridge_device)
-- but only the first time we encounter a device type. We cache them since we're refreshing
-- everything.
if
device_type and
type(device_type_refresh_handlers_map[device_type]) == "function" and
statuses_by_device_type[device_type] == nil
then
Expand Down
10 changes: 9 additions & 1 deletion drivers/SmartThings/philips-hue/src/hue/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ local json = require "st.json"
local log = require "log"
local RestClient = require "lunchbox.rest"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local HueDeviceTypes = require "hue_device_types"

-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: table, name: string?, multi_line: boolean?): string
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local APPLICATION_KEY_HEADER = "hue-application-key"
Expand Down Expand Up @@ -82,6 +85,7 @@ end
---@return table? tbl the table representation of the JSON response, nil on error
---@return string? err the error message, nil on success
---@return string? partial the partial response if the response was not complete
---@return ...
local function process_rest_response(response, err, partial, err_callback)
if err == nil and response == nil then
log.error_with({ hub_logs = true },
Expand Down Expand Up @@ -208,6 +212,7 @@ end
---@param path string
---@return table|nil response REST response, nil if error
---@return nil|string error nil on success
---@return ...
local function do_get(instance, path)
local reply_tx, reply_rx = channel.new()
reply_rx:settimeout(10)
Expand All @@ -226,6 +231,7 @@ end
---@param payload string
---@return table|nil response REST response, nil if error
---@return nil|string error nil on success
---@return ...
local function do_put(instance, path, payload)
local reply_tx, reply_rx = channel.new()
reply_rx:settimeout(10)
Expand All @@ -244,6 +250,7 @@ end
---@return HueBridgeInfo|nil bridge_info nil on err
---@return nil|string error nil on success
---@return nil|string partial partial response if available, nil otherwise
---@return ...
function PhilipsHueApi.get_bridge_info(bridge_ip, socket_builder)
local tx, rx = channel.new()
rx:settimeout(10)
Expand All @@ -266,6 +273,7 @@ end
---@return HueApiKeyResponse[]? api_key_response nil on err
---@return string? error nil on success
---@return string? partial partial response if available, nil otherwise
---@return ...
function PhilipsHueApi.request_api_key(bridge_ip, socket_builder)
local tx, rx = channel.new()
rx:settimeout(10)
Expand Down
3 changes: 3 additions & 0 deletions drivers/SmartThings/philips-hue/src/hue_debug/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local log = require "log"
local utils = require "utils"
Expand Down
2 changes: 1 addition & 1 deletion drivers/SmartThings/philips-hue/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local Driver = require "st.driver"
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: table, name: string?, multi_line: boolean?): string
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Discovery = require "disco"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ local ssl = require "cosock.ssl"
---@type fun(sock: table, config: table?): table?, string?
ssl.wrap = ssl.wrap

local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local log = require "log"
local util = require "lunchbox.util"
local Request = require "luncheon.request"
Expand Down Expand Up @@ -474,7 +479,6 @@ function EventSource.new(url, extra_headers, sock_builder)
}, EventSource)

cosock.spawn(function()
local st_utils = require "st.utils"
while true do
if source.ready_state == EventSource.ReadyStates.CLOSED and
not source._reconnect
Expand Down
3 changes: 3 additions & 0 deletions drivers/SmartThings/philips-hue/src/stray_device_helper.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local cosock = require "cosock"
local log = require "log"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Discovery = require "disco"
local Fields = require "fields"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local utils = require "utils"
local lazy_fakers = utils.lazy_handler_loader("fakers")

local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local test_helpers = require "test_helpers"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local function make_migrated_device(faker_args, bridge_info)
local device_network_id = faker_args.dni or st_utils.generate_uuid_v4()
Expand Down
4 changes: 4 additions & 0 deletions drivers/SmartThings/philips-hue/src/utils/cie_utils.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local CieUtils = {}

local DefaultGamut = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local cosock = require "cosock"
local log = require "log"
local json = require "st.json"
local st_utils = require "st.utils"
-- trick to fix the VS Code Lua Language Server typechecking
---@type fun(val: any?, name: string?, multi_line: boolean?): string
st_utils.stringify_table = st_utils.stringify_table

local Discovery = require "disco"
local EventSource = require "lunchbox.sse.eventsource"
Expand Down

0 comments on commit da07ae3

Please sign in to comment.