Make this usable as an external_component

Since v1.18.0 esphome supports external components which allow you to
use custom components much more easily.

https://esphome.io/components/external_components.html
This commit is contained in:
Simon Knopp 2021-05-26 17:50:45 +12:00
parent 06bc22c025
commit d15916fbdc
4 changed files with 49 additions and 0 deletions

3
.gitignore vendored
View File

@ -30,3 +30,6 @@
*.exe
*.out
*.app
# Python cache
__pycache__

View File

@ -0,0 +1,46 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import climate
from esphome.components.logger import HARDWARE_UART_TO_SERIAL
from esphome.const import CONF_ID, CONF_HARDWARE_UART, CONF_UPDATE_INTERVAL
from esphome.core import CORE, coroutine
AUTO_LOAD = ["climate"]
MitsubishiHeatPump = cg.global_ns.class_("MitsubishiHeatPump", climate.Climate, cg.PollingComponent)
def valid_uart(uart):
if CORE.is_esp8266:
uarts = ["UART0"] # UART1 is tx-only
elif CORE.is_esp32:
uarts = ["UART0", "UART1", "UART2"]
else:
raise NotImplementedError
return cv.one_of(*uarts, upper=True)(uart)
CONFIG_SCHEMA = climate.CLIMATE_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(MitsubishiHeatPump),
cv.Optional(CONF_HARDWARE_UART, default="UART0"): valid_uart,
# If polling interval is greater than 9 seconds, the HeatPump library
# reconnects, but doesn't then follow up with our data request.
cv.Optional(CONF_UPDATE_INTERVAL, default="500ms"): cv.All(
cv.update_interval,
cv.Range(max=cv.TimePeriod(milliseconds=9000))
),
}
).extend(cv.COMPONENT_SCHEMA)
@coroutine
def to_code(config):
serial = HARDWARE_UART_TO_SERIAL[config[CONF_HARDWARE_UART]]
var = cg.new_Pvariable(config[CONF_ID], cg.RawExpression(f'&{serial}'))
yield cg.register_component(var, config)
yield climate.register_climate(var, config)
cg.add_library("https://github.com/SwiCago/HeatPump", None)