Skip to content

Commit cc5ab2b

Browse files
committed
Remove magic numbers, Add custom command set option
1 parent 232d5eb commit cc5ab2b

File tree

13 files changed

+107
-41
lines changed

13 files changed

+107
-41
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ A example configuration can be found [here](example.yaml)
2222
- **power_pin**(**Required**, [Pin](https://esphome.io/guides/configuration-types.html#config-pin)): Pin to which the MOSFET/Transistor is connected. This pin is used to temporarily turn of the display unit.
2323
- **invert_power_pin**(**Optional**: boolean): If set to `true` the output of the power pin will be inverted. Defaults to `false`.
2424
- **power_trip_delay**(**Optional**: Time): Determines the length of the power outage applied to the display unit, which is to trick it into turning on. Defaults to `500ms`.
25+
- **model**(**Optional**: int): Different models or revisions may use different commands. This option can be used to specify the command set used by this component. Select one of `EP2220`. Defaults to `EP2220`.
2526

2627
## Philips Power switch
2728

components/philips_series_2200/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
INVERT_POWER_PIN = "invert_power_pin"
1414
POWER_TRIP_DELAY = "power_trip_delay"
1515

16+
CONF_COMMAND_SET = "model"
17+
COMMAND_SETS = {"EP_2220": "PHILIPS_EP2220"}
18+
1619
philips_series_2200_ns = cg.esphome_ns.namespace("philips_series_2200")
1720
PhilipsSeries2200 = philips_series_2200_ns.class_("PhilipsSeries2200", cg.Component)
1821

@@ -30,11 +33,17 @@
3033
max_included=cv.TimePeriod(milliseconds=10000),
3134
),
3235
),
36+
cv.Optional(CONF_COMMAND_SET, default="EP_2220"): cv.enum(
37+
COMMAND_SETS, upper=True, space="_"
38+
),
3339
}
3440
).extend(cv.COMPONENT_SCHEMA)
3541

3642

3743
def to_code(config):
44+
# Use user-specified command set, default to EP_2200
45+
cg.add_define(config[CONF_COMMAND_SET])
46+
3847
var = cg.new_Pvariable(config[CONF_ID])
3948
yield cg.register_component(var, config)
4049

components/philips_series_2200/button/action_button.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace esphome
6161
// Coffee
6262
if (action == SELECT_COFFEE || action == MAKE_COFFEE)
6363
{
64-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x08, 0x00, 0x00, 0x39, 0x1C});
64+
write_array(command_press_coffee);
6565
if (action == SELECT_COFFEE)
6666
return;
6767

@@ -72,7 +72,7 @@ namespace esphome
7272
// Espresso
7373
if (action == SELECT_ESPRESSO || action == MAKE_ESPRESSO)
7474
{
75-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x09, 0x2D});
75+
write_array(command_press_espresso);
7676
if (action == SELECT_ESPRESSO)
7777
return;
7878
delay(BUTTON_SEQUENCE_DELAY);
@@ -82,7 +82,7 @@ namespace esphome
8282
// Hot water
8383
if (action == SELECT_HOT_WATER || action == MAKE_HOT_WATER)
8484
{
85-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x04, 0x00, 0x00, 0x21, 0x01});
85+
write_array(command_press_hot_water);
8686
if (action == SELECT_HOT_WATER)
8787
return;
8888
delay(BUTTON_SEQUENCE_DELAY);
@@ -92,7 +92,7 @@ namespace esphome
9292
// Steam
9393
if (action == SELECT_STEAM || action == MAKE_STEAM)
9494
{
95-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x10, 0x00, 0x00, 0x09, 0x26});
95+
write_array(command_press_steam);
9696
if (action == SELECT_STEAM)
9797
return;
9898
delay(BUTTON_SEQUENCE_DELAY);
@@ -101,19 +101,19 @@ namespace esphome
101101

