91 lines
2.9 KiB
Python
91 lines
2.9 KiB
Python
import logging
|
|
|
|
from pybravia8 import BraviaClient, BraviaError
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
class BraviaRemote:
|
|
def __init__(self, host, psk):
|
|
self.client = BraviaClient(host)
|
|
self.psk = psk
|
|
self.connected = False
|
|
|
|
async def connect(self):
|
|
if not self.connected:
|
|
try:
|
|
await self.client.connect(psk=self.psk)
|
|
self.connected = True
|
|
except BraviaError as e:
|
|
_LOGGER.error(f"Error connecting to Bravia TV: {e}")
|
|
|
|
async def disconnect(self):
|
|
await self.client.disconnect()
|
|
self.connected = False
|
|
|
|
async def send_command(self, cmd):
|
|
await self.connect()
|
|
try:
|
|
_LOGGER.info(f"Sending {cmd}")
|
|
rc = await self.client.send_command(cmd)
|
|
_LOGGER.info(f"send command '{cmd}' returned: {rc}")
|
|
except BraviaError as e:
|
|
_LOGGER.error(f"Error sending command '{cmd}': {e}")
|
|
|
|
async def turn_on(self):
|
|
await self.disconnect()
|
|
await self.connect()
|
|
try:
|
|
_LOGGER.info(f"Turning TV on")
|
|
rc = await self.client.turn_on()
|
|
_LOGGER.info(f"turn_on() returned: {rc}")
|
|
except BraviaError as e:
|
|
_LOGGER.error(f"Error turning TV on: {e}")
|
|
|
|
async def turn_off(self):
|
|
await self.connect()
|
|
try:
|
|
_LOGGER.info(f"Turning TV off")
|
|
rc = await self.client.turn_off()
|
|
_LOGGER.info(f"turn_off() returned: {rc}")
|
|
await self.disconnect()
|
|
except BraviaError as e:
|
|
_LOGGER.error(f"Error turning TV off: {e}")
|
|
|
|
async def volume_up(self):
|
|
await self.connect()
|
|
try:
|
|
_LOGGER.info(f"Increasing volume")
|
|
rc = await self.client.volume_up()
|
|
_LOGGER.info(f"volume_up() returned: {rc}")
|
|
except BraviaError as e:
|
|
_LOGGER.error(f"Error increasing volume: {e}")
|
|
|
|
async def volume_down(self):
|
|
await self.connect()
|
|
try:
|
|
_LOGGER.info(f"Decreasing volume")
|
|
rc = await self.client.volume_down()
|
|
_LOGGER.info(f"volume_down() returned: {rc}")
|
|
except BraviaError as e:
|
|
_LOGGER.error(f"Error decreasing volume: {e}")
|
|
|
|
async def mute(self, mute: bool = None):
|
|
await self.connect()
|
|
try:
|
|
_LOGGER.info(f"Toggling mute")
|
|
rc = await self.client.volume_mute(mute)
|
|
_LOGGER.info(f"mute() returned: {rc}")
|
|
except BraviaError as e:
|
|
_LOGGER.error(f"Error toggling mute: {e}")
|
|
|
|
async def dump_commands(self):
|
|
await self.connect()
|
|
cmd_list = await self.client.get_command_list()
|
|
commands = ", ".join(cmd_list.keys())
|
|
_LOGGER.info(f"Available commands: \n{commands}")
|
|
|
|
async def dump_info(self):
|
|
await self.connect()
|
|
info = await self.client.get_system_info()
|
|
_LOGGER.info(f"System info: \n{info}")
|