Skip to content

Commit

Permalink
Add impersonation support to send_packet service (#201)
Browse files Browse the repository at this point in the history
* Add impersonation support to send_packet service

* Fix device_id in test_svc_send_packet_with_impersonation test

* Process review comments

* Fix typo in schemas.py

* Fix ruff issue
  • Loading branch information
Kars-de-Jong authored Jan 8, 2025
1 parent b085d18 commit 832ed0b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ __snapshots__

!custom_components/ramses_cc
!custom_components/ramses_cc/manifest.json
!custom_components/ramses_cc/services.yaml

!.flake8
!.gitignore
Expand Down
3 changes: 2 additions & 1 deletion custom_components/ramses_cc/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ def schema_is_minimal(schema: _SchemaT) -> bool:

SCH_SEND_PACKET = vol.Schema(
{
vol.Required(ATTR_DEVICE_ID): cv.matches_regex(r"^[0-9]{2}:[0-9]{6}$"),
vol.Required(ATTR_DEVICE_ID): _SCH_DEVICE_ID,
vol.Optional("from_id"): _SCH_DEVICE_ID,
vol.Required("verb"): vol.In((" I", "I", "RQ", "RP", " W", "W")),
vol.Required("code"): cv.matches_regex(r"^[0-9A-F]{4}$"),
vol.Required("payload"): cv.matches_regex(r"^([0-9A-F][0-9A-F]){1,48}$"),
Expand Down
8 changes: 8 additions & 0 deletions custom_components/ramses_cc/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ send_packet:
example: 01:123456
required: true

from_id:
description: >-
The source device ID (a RAMSES ID, not an entity_id).
This can be used to send a packet from a faked device.
Optional: if not specified, the device ID of the gateway is used.
example: 18:123456
required: false

verb:
description: 'The packet verb, one of: I, RQ, RP, W (leading space not required).'
example: RQ
Expand Down
2 changes: 1 addition & 1 deletion custom_components/ramses_cc/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
"advanced_features": {
"title": "Advanced features",
"data": {
"send_packet": "Enable send_packet service for broadcasting bespoke packets",
"send_packet": "Enable send_packet service for casting bespoke packets",
"message_events": "Emit events for messages matching regular expression"
},
"data_description": {
Expand Down
18 changes: 18 additions & 0 deletions tests/tests_old/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"03:123456": {"class": "THM", "faked": True},
"32:097710": {"class": "CO2"},
"32:139773": {"class": "HUM"},
"37:123456": {"class": "FAN"},
"40:123456": {"class": "REM", "faked": True},
},
}
Expand Down Expand Up @@ -793,3 +794,20 @@ async def test_svc_send_packet(hass: HomeAssistant, entry: ConfigEntry) -> None:
schemas = {SVC_SEND_PACKET: SCH_SEND_PACKET}

await _test_service_call(hass, SVC_SEND_PACKET, data, schemas=schemas)


async def test_svc_send_packet_with_impersonation(
hass: HomeAssistant, entry: ConfigEntry
) -> None:
"""Test the service call."""

data = {
"device_id": "37:123456",
"from_id": "40:123456",
"verb": " I",
"code": "22F1",
"payload": "000304",
}
schemas = {SVC_SEND_PACKET: SCH_SEND_PACKET}

await _test_service_call(hass, SVC_SEND_PACKET, data, schemas=schemas)

0 comments on commit 832ed0b

Please sign in to comment.