From d15916fbdce94160d9980958fbd4140b3799b09f Mon Sep 17 00:00:00 2001 From: Simon Knopp Date: Wed, 26 May 2021 17:50:45 +1200 Subject: [PATCH] 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 --- .gitignore | 3 ++ components/mitsubishi_heatpump/__init__.py | 46 +++++++++++++++++++ .../mitsubishi_heatpump/espmhp.cpp | 0 .../mitsubishi_heatpump/espmhp.h | 0 4 files changed, 49 insertions(+) create mode 100644 components/mitsubishi_heatpump/__init__.py rename espmhp.cpp => components/mitsubishi_heatpump/espmhp.cpp (100%) rename espmhp.h => components/mitsubishi_heatpump/espmhp.h (100%) diff --git a/.gitignore b/.gitignore index 259148f..9c89171 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ *.exe *.out *.app + +# Python cache +__pycache__ diff --git a/components/mitsubishi_heatpump/__init__.py b/components/mitsubishi_heatpump/__init__.py new file mode 100644 index 0000000..b5f213d --- /dev/null +++ b/components/mitsubishi_heatpump/__init__.py @@ -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) + diff --git a/espmhp.cpp b/components/mitsubishi_heatpump/espmhp.cpp similarity index 100% rename from espmhp.cpp rename to components/mitsubishi_heatpump/espmhp.cpp diff --git a/espmhp.h b/components/mitsubishi_heatpump/espmhp.h similarity index 100% rename from espmhp.h rename to components/mitsubishi_heatpump/espmhp.h