Skip to content
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

Replace custom actions for sleep timer with buttons in bluesound integration #133604

Open
wants to merge 21 commits into
base: dev
Choose a base branch
from

Conversation

LouisChrist
Copy link
Contributor

@LouisChrist LouisChrist commented Dec 19, 2024

Proposed change

This replaces the custom actions bluesound.set_sleep_timer and bluesound.clear_sleep_timer with two corresponding buttons.

The actions are deprecated, but still there. The version I defined for deprecation is a guess. I am not sure what the policies regarding deprecation are.

The newly added buttons are disabled by default because I think they are more of an edge case.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request: Link

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @thrawnarn, mind taking a look at this pull request as it has been labeled with an integration (bluesound) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of bluesound can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign bluesound Removes the current integration label and assignees on the pull request, add the integration domain after the command.
  • @home-assistant add-label needs-more-information Add a label (needs-more-information, problem in dependency, problem in custom component) to the pull request.
  • @home-assistant remove-label needs-more-information Remove a label (needs-more-information, problem in dependency, problem in custom component) on the pull request.

@LouisChrist LouisChrist changed the title Replace custom actions for sleep timer with button in bluesound integration Replace custom actions for sleep timer with buttons in bluesound integration Dec 19, 2024
@LouisChrist LouisChrist force-pushed the bluesound_buttons_for_sleep_timer branch from c0e68d0 to 912529b Compare January 8, 2025 18:58
Copy link
Member

@joostlek joostlek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's split adding the buttons from moving to a coordinator into separate PRs

@home-assistant home-assistant bot marked this pull request as draft January 8, 2025 19:01
@home-assistant
Copy link

home-assistant bot commented Jan 8, 2025

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@LouisChrist
Copy link
Contributor Author

I have created a new PR #135125 for the data update coordinator. I will update this one after the other one has been merged.

@LouisChrist LouisChrist marked this pull request as ready for review January 8, 2025 21:31
@home-assistant home-assistant bot requested a review from joostlek January 8, 2025 21:31
config_entry.data[CONF_PORT],
),
],
update_before_add=True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
update_before_add=True,

They don't have state

)


class SetSleepTimerButton(CoordinatorEntity[BluesoundCoordinator], ButtonEntity):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it maybe make sense to create a base entity that will be inherited in both the media player and the buttons? It would help deduplicate the device info logic

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would reommend doing this in a separate PR

Comment on lines 93 to 120
async def async_press(self) -> None:
"""Set the sleep timer."""
await self._player.sleep_timer()


class ClearSleepTimerButton(CoordinatorEntity[BluesoundCoordinator], ButtonEntity):
"""Representation of a sleep timer button."""

def __init__(
self, coordinator: BluesoundCoordinator, player: Player, port: int
) -> None:
"""Initialize the Bluesound button."""
super().__init__(coordinator)
sync_status = coordinator.data.sync_status

self._player = player
self._attr_unique_id = (
f"clear-sleep-timer-{format_unique_id(sync_status.mac, port)}"
)
self._attr_name = f"{sync_status.name} Clear Sleep Timer"
self._attr_device_info = generate_device_info(sync_status, port)
self._attr_entity_registry_enabled_default = False

async def async_press(self) -> None:
"""Clear the sleep timer."""
sleep = -1
while sleep != 0:
sleep = await self._player.sleep_timer()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having 2 classes, create a generic class that takes in an ButtonEntityDescription, which you can extend for Bluesound to add a press_fn which you execute when someone presses the button. This way we can deduplicate even less

Comment on lines 171 to 199
@pytest.fixture
async def setup_config_entry_buttons_enabled(
hass: HomeAssistant,
config_entry: MockConfigEntry,
player_mocks: PlayerMocks,
entity_registry: er.EntityRegistry,
) -> None:
"""Set up the platform."""
player_name = player_mocks.player_data.sync_status_long_polling_mock.get().name

entity_registry.async_get_or_create(
domain=BUTTON_DOMAIN,
platform=DOMAIN,
unique_id=f"set-sleep-timer-{config_entry.unique_id}",
suggested_object_id=f"{player_name}_set_sleep_timer",
disabled_by=None,
)

