mirror of
https://github.com/esphome/esphome.git
synced 2025-09-02 03:12:20 +01:00
Add device class support to text sensor (#6202)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
58
tests/component_tests/text_sensor/test_text_sensor.py
Normal file
58
tests/component_tests/text_sensor/test_text_sensor.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""Tests for the text sensor component."""
|
||||
|
||||
|
||||
def test_text_sensor_is_setup(generate_main):
|
||||
"""
|
||||
When the text is set in the yaml file, it should be registered in main
|
||||
"""
|
||||
# Given
|
||||
|
||||
# When
|
||||
main_cpp = generate_main("tests/component_tests/text_sensor/test_text_sensor.yaml")
|
||||
|
||||
# Then
|
||||
assert "new template_::TemplateTextSensor();" in main_cpp
|
||||
assert "App.register_text_sensor" in main_cpp
|
||||
|
||||
|
||||
def test_text_sensor_sets_mandatory_fields(generate_main):
|
||||
"""
|
||||
When the mandatory fields are set in the yaml, they should be set in main
|
||||
"""
|
||||
# Given
|
||||
|
||||
# When
|
||||
main_cpp = generate_main("tests/component_tests/text_sensor/test_text_sensor.yaml")
|
||||
|
||||
# Then
|
||||
assert 'ts_1->set_name("Template Text Sensor 1");' in main_cpp
|
||||
assert 'ts_2->set_name("Template Text Sensor 2");' in main_cpp
|
||||
assert 'ts_3->set_name("Template Text Sensor 3");' in main_cpp
|
||||
|
||||
|
||||
def test_text_sensor_config_value_internal_set(generate_main):
|
||||
"""
|
||||
Test that the "internal" config value is correctly set
|
||||
"""
|
||||
# Given
|
||||
|
||||
# When
|
||||
main_cpp = generate_main("tests/component_tests/text_sensor/test_text_sensor.yaml")
|
||||
|
||||
# Then
|
||||
assert "ts_2->set_internal(true);" in main_cpp
|
||||
assert "ts_3->set_internal(false);" in main_cpp
|
||||
|
||||
|
||||
def test_text_sensor_device_class_set(generate_main):
|
||||
"""
|
||||
When the device_class of text_sensor is set in the yaml file, it should be registered in main
|
||||
"""
|
||||
# Given
|
||||
|
||||
# When
|
||||
main_cpp = generate_main("tests/component_tests/text_sensor/test_text_sensor.yaml")
|
||||
|
||||
# Then
|
||||
assert 'ts_2->set_device_class("timestamp");' in main_cpp
|
||||
assert 'ts_3->set_device_class("date");' in main_cpp
|
26
tests/component_tests/text_sensor/test_text_sensor.yaml
Normal file
26
tests/component_tests/text_sensor/test_text_sensor.yaml
Normal file
@@ -0,0 +1,26 @@
|
||||
---
|
||||
esphome:
|
||||
name: test
|
||||
platform: ESP8266
|
||||
board: d1_mini_lite
|
||||
|
||||
text_sensor:
|
||||
- platform: template
|
||||
id: ts_1
|
||||
name: "Template Text Sensor 1"
|
||||
lambda: |-
|
||||
return {"Hello World"};
|
||||
- platform: template
|
||||
id: ts_2
|
||||
name: "Template Text Sensor 2"
|
||||
lambda: |-
|
||||
return {"2023-06-22T18:43:52+00:00"};
|
||||
device_class: timestamp
|
||||
internal: true
|
||||
- platform: template
|
||||
id: ts_3
|
||||
name: "Template Text Sensor 3"
|
||||
lambda: |-
|
||||
return {"2023-06-22T18:43:52+00:00"};
|
||||
device_class: date
|
||||
internal: false
|
@@ -3923,6 +3923,10 @@ text_sensor:
|
||||
- platform: template
|
||||
name: Template Text Sensor
|
||||
id: ${textname}_text
|
||||
- platform: template
|
||||
name: Template Text Sensor Timestamp
|
||||
id: ${textname}_text_timestamp
|
||||
device_class: timestamp
|
||||
- platform: wifi_info
|
||||
scan_results:
|
||||
name: Scan Results
|
||||
|
Reference in New Issue
Block a user