Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR Feedback #55

Merged
merged 12 commits into from
Aug 10, 2024
39 changes: 39 additions & 0 deletions esphome/components/mitsubishi_itp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import climate
from esphome.const import CONF_ID

CODEOWNERS = ["@Sammy1Am", "@KazWolfe"]

mitsubishi_itp_ns = cg.esphome_ns.namespace("mitsubishi_itp")
MitsubishiUART = mitsubishi_itp_ns.class_(
"MitsubishiUART", cg.PollingComponent, climate.Climate
)
CONF_MITSUBISHI_ITP_ID = "mitsubishi_itp_id"


def sensors_to_config_schema(sensors):
return cv.Schema(
{
cv.GenerateID(CONF_MITSUBISHI_ITP_ID): cv.use_id(MitsubishiUART),
}
).extend(
{
cv.Optional(sensor_designator): sensor_schema
for sensor_designator, sensor_schema in sensors.items()
}
)


async def sensors_to_code(config, sensors, registration_function):
muart_component = await cg.get_variable(config[CONF_MITSUBISHI_ITP_ID])

# Sensors

for sensor_designator, _ in sensors.items():
if sensor_conf := config.get(sensor_designator):
sensor_component = cg.new_Pvariable(sensor_conf[CONF_ID])

await registration_function(sensor_component, sensor_conf)

cg.add(getattr(muart_component, "register_listener")(sensor_component))
51 changes: 51 additions & 0 deletions esphome/components/mitsubishi_itp/binary_sensor/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import esphome.config_validation as cv
from esphome.components import (
binary_sensor,
)
from esphome.core import coroutine
from ...mitsubishi_itp import (
mitsubishi_itp_ns,
sensors_to_config_schema,
sensors_to_code,
)

CONF_ISEE_STATUS = "isee_status"


DefrostSensor = mitsubishi_itp_ns.class_("DefrostSensor", binary_sensor.BinarySensor)
FilterStatusSensor = mitsubishi_itp_ns.class_(
"FilterStatusSensor", binary_sensor.BinarySensor
)
ISeeStatusSensor = mitsubishi_itp_ns.class_(
"ISeeStatusSensor", binary_sensor.BinarySensor
)
PreheatSensor = mitsubishi_itp_ns.class_("PreheatSensor", binary_sensor.BinarySensor)
StandbySensor = mitsubishi_itp_ns.class_("StandbySensor", binary_sensor.BinarySensor)

# TODO Storing the registration function here seems weird, but I can't figure out how to determine schema type later
SENSORS = dict[str, cv.Schema](
{
"defrost": binary_sensor.binary_sensor_schema(
DefrostSensor, icon="mdi:snowflake-melt"
),
"filter_status": binary_sensor.binary_sensor_schema(
FilterStatusSensor, device_class="problem", icon="mdi:air-filter"
),
CONF_ISEE_STATUS: binary_sensor.binary_sensor_schema(
ISeeStatusSensor, icon="mdi:eye"
),
"preheat": binary_sensor.binary_sensor_schema(
PreheatSensor, icon="mdi:heating-coil"
),
"standby": binary_sensor.binary_sensor_schema(
StandbySensor, icon="mdi:pause-circle-outline"
),
}
)

CONFIG_SCHEMA = sensors_to_config_schema(SENSORS)


@coroutine
async def to_code(config):
await sensors_to_code(config, SENSORS, binary_sensor.register_binary_sensor)
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include "esphome/components/binary_sensor/binary_sensor.h"
#include "../mitp_listener.h"

namespace esphome {
namespace mitsubishi_itp {

class MITPBinarySensor : public MITPListener, public binary_sensor::BinarySensor {
public:
void publish() override {
if (mitp_binary_sensor_state_.has_value())
// Binary sensors automatically dedup publishes (I think) and so will only actually publish on change
publish_state(mitp_binary_sensor_state_.value());
}

protected:
optional<bool> mitp_binary_sensor_state_;
};

class DefrostSensor : public MITPBinarySensor {
void process_packet(const RunStateGetResponsePacket &packet) { mitp_binary_sensor_state_ = packet.in_defrost(); }
};
class FilterStatusSensor : public MITPBinarySensor {
void process_packet(const RunStateGetResponsePacket &packet) { mitp_binary_sensor_state_ = packet.service_filter(); }
};
class PreheatSensor : public MITPBinarySensor {
void process_packet(const RunStateGetResponsePacket &packet) { mitp_binary_sensor_state_ = packet.in_preheat(); }
};
class StandbySensor : public MITPBinarySensor {
void process_packet(const RunStateGetResponsePacket &packet) { mitp_binary_sensor_state_ = packet.in_standby(); }
};
class ISeeStatusSensor : public MITPBinarySensor {
void process_packet(const SettingsGetResponsePacket &packet) {
mitp_binary_sensor_state_ = packet.is_i_see_enabled();
}
};

} // namespace mitsubishi_itp
} // namespace esphome
49 changes: 49 additions & 0 deletions esphome/components/mitsubishi_itp/button/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import button
from esphome.const import (
ENTITY_CATEGORY_CONFIG,
)
from esphome.core import coroutine
from ...mitsubishi_itp import (
CONF_MITSUBISHI_ITP_ID,
mitsubishi_itp_ns,
MitsubishiUART,
)

CONF_FILTER_RESET_BUTTON = "filter_reset_button"

FilterResetButton = mitsubishi_itp_ns.class_(
"FilterResetButton", button.Button, cg.Component
)

BUTTONS = {
CONF_FILTER_RESET_BUTTON: button.button_schema(
FilterResetButton,
entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:restore",
),
}

CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_MITSUBISHI_ITP_ID): cv.use_id(MitsubishiUART),
}
).extend(
{
cv.Optional(button_designator): button_schema
for button_designator, button_schema in BUTTONS.items()
}
)


@coroutine
async def to_code(config):
muart_component = await cg.get_variable(config[CONF_MITSUBISHI_ITP_ID])

# Buttons
for button_designator, _ in BUTTONS.items():
button_conf = config[button_designator]
button_component = await button.new_button(button_conf)
await cg.register_component(button_component, button_conf)
await cg.register_parented(button_component, muart_component)
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#pragma once

#include "esphome/components/button/button.h"
#include "mitsubishi_uart.h"
#include "../mitsubishi_uart.h"

namespace esphome {
namespace mitsubishi_itp {

class MUARTButton : public button::Button, public Component, public Parented<MitsubishiUART> {
class MITPButton : public button::Button, public Component, public Parented<MitsubishiUART> {
public:
MUARTButton() = default;
MITPButton() = default;
using Parented<MitsubishiUART>::Parented;

protected:
virtual void press_action() override = 0;
};

class FilterResetButton : public MUARTButton {
class FilterResetButton : public MITPButton {
protected:
void press_action() override { this->parent_->reset_filter_status(); }
};
Expand Down
Loading
Loading