Remove duplicate code in SamsungTV (#117913)

This commit is contained in:
epenet 2024-05-22 12:29:25 +02:00 committed by GitHub
parent 9454dfc719
commit 5ee42ec780
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 16 deletions

View File

@ -12,6 +12,7 @@ from homeassistant.const import (
CONF_MODEL,
CONF_NAME,
)
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import Entity
@ -67,3 +68,19 @@ class SamsungTVEntity(CoordinatorEntity[SamsungTVDataUpdateCoordinator], Entity)
# If the ip address changed since we last saw the device
# broadcast a packet as well
send_magic_packet(self._mac)
async def _async_turn_off(self) -> None:
"""Turn the device off."""
await self._bridge.async_power_off()
await self.coordinator.async_refresh()
async def _async_turn_on(self) -> None:
"""Turn the remote on."""
if self._turn_on_action:
await self._turn_on_action.async_run(self.hass, self._context)
elif self._mac:
await self.hass.async_add_executor_job(self._wake_on_lan)
else:
raise HomeAssistantError(
f"Entity {self.entity_id} does not support this service."
)

View File

@ -311,8 +311,7 @@ class SamsungTVDevice(SamsungTVEntity, MediaPlayerEntity):
async def async_turn_off(self) -> None:
"""Turn off media player."""
await self._bridge.async_power_off()
await self.coordinator.async_refresh()
await super()._async_turn_off()
async def async_set_volume_level(self, volume: float) -> None:
"""Set volume level on the media player."""
@ -386,10 +385,7 @@ class SamsungTVDevice(SamsungTVEntity, MediaPlayerEntity):
async def async_turn_on(self) -> None:
"""Turn the media player on."""
if self._turn_on_action:
await self._turn_on_action.async_run(self.hass, self._context)
elif self._mac:
await self.hass.async_add_executor_job(self._wake_on_lan)
await super()._async_turn_on()
async def async_select_source(self, source: str) -> None:
"""Select input source."""

View File

@ -7,7 +7,6 @@ from typing import Any
from homeassistant.components.remote import ATTR_NUM_REPEATS, RemoteEntity
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import SamsungTVConfigEntry
@ -33,7 +32,7 @@ class SamsungTVRemote(SamsungTVEntity, RemoteEntity):
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
await self._bridge.async_power_off()
await super()._async_turn_off()
async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
"""Send a command to a device.
@ -53,11 +52,4 @@ class SamsungTVRemote(SamsungTVEntity, RemoteEntity):
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the remote on."""
if self._turn_on_action:
await self._turn_on_action.async_run(self.hass, self._context)
elif self._mac:
await self.hass.async_add_executor_job(self._wake_on_lan)
else:
raise HomeAssistantError(
f"Entity {self.entity_id} does not support this service."
)
await super()._async_turn_on()