Hi. @doug.stephen
this is really whole code of mine
I just started write code, it’s so simple.
4 files.
src/disco.lua
local log = require "log"
-- local old_log = require "log"
-- old_log.error = function(...)
-- old_log.error_with({ hub_logs = true }, ...)
-- end
-- old_log.warn = function(...)
-- old_log.warn_with({ hub_logs = true }, ...)
-- end
-- old_log.info = function(...)
-- old_log.info_with({ hub_logs = true }, ...)
-- end
-- local log = {}
-- log.warn = old_log.warn
-- log.error = old_log.error
-- log.debug = old_log.debug
-- log.trace = old_log.trace
-- log.info = old_log.info
local socket = require "cosock.socket"
local mdns = require "st.mdns"
local net_utils = require "st.net_utils"
local st_utils = require "st.utils"
local SERVICE_TYPE = "_worlty._tcp"
local DOMAIN = "local"
local WorltyDiscovery = {
ServiceType = SERVICE_TYPE,
Domain = DOMAIN,
device_state_cache = {},
discovery_active = false
}
local WorltyTypeEnum = {
"none",
"binary_sensor",
"climate",
"fan",
"event",
"input",
"light",
"sensor",
"switch"
}
function WorltyDiscovery.discover(driver, _, should_continue)
log.info("Okay, Start discover...")
while should_continue() do
local mdns_responses, err = mdns.discover(WorltyDiscovery.ServiceType, WorltyDiscovery.Domain)
log.info("Start discover...")
if err ~= nil then
log.error("Error during service discovery: ", err)
return
end
for _, info in ipairs(mdns_responses.found) do
log.info(info)
end
socket.sleep(1.0)
end
end
return WorltyDiscovery
src/init.lua
local log = require "log"
-- local old_log = require "log"
-- old_log.error = function(...)
-- old_log.error_with({ hub_logs = true }, ...)
-- end
-- old_log.warn = function(...)
-- old_log.warn_with({ hub_logs = true }, ...)
-- end
-- old_log.info = function(...)
-- old_log.info_with({ hub_logs = true }, ...)
-- end
-- local log = {}
-- log.warn = old_log.warn
-- log.error = old_log.error
-- log.debug = old_log.debug
-- log.trace = old_log.trace
-- log.info = old_log.info
log.info("init!")
local capabilities = require "st.capabilities"
local Driver = require "st.driver"
local Discovery = require "disco"
local disco = Discovery.discover
local device_init
local template = {
discovery = disco,
lifecycle_handlers = {
init = device_init,
},
supported_capabilities = {
capabilities.refresh,
},
}
device_init = function(driver, device)
log.info("device init")
end
local drvier = Driver("Worlty", template)
log.info("Starting Worlty driver")
drvier:run()
log.warn("Worlty driver exiting")
config.yaml
name: "WorltyDriver"
packageKey: "worlty.local"
description: "Worlty local driver"
vendorSupportInformation: "Worlty"
permission:
lan: {}
discovery: {}
profiles/worlty-pad.yaml
name: worlty-pad
components:
- id: main
capabilities:
- id: refresh
version: 1