88async def test_sensor_init_numeric (hass ):
99 """Test sensor initialization with numeric state."""
1010 sensor = NMEA2000Sensor (
11- id = "test_sensor" ,
11+ sensor_id = "test_sensor" ,
1212 friendly_name = "Temperature" ,
1313 initial_state = 25.5 ,
1414 unit_of_measurement = "°C" ,
@@ -23,7 +23,7 @@ async def test_sensor_init_numeric(hass):
2323async def test_sensor_init_string (hass ):
2424 """Test sensor initialization with string state."""
2525 sensor = NMEA2000Sensor (
26- id = "test_state" ,
26+ sensor_id = "test_state" ,
2727 friendly_name = "Status" ,
2828 initial_state = "Running" ,
2929 device_name = "Test Device" ,
@@ -35,7 +35,7 @@ async def test_sensor_init_string(hass):
3535async def test_sensor_init_none_unavailable (hass ):
3636 """Test sensor with None initial state is unavailable."""
3737 sensor = NMEA2000Sensor (
38- id = "test_null" ,
38+ sensor_id = "test_null" ,
3939 friendly_name = "Empty" ,
4040 initial_state = None ,
4141 device_name = "Test Device" ,
@@ -46,7 +46,7 @@ async def test_sensor_init_none_unavailable(hass):
4646async def test_sensor_str_repr (hass ):
4747 """Test sensor string representation."""
4848 sensor = NMEA2000Sensor (
49- id = "test_repr" ,
49+ sensor_id = "test_repr" ,
5050 friendly_name = "Wind Speed" ,
5151 initial_state = 12.3 ,
5252 unit_of_measurement = "kts" ,
@@ -60,7 +60,7 @@ async def test_sensor_str_repr(hass):
6060async def test_sensor_set_state_not_ready (hass ):
6161 """Test set_state does nothing when sensor is not ready."""
6262 sensor = NMEA2000Sensor (
63- id = "test_not_ready" ,
63+ sensor_id = "test_not_ready" ,
6464 friendly_name = "Test" ,
6565 initial_state = 0 ,
6666 device_name = "Device" ,
@@ -73,7 +73,7 @@ async def test_sensor_set_state_not_ready(hass):
7373async def test_sensor_set_state_ready (hass ):
7474 """Test set_state updates value when sensor is ready."""
7575 sensor = NMEA2000Sensor (
76- id = "test_ready" ,
76+ sensor_id = "test_ready" ,
7777 friendly_name = "Test" ,
7878 initial_state = 0 ,
7979 device_name = "Device" ,
@@ -87,7 +87,7 @@ async def test_sensor_set_state_ready(hass):
8787async def test_sensor_update_availability_not_ready (hass ):
8888 """Test update_availability does nothing when not ready."""
8989 sensor = NMEA2000Sensor (
90- id = "test_avail" ,
90+ sensor_id = "test_avail" ,
9191 friendly_name = "Test" ,
9292 initial_state = 0 ,
9393 device_name = "Device" ,
@@ -99,7 +99,7 @@ async def test_sensor_update_availability_not_ready(hass):
9999async def test_sensor_via_device (hass ):
100100 """Test sensor with via_device creates proper device info."""
101101 sensor = NMEA2000Sensor (
102- id = "test_via" ,
102+ sensor_id = "test_via" ,
103103 friendly_name = "Test" ,
104104 initial_state = 0 ,
105105 device_name = "SubDevice" ,
@@ -112,7 +112,7 @@ async def test_sensor_custom_update_frequency(hass):
112112 """Test sensor with custom update frequency."""
113113 freq = timedelta (seconds = 10 )
114114 sensor = NMEA2000Sensor (
115- id = "test_freq" ,
115+ sensor_id = "test_freq" ,
116116 friendly_name = "Test" ,
117117 initial_state = 0 ,
118118 device_name = "Device" ,
@@ -125,11 +125,36 @@ async def test_sensor_custom_ttl(hass):
125125 """Test sensor with custom TTL."""
126126 ttl = timedelta (seconds = 30 )
127127 sensor = NMEA2000Sensor (
128- id = "test_ttl" ,
128+ sensor_id = "test_ttl" ,
129129 friendly_name = "Test" ,
130130 initial_state = 0 ,
131131 device_name = "Device" ,
132132 ttl = ttl ,
133133 )
134134 # TTL is multiplied by UNAVAILABLE_FACTOR (10)
135135 assert sensor .ttl == ttl * 10
136+
137+
138+ async def test_sensor_hyphenated_id_sanitized (hass ):
139+ """Test that hyphens in sensor_id are replaced with underscores in unique_id."""
140+ sensor = NMEA2000Sensor (
141+ sensor_id = "yden-02_126993_heartbeat" ,
142+ friendly_name = "Heartbeat" ,
143+ initial_state = 0 ,
144+ device_name = "Test Device" ,
145+ )
146+ assert "-" not in sensor ._attr_unique_id
147+ assert sensor ._attr_unique_id == "yden_02_126993_heartbeat"
148+
149+
150+ async def test_sensor_multiple_special_chars_sanitized (hass ):
151+ """Test that both spaces and hyphens are sanitized in unique_id."""
152+ sensor = NMEA2000Sensor (
153+ sensor_id = "MY-DEVICE name-test" ,
154+ friendly_name = "Test" ,
155+ initial_state = 0 ,
156+ device_name = "Test Device" ,
157+ )
158+ assert "-" not in sensor ._attr_unique_id
159+ assert " " not in sensor ._attr_unique_id
160+ assert sensor ._attr_unique_id == "my_device_name_test"
0 commit comments