VeliML / MitsuRunner

ESP Home configuration file for ESP devices made for preventing Mitsubishi heatpumps to do too short heating runs and melt ice too often-
10 stars 6 forks source link

Refactoring to separate configurations #34

Open Hapattaja opened 8 months ago

Hapattaja commented 8 months ago

I suggest a small refactoring, where the actual critical logic of MitsuRunner would be separated from the configuration of a device and other configurations not directly related to MitsuRunner. The goal is that you wouldn't have to touch MitsuRunner's files at all.

This would make it possible to change MitsuRunner logic/version by only changing the include definition in device's main configuration yaml or replacing MitsuRunner's files. This makes it also more safe to update a new version of MitsuRunner.

(Of course the final solution is to create an external component, but implementing needs more work and I think it could be done later.)

I think that MitsuRunner relies on DS18B20 sensors, so it would be wise to configure them in mitsurunner.yaml. Additional sensor filters (calibration etc.) can be added by extending existing sensor definition. But for example MQTT configuration should not be a part of MitsuRunner although MitsuRunner currently requires it without changing the code.

Sensor names and ids could be substitutional also, maybe with some kind of prefix so if there will be more sensors in the future, the sensors would be named automatically consistently. There are some different approaches and needs some thinking to get it "right".

(https://esphome.io/guides/configuration-types.html#packages-as-templates)

If you like this kind of approach, please let me know. I could do a working draft with minimal changes to the current code.

Folder structure example

./tarkkala-main-heatpump-monitor.yaml
./packages/mitsurunner/mitsurunner.yaml
./packages/mitsurunner/<other files>

Main device configuration (tarkkala-main-heatpump-monitor.yaml) example

substitutions:
  id_prefix: tarkkala_main_heatpump
  name_prefix: Tarkkala - Main - Heat pump

  device_name: tarkkala-heatpump-monitor
  device_friendly_name: ${name_prefix} Monitor
  board: d1_mini
  platform: esp8266

  # MitsuRunner substitutions
  heat_exchanger_temp_sensor_id: ${id_prefix}_heat_exchanger_temperature
  heat_exchanger_temp_sensor_name: ${name_prefix} - Heat exchanger - Temperature
  outdoor_temp_sensor_id: ${id_prefix}_outdoor_temperature
  outdoor_temp_sensor_name: ${name_prefix} - Outdoor - Temperature

  dallas_address_heat_exchanger_temp: '0x653C01D607697928' # required substitution
  dallas_address_outdoor_temp: '0x93000802B67D1E10' # required substitution
  dallas_pin: "D7" # optional
  relay_pin: "D1" # optional

packages:
  base: !include packages/device_base.yaml
  mqtt_client: !include packages/mqtt_client.yaml
  ...
  mitsurunner: !include packages/mitsurunner/mitsurunner.yaml

Sensor calibration example, other settings are still in mitsurunner.yaml

sensor:
  - id: !extend ${heat_exchanger_temp_sensor_id}
    filters:
    - offset: -1.8

./packages/mitsurunner/mitsutunner.yaml

substitutions:
  # Default values
  heat_exchanger_temp_sensor_id: "heat_exhanger_temp"
  heat_exchanger_temp_sensor_name: "Heat Exchanger Temperature (Outdoor Unit)"
  outdoor_temp_sensor_id: "outdoor_temp"
  outdoor_temp_sensor_name: "Outdoor Temperature"

# initialize and define Mitsurunner
esphome:
  on_boot:
    - switch.turn_off: gpio_relay
    - script.execute: schedule_forced_defrosting
    - script.execute: initialize
  includes:
    - packages/mitsurunner/constants.h
    - packages/mitsurunner/state.h
...
MarkoPaasila commented 8 months ago

Sounds good! I’m using native api instead of mqtt and I had to modify the code a little for that reason. Now it’s harder to import upstream changes. Splicing it up would make it easier, I assume. I also welcome getting the logic out of the main config!