-
-
Notifications
You must be signed in to change notification settings - Fork 37.1k
Add buttons to Ring chime devices to play ding and motion chimes #71370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
balloob
merged 4 commits into
home-assistant:dev
from
grablair:ring-add-play-chime-controls
May 5, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| """This component provides HA button support for Ring Chimes.""" | ||
| import logging | ||
|
|
||
| from homeassistant.components.button import ButtonEntity | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
|
||
| from . import DOMAIN | ||
| from .entity import RingEntityMixin | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| BELL_ICON = "mdi:bell-ring" | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
| async_add_entities: AddEntitiesCallback, | ||
| ) -> None: | ||
| """Create the buttons for the Ring devices.""" | ||
| devices = hass.data[DOMAIN][config_entry.entry_id]["devices"] | ||
| buttons = [] | ||
|
|
||
| # add one button for each test chime type (ding, motion) | ||
| for device in devices["chimes"]: | ||
| buttons.append(ChimeButton(config_entry.entry_id, device, "ding")) | ||
| buttons.append(ChimeButton(config_entry.entry_id, device, "motion")) | ||
|
|
||
| async_add_entities(buttons) | ||
|
|
||
|
|
||
| class BaseRingButton(RingEntityMixin, ButtonEntity): | ||
| """Represents a Button for controlling an aspect of a ring device.""" | ||
|
|
||
| def __init__(self, config_entry_id, device, button_identifier, button_name): | ||
| """Initialize the switch.""" | ||
| super().__init__(config_entry_id, device) | ||
| self._button_identifier = button_identifier | ||
| self._button_name = button_name | ||
| self._attr_unique_id = f"{self._device.id}-{self._button_identifier}" | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Name of the device.""" | ||
| return f"{self._device.name} {self._button_name}" | ||
|
|
||
|
|
||
| class ChimeButton(BaseRingButton): | ||
| """Creates a button to play the test chime of a Chime device.""" | ||
|
|
||
| _attr_icon = BELL_ICON | ||
|
|
||
| def __init__(self, config_entry_id, device, kind): | ||
| """Initialize the button for a device with a chime.""" | ||
| super().__init__( | ||
| config_entry_id, device, f"play-chime-{kind}", f"Play chime: {kind}" | ||
| ) | ||
| self.kind = kind | ||
|
|
||
| def press(self) -> None: | ||
| """Send the test chime request.""" | ||
| if not self._device.test_sound(kind=self.kind): | ||
| _LOGGER.error("Failed to ring chime sound on %s", self.name) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| """The tests for the Ring button platform.""" | ||
|
|
||
| from homeassistant.const import Platform | ||
| from homeassistant.helpers import entity_registry as er | ||
|
|
||
| from .common import setup_platform | ||
|
|
||
|
|
||
| async def test_entity_registry(hass, requests_mock): | ||
| """Tests that the devices are registered in the entity registry.""" | ||
| await setup_platform(hass, Platform.BUTTON) | ||
| entity_registry = er.async_get(hass) | ||
|
|
||
| entry = entity_registry.async_get("button.downstairs_play_chime_ding") | ||
| assert entry.unique_id == "123456-play-chime-ding" | ||
|
|
||
| entry = entity_registry.async_get("button.downstairs_play_chime_motion") | ||
| assert entry.unique_id == "123456-play-chime-motion" | ||
|
|
||
|
|
||
| async def test_play_chime_buttons_report_correctly(hass, requests_mock): | ||
| """Tests that the initial state of a device that should be on is correct.""" | ||
| await setup_platform(hass, Platform.BUTTON) | ||
|
|
||
| state = hass.states.get("button.downstairs_play_chime_ding") | ||
| assert state.attributes.get("friendly_name") == "Downstairs Play chime: ding" | ||
| assert state.attributes.get("icon") == "mdi:bell-ring" | ||
|
|
||
| state = hass.states.get("button.downstairs_play_chime_motion") | ||
| assert state.attributes.get("friendly_name") == "Downstairs Play chime: motion" | ||
| assert state.attributes.get("icon") == "mdi:bell-ring" | ||
|
|
||
|
|
||
| async def test_chime_can_be_played(hass, requests_mock): | ||
| """Tests the play chime request is sent correctly.""" | ||
| await setup_platform(hass, Platform.BUTTON) | ||
|
|
||
| # Mocks the response for playing a test sound | ||
| requests_mock.post( | ||
| "https://api.ring.com/clients_api/chimes/123456/play_sound", | ||
| text="SUCCESS", | ||
| ) | ||
| await hass.services.async_call( | ||
| "button", | ||
| "press", | ||
| {"entity_id": "button.downstairs_play_chime_ding"}, | ||
| blocking=True, | ||
| ) | ||
|
|
||
| await hass.async_block_till_done() | ||
|
|
||
| assert requests_mock.request_history[-1].url.startswith( | ||
| "https://api.ring.com/clients_api/chimes/123456/play_sound?" | ||
| ) | ||
| assert "kind=ding" in requests_mock.request_history[-1].url |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.