102102
// press/play or subsequent press/play
103103
if (action == PLAY_PAUSE)
104-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x01, 0x19, 0x32});
104+
write_array(command_press_play_pause);
105105
else if (action == SELECT_BEAN)
106106
// bean button
107-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x09, 0x2F});
107+
write_array(command_press_bean);
108108
else if (action == SELECT_SIZE)
109109
// size button
110-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x04, 0x00, 0x20, 0x05});
110+
write_array(command_press_size);
111111
else if (action == SELECT_AQUA_CLEAN)
112112
// aqua clean button
113-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x10, 0x00, 0x0D, 0x36});
113+
write_array(command_press_aqua_clean);
114114
else if (action == SELECT_CALC_CLEAN)
115115
// calc clean button
116-
write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x20, 0x00, 0x28, 0x37});
116+
write_array(command_press_calc_clean);
117117
else
118118
ESP_LOGE(TAG, "Invalid Action provided!");
119119
}

components/philips_series_2200/button/action_button.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "esphome/core/component.h"
44
#include "esphome/components/button/button.h"
55
#include "esphome/components/uart/uart.h"
6+
#include "../commands.h"
67

78
#define MESSAGE_REPETITIONS 5
89
#define BUTTON_SEQUENCE_DELAY 100
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
#include <stdint.h>
3+
#include <vector>
4+
5+
namespace esphome
6+
{
7+
namespace philips_series_2200
8+
{
9+
#if defined(PHILIPS_EP2220)
10+
#define USE_DEFAULT_PHILIPS_COMMAND_SET
11+
#else
12+
#define USE_DEFAULT_PHILIPS_COMMAND_SET
13+
#endif
14+
15+
#ifdef USE_DEFAULT_PHILIPS_COMMAND_SET
16+
const uint8_t message_header[2] = {0xD5, 0x55};
17+
const uint8_t led_off = 0x00;
18+
const uint8_t led_half = 0x03;
19+
const uint8_t led_on = 0x07;
20+
const uint8_t led_second = 0x38;
21+
const uint8_t led_third = 0x3F;
22+
23+
const std::vector<uint8_t> command_pre_power_on =
24+
{0xD5, 0x55, 0x0A, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0E, 0x12};
25+
const std::vector<uint8_t> command_power_with_cleaning =
26+
{0xD5, 0x55, 0x02, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x38, 0x15};
27+
const std::vector<uint8_t> command_power_without_cleaning =
28+
{0xD5, 0x55, 0x01, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x25, 0x27};
29+
const std::vector<uint8_t> command_power_off =
30+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x01, 0x00, 0x00, 0x1D, 0x3B};
31+
const std::vector<uint8_t> command_press_play_pause =
32+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x01, 0x19, 0x32};
33+
const std::vector<uint8_t> command_press_coffee =
34+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x08, 0x00, 0x00, 0x39, 0x1C};
35+
const std::vector<uint8_t> command_press_espresso =
36+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x02, 0x00, 0x00, 0x09, 0x2D};
37+
const std::vector<uint8_t> command_press_hot_water =
38+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x04, 0x00, 0x00, 0x21, 0x01};
39+
const std::vector<uint8_t> command_press_steam =
40+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x10, 0x00, 0x00, 0x09, 0x26};
41+
const std::vector<uint8_t> command_press_bean =
42+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x09, 0x2F};
43+
const std::vector<uint8_t> command_press_size =
44+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x04, 0x00, 0x20, 0x05};
45+
const std::vector<uint8_t> command_press_aqua_clean =
46+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x10, 0x00, 0x0D, 0x36};
47+
const std::vector<uint8_t> command_press_calc_clean =
48+
{0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x20, 0x00, 0x28, 0x37};
49+
#endif
50+
} // namespace philips_series_2200
51+
} // namespace esphome

