Refactored Code: Moved most code from main component class to entity component classes. Set traits for select entities.

This commit is contained in:
ElVit
2025-06-10 20:08:35 +02:00
parent b229ee80e6
commit f9f29a635d
25 changed files with 2075 additions and 720 deletions
@@ -146,10 +146,11 @@ CONFIG_SCHEMA = cv.Schema(
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
hub = await cg.get_variable(config[CONF_PANASONIC_HEATPUMP_ID])
for key in TYPES:
parent = await cg.get_variable(config[CONF_PANASONIC_HEATPUMP_ID])
for index, key in enumerate(TYPES):
if child_config := config.get(key):
var = await binary_sensor.new_binary_sensor(child_config)
await cg.register_component(var, child_config)
await cg.register_parented(var, config[CONF_PANASONIC_HEATPUMP_ID])
cg.add(getattr(hub, f"set_{key}_binary_sensor")(var))
cg.add(var.set_parent(parent))
cg.add(var.set_id(index))
cg.add(parent.add_binary_sensor(var))
@@ -1,5 +1,4 @@
#include "panasonic_heatpump_binary_sensor.h"
#include "esphome/core/log.h"
namespace esphome
@@ -13,5 +12,153 @@ namespace esphome
LOG_BINARY_SENSOR("", "Panasonic Heatpump Binary Sensor", this);
delay(10);
}
void PanasonicHeatpumpBinarySensor::publish_new_state(const std::vector<uint8_t>& data)
{
bool new_state;
switch (this->id_)
{
case BinarySensorIds::CONF_TOP0:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit7and8(data[4]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP2:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit1and2(data[4]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP3:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit1and2(data[7]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP13:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit1and2(data[5]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP26:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit5and6(data[111]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP60:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit7and8(data[112]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP61:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit5and6(data[112]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP68:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit5and6(data[5]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP69:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit5and6(data[117]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP99:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit5and6(data[24]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP100:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit7and8(data[24]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP108:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit3and4(data[20]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP109:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit5and6(data[20]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP110:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit7and8(data[20]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP119:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit7and8(data[23]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP120:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit5and6(data[23]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP121:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit3and4(data[23]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP122:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit1and2(data[23]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP123:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit1and2(data[116]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP124:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit3and4(data[116]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP129:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit7and8(data[26]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP132:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit3and4(data[26]));
if (this->state == new_state) return;
break;
}
case BinarySensorIds::CONF_TOP133:
{
new_state = PanasonicDecode::getBinaryState(PanasonicDecode::getBit1and2(data[26]));
if (this->state == new_state) return;
break;
}
default: return;
};
this->publish_state(new_state);
}
} // namespace panasonic_heatpump
} // namespace esphome
@@ -1,18 +1,47 @@
#pragma once
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/core/component.h"
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "../panasonic_heatpump.h"
#include "../decode.h"
namespace esphome
{
namespace panasonic_heatpump
{
class PanasonicHeatpumpBinarySensor : public binary_sensor::BinarySensor, public Component, public Parented<PanasonicHeatpumpComponent>
enum BinarySensorIds : int
{
CONF_TOP0,
CONF_TOP2,
CONF_TOP3,
CONF_TOP13,
CONF_TOP26,
CONF_TOP60,
CONF_TOP61,
CONF_TOP68,
CONF_TOP69,
CONF_TOP99,
CONF_TOP100,
CONF_TOP108,
CONF_TOP109,
CONF_TOP110,
CONF_TOP119,
CONF_TOP120,
CONF_TOP121,
CONF_TOP122,
CONF_TOP123,
CONF_TOP124,
CONF_TOP129,
CONF_TOP132,
CONF_TOP133,
};
class PanasonicHeatpumpBinarySensor : public binary_sensor::BinarySensor, public Component, public Parented<PanasonicHeatpumpComponent>, public PanasonicHeatpumpEntity
{
public:
PanasonicHeatpumpBinarySensor() = default;
void dump_config() override;
void publish_new_state(const std::vector<uint8_t>& data) override;
};
} // namespace panasonic_heatpump
} // namespace esphome