entity_registry.async_get_or_create(
domain=BUTTON_DOMAIN,
platform=DOMAIN,
unique_id=f"clear-sleep-timer-{config_entry.unique_id}",
suggested_object_id=f"{player_name}_clear_sleep_timer",
disabled_by=None,
)

config_entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have a fixture for this

@pytest.mark.usefixtures("entity_registry_enabled_by_default")

@home-assistant home-assistant bot marked this pull request as draft January 9, 2025 09:50
@LouisChrist LouisChrist marked this pull request as ready for review January 9, 2025 16:22
@home-assistant home-assistant bot requested a review from joostlek January 9, 2025 16:22
Comment on lines 38 to 48
buttons = [
BluesoundButton(
config_entry.runtime_data.coordinator,
config_entry.runtime_data.player,
config_entry.data[CONF_PORT],
description,
)
for description in BUTTON_DESCRIPTIONS
]

async_add_entities(buttons)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
buttons = [
BluesoundButton(
config_entry.runtime_data.coordinator,
config_entry.runtime_data.player,
config_entry.data[CONF_PORT],
description,
)
for description in BUTTON_DESCRIPTIONS
]
async_add_entities(buttons)
async_add_entities(
BluesoundButton(
config_entry.runtime_data.coordinator,
config_entry.runtime_data.player,
config_entry.data[CONF_PORT],
description,
)
for description in BUTTON_DESCRIPTIONS
)

Comment on lines 65 to 75
async def set_sleep_timer(player: Player) -> None:
"""Set the sleep timer."""
await player.sleep_timer()


BUTTON_DESCRIPTIONS = [
BluesoundButtonEntityDescription(
key="set_sleep_timer",
name="Set Sleep Timer",
press_fn=set_sleep_timer,
),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
async def set_sleep_timer(player: Player) -> None:
"""Set the sleep timer."""
await player.sleep_timer()
BUTTON_DESCRIPTIONS = [
BluesoundButtonEntityDescription(
key="set_sleep_timer",
name="Set Sleep Timer",
press_fn=set_sleep_timer,
),
BUTTON_DESCRIPTIONS = [
BluesoundButtonEntityDescription(
key="set_sleep_timer",
name="Set Sleep Timer",
press_fn=lambda player: player.sleep_timer,
),

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The typing is not working out. press_fn returns None, but player.sleep_timer returns an int.

),
BluesoundButtonEntityDescription(
key="clear_sleep_timer",
name="Clear Sleep Timer",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use entity translations for this. Assign a translation_key and make a reference in strings.json.

(You can also use that translation key to add a nice icon in icons.json)


self.entity_description = description
self._player = player
self._attr_entity_registry_enabled_default = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's set the enabled default in the entity descriptions

self._player = player
self._attr_entity_registry_enabled_default = False
self._attr_unique_id = (
f"{self.entity_description.key}-{format_unique_id(sync_status.mac, port)}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f"{self.entity_description.key}-{format_unique_id(sync_status.mac, port)}"
f"{description.key}-{format_unique_id(sync_status.mac, port)}"

self._attr_unique_id = (
f"{self.entity_description.key}-{format_unique_id(sync_status.mac, port)}"
)
self._attr_name = f"{sync_status.name} {description.name}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set _attr_has_entity_name = True. and if you do the translation thing theres nothing more you have to do

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have set has_entity_name in the EntityDescription

self.hass,
DOMAIN,
f"deprecated_service_{SERVICE_SET_TIMER}",
breaks_in_ha_version="2025.7.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
breaks_in_ha_version="2025.7.0",
breaks_in_ha_version="2025.8.0",

self.hass,
DOMAIN,
f"deprecated_service_{SERVICE_CLEAR_TIMER}",
breaks_in_ha_version="2025.7.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
breaks_in_ha_version="2025.7.0",
breaks_in_ha_version="2025.8.0",

@home-assistant home-assistant bot marked this pull request as draft January 10, 2025 12:18
@LouisChrist LouisChrist marked this pull request as ready for review January 10, 2025 15:44
@home-assistant home-assistant bot requested a review from joostlek January 10, 2025 15:44
@LouisChrist LouisChrist force-pushed the bluesound_buttons_for_sleep_timer branch from 72b1972 to f0eadf3 Compare January 10, 2025 15:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants