Merge branch 'dev' into optolink

This commit is contained in:
j0ta29 2023-05-15 08:48:16 +02:00 committed by GitHub
commit 2de5fda972
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 41 deletions

View file

@ -22,14 +22,14 @@ void I2SAudioMediaPlayer::control(const media_player::MediaPlayerCall &call) {
this->start(); this->start();
} }
} }
if (this->i2s_state_ != I2S_STATE_RUNNING) {
return;
}
if (call.get_volume().has_value()) { if (call.get_volume().has_value()) {
this->volume = call.get_volume().value(); this->volume = call.get_volume().value();
this->set_volume_(volume); this->set_volume_(volume);
this->unmute_(); this->unmute_();
} }
if (this->i2s_state_ != I2S_STATE_RUNNING) {
return;
}
if (call.get_command().has_value()) { if (call.get_command().has_value()) {
switch (call.get_command().value()) { switch (call.get_command().value()) {
case media_player::MEDIA_PLAYER_COMMAND_PLAY: case media_player::MEDIA_PLAYER_COMMAND_PLAY:
@ -97,6 +97,7 @@ void I2SAudioMediaPlayer::unmute_() {
this->muted_ = false; this->muted_ = false;
} }
void I2SAudioMediaPlayer::set_volume_(float volume, bool publish) { void I2SAudioMediaPlayer::set_volume_(float volume, bool publish) {
if (this->audio_ != nullptr)
this->audio_->setVolume(remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 21)); this->audio_->setVolume(remap<uint8_t, float>(volume, 0.0f, 1.0f, 0, 21));
if (publish) if (publish)
this->volume = volume; this->volume = volume;
@ -157,13 +158,23 @@ void I2SAudioMediaPlayer::start_() {
#endif #endif
this->i2s_state_ = I2S_STATE_RUNNING; this->i2s_state_ = I2S_STATE_RUNNING;
this->high_freq_.start(); this->high_freq_.start();
this->audio_->setVolume(remap<uint8_t, float>(this->volume, 0.0f, 1.0f, 0, 21));
if (this->current_url_.has_value()) { if (this->current_url_.has_value()) {
this->audio_->connecttohost(this->current_url_.value().c_str()); this->audio_->connecttohost(this->current_url_.value().c_str());
this->state = media_player::MEDIA_PLAYER_STATE_PLAYING; this->state = media_player::MEDIA_PLAYER_STATE_PLAYING;
this->publish_state(); this->publish_state();
} }
} }
void I2SAudioMediaPlayer::stop() { this->i2s_state_ = I2S_STATE_STOPPING; } void I2SAudioMediaPlayer::stop() {
if (this->i2s_state_ == I2S_STATE_STOPPED) {
return;
}
if (this->i2s_state_ == I2S_STATE_STARTING) {
this->i2s_state_ = I2S_STATE_STOPPED;
return;
}
this->i2s_state_ = I2S_STATE_STOPPING;
}
void I2SAudioMediaPlayer::stop_() { void I2SAudioMediaPlayer::stop_() {
if (this->audio_->isRunning()) { if (this->audio_->isRunning()) {
this->audio_->stopSong(); this->audio_->stopSong();

View file

@ -89,6 +89,10 @@ void I2SAudioMicrophone::start_() {
void I2SAudioMicrophone::stop() { void I2SAudioMicrophone::stop() {
if (this->state_ == microphone::STATE_STOPPED || this->is_failed()) if (this->state_ == microphone::STATE_STOPPED || this->is_failed())
return; return;
if (this->state_ == microphone::STATE_STARTING) {
this->state_ = microphone::STATE_STOPPED;
return;
}
this->state_ = microphone::STATE_STOPPING; this->state_ = microphone::STATE_STOPPING;
} }

View file

@ -136,6 +136,10 @@ void I2SAudioSpeaker::player_task(void *params) {
void I2SAudioSpeaker::stop() { void I2SAudioSpeaker::stop() {
if (this->state_ == speaker::STATE_STOPPED) if (this->state_ == speaker::STATE_STOPPED)
return; return;
if (this->state_ == speaker::STATE_STARTING) {
this->state_ = speaker::STATE_STOPPED;
return;
}
this->state_ = speaker::STATE_STOPPING; this->state_ = speaker::STATE_STOPPING;
DataEvent data; DataEvent data;
data.stop = true; data.stop = true;

View file

@ -57,37 +57,43 @@ void TuyaLight::setup() {
return; return;
} }
float red, green, blue;
switch (*this->color_type_) { switch (*this->color_type_) {
case TuyaColorType::RGBHSV: case TuyaColorType::RGBHSV:
case TuyaColorType::RGB: { case TuyaColorType::RGB: {
auto red = parse_hex<uint8_t>(datapoint.value_string.substr(0, 2)); auto rgb = parse_hex<uint32_t>(datapoint.value_string.substr(0, 6));
auto green = parse_hex<uint8_t>(datapoint.value_string.substr(2, 2)); if (!rgb.has_value())
auto blue = parse_hex<uint8_t>(datapoint.value_string.substr(4, 2)); return;
if (red.has_value() && green.has_value() && blue.has_value()) {
auto rgb_call = this->state_->make_call(); red = (*rgb >> 16) / 255.0f;
rgb_call.set_rgb(float(*red) / 255, float(*green) / 255, float(*blue) / 255); green = ((*rgb >> 8) & 0xff) / 255.0f;
rgb_call.perform(); blue = (*rgb & 0xff) / 255.0f;
}
break; break;
} }
case TuyaColorType::HSV: { case TuyaColorType::HSV: {
auto hue = parse_hex<uint16_t>(datapoint.value_string.substr(0, 4)); auto hue = parse_hex<uint16_t>(datapoint.value_string.substr(0, 4));
auto saturation = parse_hex<uint16_t>(datapoint.value_string.substr(4, 4)); auto saturation = parse_hex<uint16_t>(datapoint.value_string.substr(4, 4));
auto value = parse_hex<uint16_t>(datapoint.value_string.substr(8, 4)); auto value = parse_hex<uint16_t>(datapoint.value_string.substr(8, 4));
if (hue.has_value() && saturation.has_value() && value.has_value()) { if (!hue.has_value() || !saturation.has_value() || !value.has_value())
float red, green, blue; return;
hsv_to_rgb(*hue, float(*saturation) / 1000, float(*value) / 1000, red, green, blue); hsv_to_rgb(*hue, float(*saturation) / 1000, float(*value) / 1000, red, green, blue);
auto rgb_call = this->state_->make_call();
rgb_call.set_rgb(red, green, blue);
rgb_call.perform();
}
break; break;
} }
} }
float current_red, current_green, current_blue;
this->state_->current_values_as_rgb(&current_red, &current_green, &current_blue);
if (red == current_red && green == current_green && blue == current_blue)
return;
auto rgb_call = this->state_->make_call();
rgb_call.set_rgb(red, green, blue);
rgb_call.perform();
}); });
} }
if (min_value_datapoint_id_.has_value()) { if (min_value_datapoint_id_.has_value()) {
parent_->set_integer_datapoint_value(*this->min_value_datapoint_id_, this->min_value_); this->parent_->set_integer_datapoint_value(*this->min_value_datapoint_id_, this->min_value_);
} }
} }
@ -156,7 +162,7 @@ void TuyaLight::write_state(light::LightState *state) {
} }
if (!state->current_values.is_on() && this->switch_id_.has_value()) { if (!state->current_values.is_on() && this->switch_id_.has_value()) {
parent_->set_boolean_datapoint_value(*this->switch_id_, false); this->parent_->set_boolean_datapoint_value(*this->switch_id_, false);
return; return;
} }
@ -166,14 +172,14 @@ void TuyaLight::write_state(light::LightState *state) {
if (this->color_temperature_invert_) { if (this->color_temperature_invert_) {
color_temp_int = this->color_temperature_max_value_ - color_temp_int; color_temp_int = this->color_temperature_max_value_ - color_temp_int;
} }
parent_->set_integer_datapoint_value(*this->color_temperature_id_, color_temp_int); this->parent_->set_integer_datapoint_value(*this->color_temperature_id_, color_temp_int);
} }
if (this->dimmer_id_.has_value()) { if (this->dimmer_id_.has_value()) {
auto brightness_int = static_cast<uint32_t>(brightness * this->max_value_); auto brightness_int = static_cast<uint32_t>(brightness * this->max_value_);
brightness_int = std::max(brightness_int, this->min_value_); brightness_int = std::max(brightness_int, this->min_value_);
parent_->set_integer_datapoint_value(*this->dimmer_id_, brightness_int); this->parent_->set_integer_datapoint_value(*this->dimmer_id_, brightness_int);
} }
} }
@ -210,7 +216,7 @@ void TuyaLight::write_state(light::LightState *state) {
} }
if (this->switch_id_.has_value()) { if (this->switch_id_.has_value()) {
parent_->set_boolean_datapoint_value(*this->switch_id_, true); this->parent_->set_boolean_datapoint_value(*this->switch_id_, true);
} }
} }

