Skip to content

Commit

Permalink
Merge pull request #98 from atanasenko/move_sensors
Browse files Browse the repository at this point in the history
[NASA] Move nasa-specific sensors into python config
  • Loading branch information
lanwin authored Feb 14, 2024
2 parents 69b8612 + 7183d55 commit 3589e33
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 71 deletions.
56 changes: 32 additions & 24 deletions components/samsung_ac/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@
CONF_DEVICE_ID = "samsung_ac_device_id"
CONF_DEVICE_ADDRESS = "address"
CONF_DEVICE_ROOM_TEMPERATURE = "room_temperature"
CONF_DEVICE_ROOM_HUMIDITY = "room_humidity"
CONF_DEVICE_TARGET_TEMPERATURE = "target_temperature"
CONF_DEVICE_WATER_TEMPERATURE = "water_temperature"
CONF_DEVICE_OUTDOOR_TEMPERATURE = "outdoor_temperature"
CONF_DEVICE_POWER = "power"
CONF_DEVICE_MODE = "mode"
CONF_DEVICE_CLIMATE = "climate"
CONF_DEVICE_CUSTOM = "custom_sensor"
CONF_RAW_FILTERS = "raw_filters"
CONF_DEVICE_MESSAGE = "message"

CONF_DEVICE_ROOM_HUMIDITY = "room_humidity"
CONF_DEVICE_WATER_TEMPERATURE = "water_temperature"

CUSTOM_SENSOR_SCHEMA = sensor.sensor_schema().extend({
cv.Required(CONF_DEVICE_MESSAGE): cv.hex_int,
})
Expand Down Expand Up @@ -100,6 +102,15 @@ def temperature_sensor_schema(message: int):
],
)

def humidity_sensor_schema(message: int):
return custom_sensor_schema(
message=message,
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
)

DEVICE_SCHEMA = (
cv.Schema(
{
Expand All @@ -111,33 +122,31 @@ def temperature_sensor_schema(message: int):
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_DEVICE_WATER_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_DEVICE_OUTDOOR_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_DEVICE_ROOM_HUMIDITY): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_HUMIDITY,
state_class=STATE_CLASS_MEASUREMENT,
),

cv.Optional(CONF_DEVICE_TARGET_TEMPERATURE): NUMBER_SCHEMA,
cv.Optional(CONF_DEVICE_POWER): switch.switch_schema(Samsung_AC_Switch),
cv.Optional(CONF_DEVICE_MODE): SELECT_MODE_SCHEMA,
cv.Optional(CONF_DEVICE_CLIMATE): CLIMATE_SCHEMA,
cv.Optional(CONF_DEVICE_CUSTOM, default=[]): cv.ensure_list(CUSTOM_SENSOR_SCHEMA),

# keep CUSTOM_SENSOR_KEYS in sync with these
cv.Optional(CONF_DEVICE_WATER_TEMPERATURE): temperature_sensor_schema(0x4237),
cv.Optional(CONF_DEVICE_ROOM_HUMIDITY): humidity_sensor_schema(0x4038),
}
)
)

CUSTOM_SENSOR_KEYS = [
CONF_DEVICE_WATER_TEMPERATURE,
CONF_DEVICE_ROOM_HUMIDITY,
]

CONF_DEVICES = "devices"

CONF_DEBUG_MQTT_HOST = "debug_mqtt_host"
Expand Down Expand Up @@ -192,16 +201,6 @@ async def to_code(config):
sens = await sensor.new_sensor(conf)
cg.add(var_dev.set_outdoor_temperature_sensor(sens))

if CONF_DEVICE_WATER_TEMPERATURE in device:
conf = device[CONF_DEVICE_WATER_TEMPERATURE]
sens = await sensor.new_sensor(conf)
cg.add(var_dev.set_water_temperature_sensor(sens))

if CONF_DEVICE_ROOM_HUMIDITY in device:
conf = device[CONF_DEVICE_ROOM_HUMIDITY]
sens = await sensor.new_sensor(conf)
cg.add(var_dev.set_room_humidity_sensor(sens))

if CONF_DEVICE_TARGET_TEMPERATURE in device:
conf = device[CONF_DEVICE_TARGET_TEMPERATURE]
conf[CONF_UNIT_OF_MEASUREMENT] = UNIT_CELSIUS
Expand All @@ -228,6 +227,15 @@ async def to_code(config):
for cust_sens in device[CONF_DEVICE_CUSTOM]:
sens = await sensor.new_sensor(cust_sens)
cg.add(var_dev.add_custom_sensor(cust_sens[CONF_DEVICE_MESSAGE], sens))

