Skip to content

Commit

Permalink
Implement /user/unlink handler (close #533)
Browse files Browse the repository at this point in the history
  • Loading branch information
dext0r committed May 8, 2024
1 parent 0aed409 commit ef7d3de
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
9 changes: 8 additions & 1 deletion custom_components/yandex_smart_home/entry_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,14 @@ def link_platform(self, platform: SmartHomePlatform) -> None:
return

data = self.entry.data.copy()
data.setdefault(CONF_LINKED_PLATFORMS, []).append(platform)
data[CONF_LINKED_PLATFORMS] = data.get(CONF_LINKED_PLATFORMS, []) + [platform]

self._hass.config_entries.async_update_entry(self.entry, data=data)

def unlink_platform(self, platform: SmartHomePlatform) -> None:
"""Unlink smart home platform."""
data = self.entry.data.copy()
data[CONF_LINKED_PLATFORMS] = list(self.linked_platforms - {platform})

self._hass.config_entries.async_update_entry(self.entry, data=data)

Expand Down
4 changes: 2 additions & 2 deletions custom_components/yandex_smart_home/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ async def async_devices_action(hass: HomeAssistant, data: RequestData, payload:


@HANDLERS.register("/user/unlink")
async def async_user_unlink(_hass: HomeAssistant, _data: RequestData, _payload: str) -> None:
async def async_user_unlink(_hass: HomeAssistant, data: RequestData, _payload: str) -> None:
"""Handle request indicates that the user has unlink the account.
https://yandex.ru/dev/dialogs/smart-home/doc/reference/unlink.html
"""
return None
data.entry_data.unlink_platform(data.platform)
28 changes: 28 additions & 0 deletions tests/test_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,34 @@ async def test_handler_link_platform_direct(hass_platform_direct: HomeAssistant,
await hass.async_block_till_done()


async def test_handler_user_unlink(hass_platform_direct: HomeAssistant, hass_admin_user: User) -> None:
hass = hass_platform_direct
component: YandexSmartHome = hass.data[DOMAIN]
entry_data = component.get_direct_connection_entry_data(
platform=SmartHomePlatform.YANDEX, user_id=hass_admin_user.id
)
assert entry_data is not None

await handlers.async_device_list(
hass, RequestData(entry_data, Context(), SmartHomePlatform.YANDEX, "foo", REQ_ID), ""
)
assert entry_data.linked_platforms == {SmartHomePlatform.YANDEX}
await hass.async_block_till_done()

with patch("homeassistant.config_entries.ConfigEntries.async_update_entry") as mock_update_entry:
await handlers.async_user_unlink(
hass, RequestData(entry_data, Context(), SmartHomePlatform.YANDEX, "foo", REQ_ID), ""
)
mock_update_entry.assert_called_once()

for _ in range(0, 2):
await handlers.async_user_unlink(
hass, RequestData(entry_data, Context(), SmartHomePlatform.YANDEX, "foo", REQ_ID), ""
)
assert entry_data.linked_platforms == set()
await hass.async_block_till_done()


async def test_handler_devices_action(hass, caplog):
class MockCapability(StateToggleCapability):
@property
Expand Down

0 comments on commit ef7d3de

Please sign in to comment.