components/philips_series_2200/number/beverage_setting.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace esphome
2626
void BeverageSetting::update_status(uint8_t *data, size_t len)
2727
{
2828
// reject invalid messages
29-
if (len < 19 && data[0] != 0xD5 && data[1] != 0x55)
29+
if (len < 19 && data[0] != message_header[0] && data[1] != message_header[1])
3030
return;
3131

3232
// only apply status if source is currently selected
@@ -44,17 +44,17 @@ namespace esphome
4444
((source_ == CAPPUCCINO || source_ == ANY) &&
4545
status_sensor_->get_raw_state().compare("Cappuccino selected") == 0)))
4646
{
47-
if (data[is_bean_ ? 9 : 11] == 0x07)
47+
if (data[is_bean_ ? 9 : 11] == led_on)
4848
{
4949
switch (data[is_bean_ ? 8 : 10])
5050
{
51-
case 0x00:
51+
case led_off:
5252
update_state(1);
5353
break;
54-
case 0x38:
54+
case led_second:
5555
update_state(2);
5656
break;
57-
case 0x3F:
57+
case led_third:
5858
update_state(3);
5959
break;
6060
default:
@@ -66,9 +66,9 @@ namespace esphome
6666
{
6767
for (unsigned int i = 0; i <= MESSAGE_REPETITIONS; i++)
6868
if (is_bean_)
69-
mainboard_uart_->write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x02, 0x00, 0x09, 0x2F});
69+
mainboard_uart_->write_array(command_press_bean);
7070
else
71-
mainboard_uart_->write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x00, 0x04, 0x00, 0x20, 0x05});
71+
mainboard_uart_->write_array(command_press_size);
7272
mainboard_uart_->flush();
7373
last_transmission_ = millis();
7474
}

components/philips_series_2200/number/beverage_setting.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "esphome/components/number/number.h"
55
#include "esphome/components/uart/uart.h"
66
#include "../text_sensor/status_sensor.h"
7+
#include "../commands.h"
78

89
#define MESSAGE_REPETITIONS 5
910
#define SETTINGS_BUTTON_SEQUENCE_DELAY 500

components/philips_series_2200/philips_series_2200.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace esphome
5050
while (mainboard_uart_.available())
5151
{
5252
uint8_t buffer = mainboard_uart_.peek();
53-
if (buffer == 0xD5)
53+
if (buffer == message_header[0])
5454
break;
5555
display_uart_.write(mainboard_uart_.read());
5656
}
@@ -64,7 +64,7 @@ namespace esphome
6464
display_uart_.write_array(buffer, size);
6565

6666
// Only process messages starting with start bytes
67-
if (size > 1 && buffer[0] == 0xD5 && buffer[1] == 0x55)
67+
if (size > 1 && buffer[0] == message_header[0] && buffer[1] == message_header[1])
6868
{
6969
last_message_from_mainboard_time_ = millis();
7070

components/philips_series_2200/philips_series_2200.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "esphome/core/component.h"
44
#include "esphome/components/uart/uart.h"
5+
#include "commands.h"
56
#ifdef USE_SWITCH
67
#include "switch/power.h"
78
#endif

components/philips_series_2200/switch/power.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,20 @@ namespace esphome
4141
{
4242
// Send pre-power on message
4343
for (unsigned int i = 0; i <= MESSAGE_REPETITIONS; i++)
44-
mainboard_uart_->write_array({0xD5, 0x55, 0x0A, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x0E, 0x12});
44+
mainboard_uart_->write_array(command_pre_power_on);
4545

4646
// Send power on message
4747
if (cleaning_)
4848
{
4949
// Send power on command with cleaning
5050
for (unsigned int i = 0; i <= MESSAGE_REPETITIONS; i++)
51-
mainboard_uart_->write_array({0xD5, 0x55, 0x02, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x38, 0x15});
51+
mainboard_uart_->write_array(command_power_with_cleaning);
5252
}
5353
else
5454
{
5555
// Send power on command without cleaning
5656
for (unsigned int i = 0; i <= MESSAGE_REPETITIONS; i++)
57-
mainboard_uart_->write_array({0xD5, 0x55, 0x01, 0x01, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x25, 0x27});
57+
mainboard_uart_->write_array(command_power_without_cleaning);
5858
}
5959

6060
mainboard_uart_->flush();
@@ -67,7 +67,7 @@ namespace esphome
6767
{
6868
// Send power off message
6969
for (unsigned int i = 0; i <= MESSAGE_REPETITIONS; i++)
70-
mainboard_uart_->write_array({0xD5, 0x55, 0x00, 0x01, 0x02, 0x00, 0x02, 0x01, 0x00, 0x00, 0x1D, 0x3B});
70+
mainboard_uart_->write_array(command_power_off);
7171
mainboard_uart_->flush();
7272
}
7373

0 commit comments

Comments
 (0)