From 4e396acda409d102d2b357f0d774da1b78477870 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Tue, 13 Oct 2026 11:45:30 +0300 Subject: [PATCH] Add Shelly last command source sensor (#186082) --- homeassistant/components/shelly/icons.json | 6 + homeassistant/components/shelly/sensor.py | 64 +++ homeassistant/components/shelly/strings.json | 100 ++++ homeassistant/components/shelly/utils.py | 6 + .../shelly/snapshots/test_devices.ambr | 510 ++++++++++++++++++ tests/components/shelly/test_sensor.py | 41 ++ 6 files changed, 727 insertions(+) diff --git a/homeassistant/components/shelly/icons.json b/homeassistant/components/shelly/icons.json index abba23959b83..4ce71c451c6a 100644 --- a/homeassistant/components/shelly/icons.json +++ b/homeassistant/components/shelly/icons.json @@ -41,6 +41,12 @@ "lamp_life": { "default": "mdi:progress-wrench" }, + "last_command_source": { + "default": "mdi:ray-start-arrow" + }, + "last_command_source_with_channel_name": { + "default": "mdi:ray-start-arrow" + }, "left_slot_level": { "default": "mdi:bottle-tonic-outline" }, diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index e74ba4d8a9e8..aa7e90f03d27 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -66,6 +66,7 @@ from .utils import ( get_virtual_component_unit, is_rpc_wifi_stations_disabled, is_view_for_platform, + normalize_command_source, ) PARALLEL_UPDATES = 0 @@ -1811,6 +1812,69 @@ RPC_SENSORS: Final = { entity_category=EntityCategory.DIAGNOSTIC, removal_condition=lambda _, status, key: not status[key]["present"], ), + "switch_source": RpcSensorDescription( + key="switch", + sub_key="source", + translation_key="last_command_source", + device_class=SensorDeviceClass.ENUM, + value=lambda status, _: normalize_command_source(status), + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), + "light_source": RpcSensorDescription( + key="light", + sub_key="source", + translation_key="last_command_source", + device_class=SensorDeviceClass.ENUM, + value=lambda status, _: normalize_command_source(status), + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), + "cct_source": RpcSensorDescription( + key="cct", + sub_key="source", + translation_key="last_command_source", + device_class=SensorDeviceClass.ENUM, + value=lambda status, _: normalize_command_source(status), + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), + "rgb_source": RpcSensorDescription( + key="rgb", + sub_key="source", + translation_key="last_command_source", + device_class=SensorDeviceClass.ENUM, + value=lambda status, _: normalize_command_source(status), + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), + "rgbw_source": RpcSensorDescription( + key="rgbw", + sub_key="source", + translation_key="last_command_source", + device_class=SensorDeviceClass.ENUM, + value=lambda status, _: normalize_command_source(status), + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), + "rgbcct_source": RpcSensorDescription( + key="rgbcct", + sub_key="source", + translation_key="last_command_source", + device_class=SensorDeviceClass.ENUM, + value=lambda status, _: normalize_command_source(status), + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), + "cb_source": RpcSensorDescription( + key="cb", + sub_key="source", + translation_key="last_command_source", + device_class=SensorDeviceClass.ENUM, + value=lambda status, _: normalize_command_source(status), + entity_category=EntityCategory.DIAGNOSTIC, + entity_registry_enabled_default=False, + ), } diff --git a/homeassistant/components/shelly/strings.json b/homeassistant/components/shelly/strings.json index dfab7e3edbf3..d006871cc43a 100644 --- a/homeassistant/components/shelly/strings.json +++ b/homeassistant/components/shelly/strings.json @@ -479,6 +479,106 @@ "lamp_life": { "name": "Lamp life" }, + "last_command_source": { + "name": "Last command source", + "state": { + "activate": "Input activation", + "analog": "Analog input", + "ble": "Bluetooth", + "ble_indicate": "Bluetooth indication", + "button": "Button", + "calibration": "Calibration", + "dim": "Dimming started", + "dimstop": "Dimming stopped", + "double": "Double press", + "dual_dim": "Dual dimming started", + "http": "HTTP", + "init": "Restored state", + "knx": "KNX", + "limit_switch": "Limit switch", + "local": "Manual operation", + "long_push": "Long press", + "loopback": "Internal", + "mqtt": "MQTT", + "night_mode_end_period": "Night mode ended", + "night_mode_start_full_period": "Night mode full period", + "night_mode_start_period": "Night mode started", + "night_mode_started_period": "Night mode already active", + "overcurrent": "Overcurrent", + "overpower": "Overpower", + "overtemp": "Overheating", + "overvoltage": "Overvoltage", + "overvoltage_clear": "Overvoltage cleared", + "profile_change": "Profile change", + "reboot": "Reboot", + "rpc": "RPC", + "rpc_set": "RPC", + "safety_switch": "Safety switch", + "shc": "Shelly Cloud", + "short_push": "Short press", + "stuck": "Stuck", + "switch": "Switch input", + "timeout": "Timeout", + "timer": "Timer", + "transition": "Transition", + "ui": "Device controls", + "undervoltage": "Undervoltage", + "undervoltage_clear": "Undervoltage cleared", + "unknown": "Unknown", + "unsupported_load": "Unsupported load", + "ws_in": "WebSocket" + } + }, + "last_command_source_with_channel_name": { + "name": "{channel_name} last command source", + "state": { + "activate": "[%key:component::shelly::entity::sensor::last_command_source::state::activate%]", + "analog": "[%key:component::shelly::entity::sensor::last_command_source::state::analog%]", + "ble": "[%key:component::shelly::entity::sensor::last_command_source::state::ble%]", + "ble_indicate": "[%key:component::shelly::entity::sensor::last_command_source::state::ble_indicate%]", + "button": "[%key:component::shelly::entity::sensor::last_command_source::state::button%]", + "calibration": "[%key:component::shelly::entity::sensor::last_command_source::state::calibration%]", + "dim": "[%key:component::shelly::entity::sensor::last_command_source::state::dim%]", + "dimstop": "[%key:component::shelly::entity::sensor::last_command_source::state::dimstop%]", + "double": "[%key:component::shelly::entity::sensor::last_command_source::state::double%]", + "dual_dim": "[%key:component::shelly::entity::sensor::last_command_source::state::dual_dim%]", + "http": "[%key:component::shelly::entity::sensor::last_command_source::state::http%]", + "init": "[%key:component::shelly::entity::sensor::last_command_source::state::init%]", + "knx": "[%key:component::shelly::entity::sensor::last_command_source::state::knx%]", + "limit_switch": "[%key:component::shelly::entity::sensor::last_command_source::state::limit_switch%]", + "local": "[%key:component::shelly::entity::sensor::last_command_source::state::local%]", + "long_push": "[%key:component::shelly::entity::sensor::last_command_source::state::long_push%]", + "loopback": "[%key:component::shelly::entity::sensor::last_command_source::state::loopback%]", + "mqtt": "[%key:component::shelly::entity::sensor::last_command_source::state::mqtt%]", + "night_mode_end_period": "[%key:component::shelly::entity::sensor::last_command_source::state::night_mode_end_period%]", + "night_mode_start_full_period": "[%key:component::shelly::entity::sensor::last_command_source::state::night_mode_start_full_period%]", + "night_mode_start_period": "[%key:component::shelly::entity::sensor::last_command_source::state::night_mode_start_period%]", + "night_mode_started_period": "[%key:component::shelly::entity::sensor::last_command_source::state::night_mode_started_period%]", + "overcurrent": "[%key:component::shelly::entity::sensor::last_command_source::state::overcurrent%]", + "overpower": "[%key:component::shelly::entity::sensor::last_command_source::state::overpower%]", + "overtemp": "[%key:component::shelly::entity::sensor::last_command_source::state::overtemp%]", + "overvoltage": "[%key:component::shelly::entity::sensor::last_command_source::state::overvoltage%]", + "overvoltage_clear": "[%key:component::shelly::entity::sensor::last_command_source::state::overvoltage_clear%]", + "profile_change": "[%key:component::shelly::entity::sensor::last_command_source::state::profile_change%]", + "reboot": "[%key:component::shelly::entity::sensor::last_command_source::state::reboot%]", + "rpc": "[%key:component::shelly::entity::sensor::last_command_source::state::rpc%]", + "rpc_set": "[%key:component::shelly::entity::sensor::last_command_source::state::rpc_set%]", + "safety_switch": "[%key:component::shelly::entity::sensor::last_command_source::state::safety_switch%]", + "shc": "[%key:component::shelly::entity::sensor::last_command_source::state::shc%]", + "short_push": "[%key:component::shelly::entity::sensor::last_command_source::state::short_push%]", + "stuck": "[%key:component::shelly::entity::sensor::last_command_source::state::stuck%]", + "switch": "[%key:component::shelly::entity::sensor::last_command_source::state::switch%]", + "timeout": "[%key:component::shelly::entity::sensor::last_command_source::state::timeout%]", + "timer": "[%key:component::shelly::entity::sensor::last_command_source::state::timer%]", + "transition": "[%key:component::shelly::entity::sensor::last_command_source::state::transition%]", + "ui": "[%key:component::shelly::entity::sensor::last_command_source::state::ui%]", + "undervoltage": "[%key:component::shelly::entity::sensor::last_command_source::state::undervoltage%]", + "undervoltage_clear": "[%key:component::shelly::entity::sensor::last_command_source::state::undervoltage_clear%]", + "unknown": "[%key:component::shelly::entity::sensor::last_command_source::state::unknown%]", + "unsupported_load": "[%key:component::shelly::entity::sensor::last_command_source::state::unsupported_load%]", + "ws_in": "[%key:component::shelly::entity::sensor::last_command_source::state::ws_in%]" + } + }, "last_seen": { "name": "Last seen" }, diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index f1e8ffd99cc3..30f8209f62f2 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -1169,3 +1169,9 @@ async def async_manage_coiot_issues_task( }, data={"entry_id": entry.entry_id}, ) + + +def normalize_command_source(source: str | None) -> str | None: + """Normalize the last command source to match translation keys.""" + # Sources mix case (e.g. "WS_in"); Wall Display also uses spaces ("RPC Set") + return source and source.lower().replace(" ", "_") diff --git a/tests/components/shelly/snapshots/test_devices.ambr b/tests/components/shelly/snapshots/test_devices.ambr index ee5f8c8648dd..cbdb4086d8cd 100644 --- a/tests/components/shelly/snapshots/test_devices.ambr +++ b/tests/components/shelly/snapshots/test_devices.ambr @@ -1415,6 +1415,57 @@ 'state': '0.200589', }) # --- +# name: test_device[duo_bulb_gen3][sensor.test_name_living_room_lamp_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_name_living_room_lamp_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Living room lamp last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Living room lamp last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source_with_channel_name', + 'unique_id': '123456789ABC-cct:0-cct_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_device[duo_bulb_gen3][sensor.test_name_living_room_lamp_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Test name Living room lamp last command source', + }), + 'context': , + 'entity_id': 'sensor.test_name_living_room_lamp_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'transition', + }) +# --- # name: test_device[duo_bulb_gen3][sensor.test_name_living_room_lamp_power-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -3032,6 +3083,57 @@ 'state': '50.0', }) # --- +# name: test_device[power_strip_gen4][sensor.switch_1_name_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.switch_1_name_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-switch:1-switch_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_device[power_strip_gen4][sensor.switch_1_name_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Switch 1 name Last command source', + }), + 'context': , + 'entity_id': 'sensor.switch_1_name_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'ws_in', + }) +# --- # name: test_device[power_strip_gen4][sensor.switch_1_name_power-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -3447,6 +3549,57 @@ 'state': '50.0', }) # --- +# name: test_device[power_strip_gen4][sensor.switch_3_name_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.switch_3_name_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-switch:3-switch_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_device[power_strip_gen4][sensor.switch_3_name_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Switch 3 name Last command source', + }), + 'context': , + 'entity_id': 'sensor.switch_3_name_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'init', + }) +# --- # name: test_device[power_strip_gen4][sensor.switch_3_name_power-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -3862,6 +4015,57 @@ 'state': '50.0', }) # --- +# name: test_device[power_strip_gen4][sensor.test_name_output_0_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_name_output_0_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-switch:0-switch_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_device[power_strip_gen4][sensor.test_name_output_0_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Test name Output 0 Last command source', + }), + 'context': , + 'entity_id': 'sensor.test_name_output_0_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'init', + }) +# --- # name: test_device[power_strip_gen4][sensor.test_name_output_0_power-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -4277,6 +4481,57 @@ 'state': '50.0', }) # --- +# name: test_device[power_strip_gen4][sensor.test_name_output_2_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_name_output_2_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-switch:2-switch_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_device[power_strip_gen4][sensor.test_name_output_2_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Test name Output 2 Last command source', + }), + 'context': , + 'entity_id': 'sensor.test_name_output_2_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'ws_in', + }) +# --- # name: test_device[power_strip_gen4][sensor.test_name_output_2_power-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -5792,6 +6047,57 @@ 'state': 'unknown', }) # --- +# name: test_device[pro1cb_gen2][sensor.test_name_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_name_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-cb:0-cb_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_device[pro1cb_gen2][sensor.test_name_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Test name Last command source', + }), + 'context': , + 'entity_id': 'sensor.test_name_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'init', + }) +# --- # name: test_device[pro1cb_gen2][sensor.test_name_signal_strength-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -6893,6 +7199,57 @@ 'state': 'twilight', }) # --- +# name: test_device[wall_display_xl][sensor.test_name_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_name_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-switch:0-switch_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_device[wall_display_xl][sensor.test_name_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Test name Last command source', + }), + 'context': , + 'entity_id': 'sensor.test_name_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'rpc_set', + }) +# --- # name: test_device[wall_display_xl][sensor.test_name_signal_strength-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -7865,6 +8222,57 @@ 'state': '50.0', }) # --- +# name: test_shelly_2pm_gen3_cover[sensor.test_name_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_name_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-cover:0-switch_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_shelly_2pm_gen3_cover[sensor.test_name_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Test name Last command source', + }), + 'context': , + 'entity_id': 'sensor.test_name_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'init', + }) +# --- # name: test_shelly_2pm_gen3_cover[sensor.test_name_power-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -9233,6 +9641,57 @@ 'state': '50.0', }) # --- +# name: test_shelly_2pm_gen3_no_relay_names[sensor.test_name_output_0_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_name_output_0_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-switch:0-switch_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_shelly_2pm_gen3_no_relay_names[sensor.test_name_output_0_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Test name Output 0 Last command source', + }), + 'context': , + 'entity_id': 'sensor.test_name_output_0_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'init', + }) +# --- # name: test_shelly_2pm_gen3_no_relay_names[sensor.test_name_output_0_power-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ @@ -9706,6 +10165,57 @@ 'state': '50.0', }) # --- +# name: test_shelly_2pm_gen3_no_relay_names[sensor.test_name_output_1_last_command_source-entry] + EntityRegistryEntrySnapshot({ + 'aliases': list([ + None, + ]), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.test_name_output_1_last_command_source', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Last command source', + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Last command source', + 'platform': 'shelly', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'last_command_source', + 'unique_id': '123456789ABC-switch:1-switch_source', + 'unit_of_measurement': None, + }) +# --- +# name: test_shelly_2pm_gen3_no_relay_names[sensor.test_name_output_1_last_command_source-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + : 'enum', + : 'Test name Output 1 Last command source', + }), + 'context': , + 'entity_id': 'sensor.test_name_output_1_last_command_source', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'init', + }) +# --- # name: test_shelly_2pm_gen3_no_relay_names[sensor.test_name_output_1_power-entry] EntityRegistryEntrySnapshot({ 'aliases': list([ diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index fa96a56256f6..cb09dc96309e 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -2518,3 +2518,44 @@ async def test_rpc_storage_fs_free_sensor_removal( await hass.async_block_till_done() assert entity_registry.async_get(entity_id) is None + + +@pytest.mark.usefixtures("entity_registry_enabled_by_default") +@pytest.mark.parametrize( + ("source", "expected_state"), + [ + pytest.param("init", "init", id="lowercase"), + pytest.param("WS_in", "ws_in", id="mixed_case"), + pytest.param("RPC Set", "rpc_set", id="with_space"), + pytest.param("New Source", "new_source", id="unknown_source"), + ], +) +@pytest.mark.parametrize( + "component", ["switch", "light", "cct", "rgb", "rgbw", "rgbcct", "cb"] +) +async def test_rpc_last_command_source_sensor( + hass: HomeAssistant, + mock_rpc_device: Mock, + entity_registry: EntityRegistry, + monkeypatch: pytest.MonkeyPatch, + source: str, + expected_state: str, + component: str, +) -> None: + """Test RPC last command source sensor.""" + status = {f"{component}:0": {"id": 0, "output": True, "source": source}} + monkeypatch.setattr(mock_rpc_device, "status", status) + + config = {f"{component}:0": {"id": 0}} + monkeypatch.setattr(mock_rpc_device, "config", config) + + entity_id = f"{SENSOR_DOMAIN}.test_name_last_command_source" + await init_integration(hass, 2) + + assert (state := hass.states.get(entity_id)) + assert state.state == expected_state + assert state.attributes[ATTR_DEVICE_CLASS] == SensorDeviceClass.ENUM + + assert (entry := entity_registry.async_get(entity_id)) + assert entry.unique_id == f"123456789ABC-{component}:0-{component}_source" + assert entry.entity_category == EntityCategory.DIAGNOSTIC