implement a better version for home assistant
This commit is contained in:
@@ -37,9 +37,14 @@ msgstr "CodeServer 未运行"
|
||||
msgid "Open CodeServer"
|
||||
msgstr "打开 CodeServer"
|
||||
|
||||
msgid "Tool"
|
||||
msgstr "操作"
|
||||
|
||||
msgid "Console"
|
||||
msgstr "控制台"
|
||||
|
||||
msgid "Only works in LAN"
|
||||
msgstr "只在内网环境下工作。"
|
||||
|
||||
msgid "Execute"
|
||||
msgstr "执行"
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
PKG_VERSION:=1.1.0-20220830
|
||||
PKG_VERSION:=1.1.0-20221125
|
||||
PKG_RELEASE:=
|
||||
|
||||
LUCI_TITLE:=LuCI support for homeassistant
|
||||
|
||||
@@ -3,5 +3,7 @@ module("luci.controller.homeassistant", package.seeall)
|
||||
|
||||
function index()
|
||||
entry({"admin", "services", "homeassistant"}, alias("admin", "services", "homeassistant", "config"), _("Home Assistant"), 30).dependent = true
|
||||
entry({"admin", "services", "homeassistant", "config"}, cbi("homeassistant"))
|
||||
entry({"admin", "services", "homeassistant", "config"}, cbi("homeassistant/config"), _("Config"), 10).leaf = true
|
||||
entry({"admin", "services", "homeassistant", "tool"}, form("homeassistant/tool"), _("Tool"), 30).leaf = true
|
||||
entry({"admin", "services", "homeassistant", "console"}, form("homeassistant/console"), _("Console"), 50).leaf = true
|
||||
end
|
||||
|
||||
@@ -3,6 +3,7 @@ LuCI - Lua Configuration Interface
|
||||
]]--
|
||||
|
||||
local taskd = require "luci.model.tasks"
|
||||
local homeassistant_model = require "luci.model.homeassistant"
|
||||
local m, s, o
|
||||
|
||||
m = taskd.docker_map("homeassistant", "homeassistant", "/usr/libexec/istorec/homeassistant.sh",
|
||||
@@ -17,9 +18,29 @@ s = m:section(TypedSection, "homeassistant", translate("Setup"), translate("The
|
||||
s.addremove=false
|
||||
s.anonymous=true
|
||||
|
||||
o = s:option(Value, "image_name", translate("Image").."<b>*</b>")
|
||||
o.rmempty = false
|
||||
o.datatype = "string"
|
||||
o:value("homeassistant/home-assistant:latest", "homeassistant/home-assistant:latest")
|
||||
o:value("ghcr.io/home-assistant/home-assistant:stable", "ghcr.io/home-assistant/home-assistant:stable")
|
||||
o:value("ghcr.io/home-assistant/home-assistant:2022.11.4", "ghcr.io/home-assistant/home-assistant:2022.11.4")
|
||||
o.default = "homeassistant/home-assistant:latest"
|
||||
|
||||
local blocks = homeassistant_model.blocks()
|
||||
local home = homeassistant_model.home()
|
||||
|
||||
o = s:option(Value, "config_path", translate("Config path").."<b>*</b>")
|
||||
o.rmempty = false
|
||||
o.default = "/root/homeassistant/config"
|
||||
o.datatype = "string"
|
||||
|
||||
local paths, default_path = homeassistant_model.find_paths(blocks, home, "Configs")
|
||||
for _, val in pairs(paths) do
|
||||
o:value(val, val)
|
||||
end
|
||||
o.default = default_path
|
||||
|
||||
o = s:option(Value, "time_zone", translate("Timezone"))
|
||||
o.datatype = "string"
|
||||
o:value("Asia/Shanghai", "Asia/Shanghai")
|
||||
|
||||
return m
|
||||
@@ -0,0 +1,116 @@
|
||||
--[[
|
||||
LuCI - Lua Configuration Interface
|
||||
]]--
|
||||
|
||||
|
||||
require "luci.util"
|
||||
|
||||
local docker = require "luci.model.docker"
|
||||
local dk = docker.new()
|
||||
|
||||
local container_name = "homeassistant"
|
||||
|
||||
local m, s, o
|
||||
local images, networks, container_info, res
|
||||
|
||||
res = dk.containers:inspect({name = container_name})
|
||||
if res.code < 300 then
|
||||
container_info = res.body
|
||||
else
|
||||
return
|
||||
end
|
||||
|
||||
local cmd_docker = luci.util.exec("command -v docker"):match("^.+docker") or nil
|
||||
local cmd_ttyd = luci.util.exec("command -v ttyd"):match("^.+ttyd") or nil
|
||||
|
||||
if cmd_docker and cmd_ttyd and container_info.State.Status == "running" then
|
||||
local cmd = "/bin/bash"
|
||||
local uid
|
||||
|
||||
m=SimpleForm("Console", "", translate("Only works in LAN"))
|
||||
m.submit = false
|
||||
m.reset = false
|
||||
s = m:section(SimpleSection)
|
||||
|
||||
o = s:option(Value, "command", translate("Command"))
|
||||
o:value("/bin/sh", "/bin/sh")
|
||||
o:value("/bin/ash", "/bin/ash")
|
||||
o:value("/bin/bash", "/bin/bash")
|
||||
o.default = "/bin/bash"
|
||||
o.forcewrite = true
|
||||
o.write = function(self, section, value)
|
||||
cmd = value
|
||||
end
|
||||
|
||||
o = s:option(Value, "uid", translate("UID"))
|
||||
o.forcewrite = true
|
||||
o.write = function(self, section, value)
|
||||
uid = value
|
||||
end
|
||||
|
||||
o = s:option(Button, "connect")
|
||||
o.render = function(self, section, scope)
|
||||
self.inputstyle = "add"
|
||||
self.title = " "
|
||||
self.inputtitle = translate("Connect")
|
||||
Button.render(self, section, scope)
|
||||
end
|
||||
o.write = function(self, section)
|
||||
local cmd_docker = luci.util.exec("command -v docker"):match("^.+docker") or nil
|
||||
local cmd_ttyd = luci.util.exec("command -v ttyd"):match("^.+ttyd") or nil
|
||||
|
||||
if not cmd_docker or not cmd_ttyd or cmd_docker:match("^%s+$") or cmd_ttyd:match("^%s+$")then
|
||||
return
|
||||
end
|
||||
|
||||
local pid = luci.util.trim(luci.util.exec("netstat -lnpt | grep :7682 | grep ttyd | tr -s ' ' | cut -d ' ' -f7 | cut -d'/' -f1"))
|
||||
if pid and pid ~= "" then
|
||||
luci.util.exec("kill -9 " .. pid)
|
||||
end
|
||||
|
||||
local hosts
|
||||
local uci = require "luci.model.uci".cursor()
|
||||
local remote = uci:get_bool("dockerd", "globals", "remote_endpoint") or false
|
||||
local host = nil
|
||||
local port = nil
|
||||
local socket = nil
|
||||
|
||||
if remote then
|
||||
host = uci:get("dockerd", "globals", "remote_host") or nil
|
||||
port = uci:get("dockerd", "globals", "remote_port") or nil
|
||||
else
|
||||
socket = uci:get("dockerd", "globals", "socket_path") or "/var/run/docker.sock"
|
||||
end
|
||||
|
||||
if remote and host and port then
|
||||
hosts = host .. ':'.. port
|
||||
elseif socket then
|
||||
hosts = socket
|
||||
else
|
||||
return
|
||||
end
|
||||
|
||||
if uid and uid ~= "" then
|
||||
uid = "-u " .. uid
|
||||
else
|
||||
uid = ""
|
||||
end
|
||||
|
||||
local start_cmd = string.format('%s -d 2 --once -p 7682 %s -H "unix://%s" exec -it %s %s %s&', cmd_ttyd, cmd_docker, hosts, uid, container_name, cmd)
|
||||
|
||||
os.execute(start_cmd)
|
||||
|
||||
m.children[#m.children] = nil
|
||||
s = m:section(SimpleSection)
|
||||
o = s:option(DummyValue, "console")
|
||||
o.container_id = container_id
|
||||
o.template = container_name .. "/console"
|
||||
end
|
||||
else
|
||||
m=SimpleForm("Console", "", translate("Home Assistant is not running"))
|
||||
m.submit = false
|
||||
m.reset = false
|
||||
s = m:section(SimpleSection)
|
||||
end
|
||||
|
||||
return m
|
||||
@@ -0,0 +1,41 @@
|
||||
--[[
|
||||
LuCI - Lua Configuration Interface
|
||||
]]--
|
||||
|
||||
local http = require 'luci.http'
|
||||
|
||||
m=SimpleForm("Tools")
|
||||
m.submit = false
|
||||
m.reset = false
|
||||
|
||||
s = m:section(SimpleSection)
|
||||
|
||||
o = s:option(Value, "action", translate("Action").."<b>*</b>")
|
||||
o.rmempty = false
|
||||
o.datatype = "string"
|
||||
o:value("hacs-install", "hacs-install")
|
||||
o.default = "hacs-install"
|
||||
|
||||
local t=Template("homeassistant/tool")
|
||||
m:append(t)
|
||||
|
||||
local btn_do = s:option(Button, "_do")
|
||||
btn_do.render = function(self, section, scope)
|
||||
self.inputstyle = "add"
|
||||
self.title = " "
|
||||
self.inputtitle = translate("Execute")
|
||||
Button.render(self, section, scope)
|
||||
end
|
||||
|
||||
btn_do.write = function(self, section, value)
|
||||
local action = m:get(section, "action")
|
||||
if action == "hacs-install" then
|
||||
local cmd = string.format("/usr/libexec/istorec/homeassistant.sh %s", action)
|
||||
cmd = "/etc/init.d/tasks task_add homeassistant " .. luci.util.shellquote(cmd) .. " >/dev/null 2>&1"
|
||||
os.execute(cmd)
|
||||
t.show_log_taskid = "homeassistant"
|
||||
end
|
||||
end
|
||||
|
||||
return m
|
||||
|
||||
@@ -33,3 +33,19 @@ msgstr "Home Assistant 未运行"
|
||||
|
||||
msgid "Open the Home Assistant"
|
||||
msgstr "打开 Home Assistant"
|
||||
|
||||
msgid "Tool"
|
||||
msgstr "操作"
|
||||
|
||||
msgid "Console"
|
||||
msgstr "控制台"
|
||||
|
||||
msgid "Only works in LAN"
|
||||
msgstr "只在内网环境下工作。"
|
||||
|
||||
msgid "Execute"
|
||||
msgstr "执行"
|
||||
|
||||
msgid "Timezone"
|
||||
msgstr "时区"
|
||||
|
||||
|
||||
@@ -1,2 +1,4 @@
|
||||
config homeassistant
|
||||
option 'config_path' '/root/homeassistant/config'
|
||||
option 'config_path' ''
|
||||
option 'image_name' 'homeassistant/home-assistant:latest'
|
||||
option 'time_zone' ''
|
||||
|
||||
@@ -3,34 +3,29 @@
|
||||
ACTION=${1}
|
||||
shift 1
|
||||
|
||||
get_image() {
|
||||
IMAGE_NAME="homeassistant/home-assistant:latest"
|
||||
}
|
||||
|
||||
do_install() {
|
||||
get_image
|
||||
echo "docker pull ${IMAGE_NAME}"
|
||||
docker pull ${IMAGE_NAME}
|
||||
docker rm -f homeassistant
|
||||
|
||||
do_install_detail
|
||||
}
|
||||
|
||||
do_install_detail() {
|
||||
local config=`uci get homeassistant.@homeassistant[0].config_path 2>/dev/null`
|
||||
local IMAGE_NAME=`uci get homeassistant.@homeassistant[0].image_name 2>/dev/null`
|
||||
local tz=`uci get homeassistant.@homeassistant[0].time_zone 2>/dev/null`
|
||||
|
||||
if [ -z "$config" ]; then
|
||||
echo "config path is empty!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "docker pull ${IMAGE_NAME}"
|
||||
docker pull ${IMAGE_NAME}
|
||||
docker rm -f homeassistant
|
||||
|
||||
local cmd="docker run --restart=unless-stopped -d \
|
||||
-v \"$config:/config\" \
|
||||
--privileged \
|
||||
--network=host \
|
||||
--dns=127.0.0.1 "
|
||||
|
||||
local tz="`cat /tmp/TZ`"
|
||||
if [ -z "$tz" ]; then
|
||||
tz="`cat /tmp/TZ`"
|
||||
fi
|
||||
[ -z "$tz" ] || cmd="$cmd -e TZ=$tz"
|
||||
|
||||
cmd="$cmd --name homeassistant \"$IMAGE_NAME\""
|
||||
@@ -38,6 +33,17 @@ do_install_detail() {
|
||||
echo "$cmd"
|
||||
eval "$cmd"
|
||||
|
||||
RET=$?
|
||||
if [ "$RET" = "0" ]; then
|
||||
do_hacs_install
|
||||
fi
|
||||
}
|
||||
|
||||
do_hacs_install() {
|
||||
echo "wget -O - https://get.hacs.xyz | bash -" | docker exec -i homeassistant bash -
|
||||
sleep 3
|
||||
echo "restart homeassistant"
|
||||
docker restart homeassistant
|
||||
}
|
||||
|
||||
usage() {
|
||||
@@ -69,6 +75,9 @@ case ${ACTION} in
|
||||
"port")
|
||||
docker ps --all -f 'name=homeassistant' --format '{{.Ports}}' | grep -om1 '0.0.0.0:[0-9]*' | sed 's/0.0.0.0://'
|
||||
;;
|
||||
"hacs-install")
|
||||
do_hacs_install
|
||||
;;
|
||||
*)
|
||||
usage
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user