# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

DISTUTILS_USE_PEP517=setuptools
DISTUTILS_SINGLE_IMPL=1
PYTHON_COMPAT=( python3_{11..14} )

inherit readme.gentoo-r1 distutils-r1 systemd

if [[ ${PV} == *9999* ]]; then
    inherit git-r3
    EGIT_REPO_URI="https://github.com/esphome/esphome.git"
    EGIT_BRANCH="dev"
    S="${WORKDIR}/${P}/"
else
    inherit pypi
    MY_P=${P/_beta/b}
    MY_PV=${PV/_beta/b}
    S="${WORKDIR}/${MY_P}/"
fi

DESCRIPTION="Make creating custom firmwares for ESP32/ESP8266 super easy."
HOMEPAGE="https://github.com/esphome/esphome https://pypi.org/project/esphome/"

LICENSE="MIT"
SLOT="0"
KEYWORDS="~amd64 ~arm ~arm64 ~x86"
IUSE="+esphomeDashboard esphome-device-builder +systemd test"
REQUIRED_USE="^^ ( esphomeDashboard esphome-device-builder )"
RESTRICT="!test? ( test )"

DOCS="README.md"

RDEPEND="$(python_gen_cond_dep '
    esphomeDashboard? (
	acct-group/esphome
	acct-user/esphome
	~dev-embedded/esphome-dashboard-20260425.0[${PYTHON_USEDEP}]
	~dev-python/tornado-6.5.5[${PYTHON_USEDEP}]
    )
    esphome-device-builder? (
	acct-group/esphome
	acct-user/esphome
	~dev-embedded/esphome-device-builder-9999[${PYTHON_SINGLE_USEDEP}]
	~dev-python/tornado-6.5.5[${PYTHON_USEDEP}]
    )
    >=dev-python/cryptography-48.0.0[${PYTHON_USEDEP}]
    ~dev-python/voluptuous-0.16.0[${PYTHON_USEDEP}]
    >=dev-python/pyyaml-6.0.3[${PYTHON_USEDEP}]
    >=dev-python/paho-mqtt-1.6.1[${PYTHON_USEDEP}]
    ~dev-python/colorama-0.4.6[${PYTHON_USEDEP}]
    ~dev-python/icmplib-3.0.4[${PYTHON_USEDEP}]
    ~dev-python/tzlocal-5.3.1[${PYTHON_USEDEP}]
    >=dev-python/tzdata-10001[${PYTHON_USEDEP}]
    ~dev-python/pyserial-3.5[${PYTHON_USEDEP}]
    ~dev-embedded/platformio-6.1.19[${PYTHON_USEDEP}]
    ~dev-embedded/esptool-5.2.0[${PYTHON_SINGLE_USEDEP}]
    ~dev-python/click-8.3.3[${PYTHON_USEDEP}]
    dev-python/aioesphomeapi[${PYTHON_USEDEP}]
    dev-python/zeroconf[${PYTHON_USEDEP}]
    ~dev-python/puremagic-2.2.0[${PYTHON_USEDEP}]
    ~dev-embedded/esphome-glyphsets-0.2.0[${PYTHON_USEDEP}]
    dev-python/pillow[${PYTHON_USEDEP}]
    ~dev-python/resvg-py-0.3.2[${PYTHON_USEDEP}]
    ~dev-python/freetype-py-2.5.1[${PYTHON_USEDEP}]
    ~dev-python/jinja2-3.1.6[${PYTHON_USEDEP}]
    >=dev-python/bleak-2.1.1[${PYTHON_USEDEP}]
    >=dev-python/smpclient-6.0.0[${PYTHON_USEDEP}]
    >=dev-python/requests-2.34.2[${PYTHON_USEDEP}]
    >=dev-python/pyparsing-3.0[${PYTHON_USEDEP}]
    >=dev-python/argcomplete-3.6.3[${PYTHON_USEDEP}]
')"

