Skip to content

Commit 433712b

Browse files
authored
Add Shelly binary sensor translation (home-assistant#154116)
Signed-off-by: David Rapan <[email protected]>
1 parent 5d87e0f commit 433712b

File tree

8 files changed

+322
-115
lines changed

8 files changed

+322
-115
lines changed

homeassistant/components/shelly/binary_sensor.py

Lines changed: 141 additions & 32 deletions
Large diffs are not rendered by default.

homeassistant/components/shelly/strings.json

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,80 @@
129129
}
130130
},
131131
"entity": {
132+
"binary_sensor": {
133+
"cable_unplugged": {
134+
"name": "Cable unplugged"
135+
},
136+
"cable_unplugged_with_channel_name": {
137+
"name": "{channel_name} cable unplugged"
138+
},
139+
"calibration": {
140+
"name": "Calibration"
141+
},
142+
"cloud": {
143+
"name": "Cloud"
144+
},
145+
"door": {
146+
"name": "Door"
147+
},
148+
"external_input": {
149+
"name": "External input"
150+
},
151+
"external_power": {
152+
"name": "External power"
153+
},
154+
"flood": {
155+
"name": "Flood"
156+
},
157+
"flood_with_channel_name": {
158+
"name": "{channel_name} flood"
159+
},
160+
"input": {
161+
"name": "Input"
162+
},
163+
"input_with_number": {
164+
"name": "Input {input_number}"
165+
},
166+
"mute": {
167+
"name": "Mute"
168+
},
169+
"mute_with_channel_name": {
170+
"name": "{channel_name} mute"
171+
},
172+
"occupancy_with_channel_name": {
173+
"name": "{channel_name} occupancy"
174+
},
175+
"overcurrent": {
176+
"name": "Overcurrent"
177+
},
178+
"overcurrent_with_channel_name": {
179+
"name": "{channel_name} overcurrent"
180+
},
181+
"overheating": {
182+
"name": "Overheating"
183+
},
184+
"overheating_with_channel_name": {
185+
"name": "{channel_name} overheating"
186+
},
187+
"overpowering": {
188+
"name": "Overpowering"
189+
},
190+
"overpowering_with_channel_name": {
191+
"name": "{channel_name} overpowering"
192+
},
193+
"overvoltage": {
194+
"name": "Overvoltage"
195+
},
196+
"overvoltage_with_channel_name": {
197+
"name": "{channel_name} overvoltage"
198+
},
199+
"restart_required": {
200+
"name": "Restart required"
201+
},
202+
"smoke_with_channel_name": {
203+
"name": "{channel_name} smoke"
204+
}
205+
},
132206
"button": {
133207
"calibrate": {
134208
"name": "Calibrate"

homeassistant/components/shelly/utils.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,13 @@ def get_shelly_model_name(
391391
return cast(str, MODEL_NAMES.get(model))
392392

393393

394-
def get_rpc_component_name(device: RpcDevice, key: str) -> str | None:
394+
def get_rpc_key(value: str) -> tuple[bool, str, str]:
395+
"""Get split device key."""
396+
parts = value.split(":")
397+
return len(parts) > 1, parts[0], parts[-1]
398+
399+
400+
def get_rpc_custom_name(device: RpcDevice, key: str) -> str | None:
395401
"""Get component name from device config."""
396402
if (
397403
key in device.config
@@ -403,6 +409,11 @@ def get_rpc_component_name(device: RpcDevice, key: str) -> str | None:
403409
return None
404410

405411

412+
def get_rpc_component_name(device: RpcDevice, key: str) -> str | None:
413+
"""Get component name from device config."""
414+
return get_rpc_custom_name(device, key)
415+
416+
406417
def get_rpc_channel_name(device: RpcDevice, key: str) -> str | None:
407418
"""Get name based on device and channel name."""
408419
if BLU_TRV_IDENTIFIER in key:
@@ -414,11 +425,11 @@ def get_rpc_channel_name(device: RpcDevice, key: str) -> str | None:
414425
component = key.split(":")[0]
415426
component_id = key.split(":")[-1]
416427

417-
if component_name := get_rpc_component_name(device, key):
428+
if custom_name := get_rpc_custom_name(device, key):
418429
if component in (*VIRTUAL_COMPONENTS, "input", "presencezone", "script"):
419-
return component_name
430+
return custom_name
420431

421-
return component_name if instances == 1 else None
432+
return custom_name if instances == 1 else None
422433

423434
if component in (*VIRTUAL_COMPONENTS, "input"):
424435
return f"{component.title()} {component_id}"

tests/components/shelly/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ def mock_white_light_set_state(
247247
"wifi": {"sta": {"enable": True}, "sta1": {"enable": False}},
248248
"ws": {"enable": False, "server": None},
249249
"voltmeter:100": {"xvoltage": {"unit": "ppm"}},
250+
"smoke:0": {"id": 0, "name": "test channel name"},
250251
"script:1": {"id": 1, "name": "test_script.js", "enable": True},
251252
"script:2": {"id": 2, "name": "test_script_2.js", "enable": False},
252253
"script:3": {"id": 3, "name": BLE_SCRIPT_NAME, "enable": False},
@@ -439,6 +440,7 @@ def mock_white_light_set_state(
439440
"current_C": 12.3,
440441
"output": True,
441442
},
443+
"smoke:0": {"id": 0, "alarm": False, "mute": False},
442444
"script:1": {
443445
"id": 1,
444446
"running": True,

tests/components/shelly/snapshots/test_binary_sensor.ambr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
'previous_unique_id': None,
3030
'suggested_object_id': None,
3131
'supported_features': 0,
32-
'translation_key': None,
32+
'translation_key': 'calibration',
3333
'unique_id': '123456789ABC-blutrv:200-calibration',
3434
'unit_of_measurement': None,
3535
})
@@ -78,7 +78,7 @@
7878
'previous_unique_id': None,
7979
'suggested_object_id': None,
8080
'supported_features': 0,
81-
'translation_key': None,
81+
'translation_key': 'cable_unplugged_with_channel_name',
8282
'unique_id': '123456789ABC-flood:0-flood_cable_unplugged',
8383
'unit_of_measurement': None,
8484
})
@@ -127,7 +127,7 @@
127127
'previous_unique_id': None,
128128
'suggested_object_id': None,
129129
'supported_features': 0,
130-
'translation_key': None,
130+
'translation_key': 'flood_with_channel_name',
131131
'unique_id': '123456789ABC-flood:0-flood',
132132
'unit_of_measurement': None,
133133
})
@@ -176,7 +176,7 @@
176176
'previous_unique_id': None,
177177
'suggested_object_id': None,
178178
'supported_features': 0,
179-
'translation_key': None,
179+
'translation_key': 'mute_with_channel_name',
180180
'unique_id': '123456789ABC-flood:0-mute',
181181
'unit_of_measurement': None,
182182
})

0 commit comments

Comments
 (0)