for key in CUSTOM_SENSOR_KEYS:
if key in device:
conf = device[key]
# combine raw filters with any user-defined filters
conf_copy = conf.copy()
conf_copy[CONF_FILTERS] = (conf[CONF_RAW_FILTERS] if CONF_RAW_FILTERS in conf else []) + (conf[CONF_FILTERS] if CONF_FILTERS in conf else [])
sens = await sensor.new_sensor(conf_copy)
cg.add(var_dev.add_custom_sensor(conf[CONF_DEVICE_MESSAGE], sens))

cg.add(var.register_device(var_dev))

Expand Down
2 changes: 0 additions & 2 deletions components/samsung_ac/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ namespace esphome
virtual void register_address(const std::string address) = 0;
virtual void set_power(const std::string address, bool value) = 0;
virtual void set_room_temperature(const std::string address, float value) = 0;
virtual void set_water_temperature(const std::string address, float value) = 0;
virtual void set_room_humidity(const std::string address, float value) = 0;
virtual void set_target_temperature(const std::string address, float value) = 0;
virtual void set_outdoor_temperature(const std::string address, float value) = 0;
virtual void set_mode(const std::string address, Mode mode) = 0;
Expand Down
7 changes: 2 additions & 5 deletions components/samsung_ac/protocol_nasa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,6 @@ namespace esphome
{
// XML Enum no value but in Code it adds unit
ESP_LOGW(TAG, "s:%s d:%s ENUM_in_state_humidity_percent %li", source.c_str(), dest.c_str(), message.value);
target->set_room_humidity(source, message.value);
return;
}
case MessageNumber::ENUM_in_operation_power:
Expand Down Expand Up @@ -623,14 +622,12 @@ namespace esphome
}
case MessageNumber::VAR_in_temp_water_tank_f:
{
double temp = (double)message.value / (double)10;
ESP_LOGW(TAG, "s:%s d:%s VAR_in_temp_water_tank_f %f", source.c_str(), dest.c_str(), temp);
target->set_water_temperature(source, temp);
ESP_LOGW(TAG, "s:%s d:%s VAR_in_temp_water_tank_f %f", source.c_str(), dest.c_str(), message.value);
return;
}
case MessageNumber::VAR_out_sensor_airout:
{
double temp = (double)message.value / (double)10;
double temp = (double) ((int16_t)message.value) / (double)10;
ESP_LOGW(TAG, "s:%s d:%s VAR_out_sensor_airout %li", source.c_str(), dest.c_str(), message.value);
target->set_outdoor_temperature(source, temp);
return;
Expand Down
14 changes: 0 additions & 14 deletions components/samsung_ac/samsung_ac.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,6 @@ namespace esphome
dev->update_room_temperature(value);
}

void /*MessageTarget::*/ set_water_temperature(const std::string address, float value) override
{
Samsung_AC_Device *dev = find_device(address);
if (dev != nullptr)
dev->update_water_temperature(value);
}

void /*MessageTarget::*/ set_room_humidity(const std::string address, float value) override
{
Samsung_AC_Device *dev = find_device(address);
if (dev != nullptr)
dev->update_room_humidity(value);
}

void /*MessageTarget::*/ set_outdoor_temperature(const std::string address, float value) override
{
Samsung_AC_Device *dev = find_device(address);
Expand Down
29 changes: 3 additions & 26 deletions components/samsung_ac/samsung_ac_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ namespace esphome

std::string address;
sensor::Sensor *room_temperature{nullptr};
sensor::Sensor *water_temperature{nullptr};
sensor::Sensor *outdoor_temperature{nullptr};
sensor::Sensor *room_humidity{nullptr};
Samsung_AC_Number *target_temperature{nullptr};
Samsung_AC_Switch *power{nullptr};
Samsung_AC_Mode_Select *mode{nullptr};
Expand All @@ -102,15 +100,6 @@ namespace esphome
outdoor_temperature = sensor;
}

void set_water_temperature_sensor(sensor::Sensor *sensor)
{
water_temperature = sensor;
}

void set_room_humidity_sensor(sensor::Sensor *sensor)
{
room_humidity = sensor;
}

void add_custom_sensor(int message_number, sensor::Sensor *sensor)
{
Expand Down Expand Up @@ -167,12 +156,6 @@ namespace esphome
climate->device = this;
}

void update_outdoor_temperature(float value)
{
if (outdoor_temperature != nullptr)
outdoor_temperature->publish_state(value);
}

void update_target_temperature(float value)
{
if (target_temperature != nullptr)
Expand Down Expand Up @@ -274,16 +257,10 @@ namespace esphome
}
}

void update_water_temperature(float value)
{
if (water_temperature != nullptr)
water_temperature->publish_state(value);
}

void update_room_humidity(float value)
void update_outdoor_temperature(float value)
{
if (room_humidity != nullptr)
room_humidity->publish_state(value);
if (outdoor_temperature != nullptr)
outdoor_temperature->publish_state(value);
}

void update_custom_sensor(uint16_t message_number, float value)
Expand Down

0 comments on commit 3589e33

Please sign in to comment.