BDEPEND="$(python_gen_cond_dep '
    dev-python/setuptools[${PYTHON_USEDEP}]
    test? (
	dev-python/pytest[${PYTHON_USEDEP}]
	dev-python/pytest-cov[${PYTHON_USEDEP}]
	dev-python/pytest-mock[${PYTHON_USEDEP}]
	dev-python/pytest-asyncio[${PYTHON_USEDEP}]
	dev-python/asyncmock[${PYTHON_USEDEP}]
	dev-python/hypothesis[${PYTHON_USEDEP}]
    )
')"

DISABLE_AUTOFORMATTING=1

src_prepare() {
    sed "/aioesphomeapi==/c\aioesphomeapi" -i requirements.txt || die
    sed "/click==/c\click" -i requirements.txt || die
    sed "/tornado==/c\tornado" -i requirements.txt || die
    sed "/colorama==/c\colorama" -i requirements.txt || die
    sed "/zeroconf==/c\zeroconf" -i requirements.txt || die
    sed "/voluptuous==/c\voluptuous" -i requirements.txt || die
    sed "/cryptography==/c\cryptography" -i requirements.txt || die
    sed "/icmplib==/c\icmplib" -i requirements.txt || die
    sed "/pyyaml==/c\pyyaml" -i requirements.txt || die
    sed "/paho-mqtt==/c\paho-mqtt" -i requirements.txt || die
    sed "/pillow==/c\pillow" -i requirements.txt || die
    sed "/puremagic==/c\puremagic" -i requirements.txt || die

    eapply_user
}

python_install_all() {
    dodoc ${DOCS}
    distutils-r1_python_install_all

    if use esphomeDashboard; then
	DOC_CONTENTS="
The ESPHome dashboard listens on port 6052
ESPHome configuration is in: /var/lib/${PN}
dashboard command line arguments are configured in: /etc/conf.d/${PN}
logging is to: /var/log/${PN}/{dashboard,warnings}.log
* Please note ebuilds from version 2026.5.0 are using directory /var/lib/${PN} as base dir. You may need to move your existing device code (yaml files) to this dir. The change is to better accommodate the new frontend
support at github.com/tabascoz/ha-bleeding-edge
"
	readme.gentoo_create_doc

	keepdir "/var/lib/${PN}"
	fowners -R "${PN}:${PN}" "/var/lib/${PN}"
	keepdir "/var/log/${PN}"
	fowners -R "${PN}:${PN}" "/var/log/${PN}"
	newconfd "${FILESDIR}/${PN}.conf.d" "${PN}"
	newinitd "${FILESDIR}/${PN}.init.d-r3" "${PN}"

	if use systemd; then
	    systemd_dounit "${FILESDIR}/esphomeDashboard.service"
	fi

    elif use esphome-device-builder; then
	DOC_CONTENTS="
ESPHome device builder listens on port 5173
Configuration is in: /var/lib/${PN}
Data directory: /var/lib/${PN}
* note that these ebuilds are experimental - following upstream.
** Please note ebuilds from version 2026.5.0 are using directory /var/lib/${PN} as base dir. You may need to move your existing device code (yaml files) to this dir. The change is to better accommodate the new frontend
support at github.com/tabascoz/ha-bleeding-edge
"
	readme.gentoo_create_doc

	keepdir "/var/lib/${PN}"
	fowners -R "${PN}:${PN}" "/var/lib/${PN}"
	keepdir "/var/log/${PN}"
	fowners -R "${PN}:${PN}" "/var/log/${PN}"
	newconfd "${FILESDIR}/${PN}.conf.d" "${PN}"
	newinitd "${FILESDIR}/${PN}.init.d-r3" "${PN}"

	#if use systemd; then
	#    systemd_dounit "${FILESDIR}/esphome-device-builder.service"
	#fi
    fi


}

pkg_postinst() {

    if use esphomeDashboard || use esphome-device-builder; then
    readme.gentoo_print_elog
    fi
}

distutils_enable_tests pytest