Files
community-plugins/hassio/widget.luau
T
ArturandGitHub ef512945ce fix(hassio): Resolve perpetual "connecting" and add unreachable state (#78)
* fix(service_sse): Fixed perpetual connecting from hung states request.

* fix(service_sse): Distinguish unreachable server from disconnected/auth
states.

* chore(plugin): Bumped version to 2.0.3
2026-07-21 23:31:05 -04:00

105 lines
2.4 KiB
Luau

--!nonstrict
local haUrlRaw = noctalia.getConfig("ha_url")
local showEntityCount = noctalia.getConfig("show_entity_count")
local status = "unconfigured"
local entityCount = 0
local render
local function getCleanUrl()
if type(haUrlRaw) ~= "string" then return "" end
return (haUrlRaw:gsub("/$", ""))
end
local function tr(key, subst)
return noctalia.tr("widget." .. key, subst)
end
noctalia.state.watch("connection_status", function(value)
status = value or "unconfigured"
if render then render() end
end)
noctalia.state.watch("entity_count", function(value)
entityCount = value or 0
if render then render() end
end)
local function getStatusText()
if status == "connected" then
return tr("status_connected")
elseif status == "connecting" then
return tr("status_connecting")
elseif status == "disconnected" then
return tr("status_disconnected")
elseif status == "unreachable" then
return tr("status_unreachable")
elseif status == "auth_failed" then
return tr("status_auth_failed")
else
return tr("status_unconfigured")
end
end
local function getStatusColor()
if status == "connected" then
return "primary"
elseif status == "connecting" then
return "on_error"
elseif status == "disconnected" or status == "unreachable" or status == "auth_failed" then
return "error"
else
return "on_surface_variant"
end
end
render = function()
barWidget.setGlyph("smart-home")
barWidget.setGlyphColor(getStatusColor())
if showEntityCount and entityCount > 0 then
barWidget.setText(tostring(entityCount))
barWidget.setTooltip(tr("tooltip_entities", {
status = getStatusText(),
count = entityCount
}))
else
barWidget.setTooltip(tr("tooltip", {
status = getStatusText()
}))
end
end
function update()
noctalia.setUpdateInterval(5000)
if status == "unconfigured" then
local serviceStatus = noctalia.state.get("connection_status")
if serviceStatus then
status = serviceStatus
end
end
render()
end
function onClick()
noctalia.togglePanel("pozzoo/hassio:entity_manager")
end
function onRightClick()
local url = getCleanUrl()
if url and url ~= "" then
noctalia.runAsync("xdg-open " .. ("'" .. url:gsub("'", "'\\''") .. "'"))
end
end
function onIpc(event, payload)
if event == "refresh" then
noctalia.state.set("command", "refresh")
noctalia.notify(noctalia.tr("shortcut.title"), tr("refreshing_connection"))
end
end