View file

@ -37,9 +37,7 @@ def test_button_config_value_internal_set(generate_main):
# Given # Given
# When # When
main_cpp = generate_main( main_cpp = generate_main("tests/component_tests/button/test_button.yaml")
"tests/component_tests/button/test_button.yaml"
)
# Then # Then
assert "wol_1->set_internal(true);" in main_cpp assert "wol_1->set_internal(true);" in main_cpp

View file

@ -5,9 +5,7 @@ def test_deep_sleep_setup(generate_main):
""" """
When the deep sleep is set in the yaml file, it should be registered in main When the deep sleep is set in the yaml file, it should be registered in main
""" """
main_cpp = generate_main( main_cpp = generate_main("tests/component_tests/deep_sleep/test_deep_sleep1.yaml")
"tests/component_tests/deep_sleep/test_deep_sleep1.yaml"
)
assert "deepsleep = new deep_sleep::DeepSleepComponent();" in main_cpp assert "deepsleep = new deep_sleep::DeepSleepComponent();" in main_cpp
assert "App.register_component(deepsleep);" in main_cpp assert "App.register_component(deepsleep);" in main_cpp
@ -17,9 +15,7 @@ def test_deep_sleep_sleep_duration(generate_main):
""" """
When deep sleep is configured with sleep duration, it should be set. When deep sleep is configured with sleep duration, it should be set.
""" """
main_cpp = generate_main( main_cpp = generate_main("tests/component_tests/deep_sleep/test_deep_sleep1.yaml")
"tests/component_tests/deep_sleep/test_deep_sleep1.yaml"
)
assert "deepsleep->set_sleep_duration(60000);" in main_cpp assert "deepsleep->set_sleep_duration(60000);" in main_cpp
@ -28,9 +24,7 @@ def test_deep_sleep_run_duration_simple(generate_main):
""" """
When deep sleep is configured with run duration, it should be set. When deep sleep is configured with run duration, it should be set.
""" """
main_cpp = generate_main( main_cpp = generate_main("tests/component_tests/deep_sleep/test_deep_sleep1.yaml")
"tests/component_tests/deep_sleep/test_deep_sleep1.yaml"
)
assert "deepsleep->set_run_duration(10000);" in main_cpp assert "deepsleep->set_run_duration(10000);" in main_cpp
@ -39,9 +33,7 @@ def test_deep_sleep_run_duration_dictionary(generate_main):
""" """
When deep sleep is configured with dictionary run duration, it should be set. When deep sleep is configured with dictionary run duration, it should be set.
""" """
main_cpp = generate_main( main_cpp = generate_main("tests/component_tests/deep_sleep/test_deep_sleep2.yaml")
"tests/component_tests/deep_sleep/test_deep_sleep2.yaml"
)
assert ( assert (
"deepsleep->set_run_duration(deep_sleep::WakeupCauseToRunDuration{\n" "deepsleep->set_run_duration(deep_sleep::WakeupCauseToRunDuration{\n"

View file

@ -66,7 +66,16 @@ def test_string_string__invalid(value):
config_validation.string_strict(value) config_validation.string_strict(value)
@given(builds(lambda v: "mdi:" + v, text(alphabet=string.ascii_letters + string.digits + "-_", min_size=1, max_size=20))) @given(
builds(
lambda v: "mdi:" + v,
text(
alphabet=string.ascii_letters + string.digits + "-_",
min_size=1,
max_size=20,
),
)
)
@example("") @example("")
def test_icon__valid(value): def test_icon__valid(value):
actual = config_validation.icon(value) actual = config_validation.icon(value)