『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 6283|回复: 7

[求助] Sony Bravia TV的一些问题

[复制链接]

16

主题

324

帖子

2274

积分

金牌会员

Rank: 6Rank: 6

积分
2274
金钱
1950
HASS币
0
发表于 2018-6-13 08:44:28 | 显示全部楼层 |阅读模式
官方支持Sony Bravia TV的接入和控制,官方的教程很简单就是输入
media_player:
  - platform: braviatv
         name: braviatv
         host: 192.168.2.7
后就能有这样的一个界面,可以开关机切换HDMI等操作,也有开关机状态的反馈
QQ图片20180613083816.png QQ图片20180613084000.png
QQ图片20180613083859.png
但是我不想用他这个界面,或者想单独利用里面的几个功能写到自动化等其他功能里,该输入什么命令,比如我只想用他的开关机和开关机状态反馈以及HDMI切换,那我应该怎么敲码?
这是官方的PY文件
"""
Support for interface with a Sony Bravia TV.

For more details about this platform, please refer to the documentation at
[url]https://home-assistant.io/components/media_player.braviatv/[/url]
"""
import logging
import re

import voluptuous as vol

from homeassistant.components.media_player import (
    SUPPORT_NEXT_TRACK, SUPPORT_PAUSE, SUPPORT_PREVIOUS_TRACK, SUPPORT_TURN_ON,
    SUPPORT_TURN_OFF, SUPPORT_VOLUME_MUTE, SUPPORT_VOLUME_STEP, SUPPORT_PLAY,
    SUPPORT_VOLUME_SET, SUPPORT_SELECT_SOURCE, MediaPlayerDevice,
    PLATFORM_SCHEMA)
from homeassistant.const import (CONF_HOST, CONF_NAME, STATE_OFF, STATE_ON)
import homeassistant.helpers.config_validation as cv
from homeassistant.util.json import load_json, save_json

REQUIREMENTS = [
    'https://github.com/aparraga/braviarc/archive/0.3.7.zip'
    '#braviarc==0.3.7']

BRAVIA_CONFIG_FILE = 'bravia.conf'

CLIENTID_PREFIX = 'HomeAssistant'

DEFAULT_NAME = 'Sony Bravia TV'

NICKNAME = 'Home Assistant'

# Map ip to request id for configuring
_CONFIGURING = {}

_LOGGER = logging.getLogger(__name__)

SUPPORT_BRAVIA = SUPPORT_PAUSE | SUPPORT_VOLUME_STEP | \
                 SUPPORT_VOLUME_MUTE | SUPPORT_VOLUME_SET | \
                 SUPPORT_PREVIOUS_TRACK | SUPPORT_NEXT_TRACK | \
                 SUPPORT_TURN_ON | SUPPORT_TURN_OFF | \
                 SUPPORT_SELECT_SOURCE | SUPPORT_PLAY

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
    vol.Required(CONF_HOST): cv.string,
    vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
})


def _get_mac_address(ip_address):
    """Get the MAC address of the device."""
    from subprocess import Popen, PIPE

    pid = Popen(["arp", "-n", ip_address], stdout=PIPE)
    pid_component = pid.communicate()[0]
    match = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})".encode('UTF-8'),
                      pid_component)
    if match is not None:
        return match.groups()[0]
    return None


# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
    """Set up the Sony Bravia TV platform."""
    host = config.get(CONF_HOST)

    if host is None:
        return

    pin = None
    bravia_config = load_json(hass.config.path(BRAVIA_CONFIG_FILE))
    while bravia_config:
        # Set up a configured TV
        host_ip, host_config = bravia_config.popitem()
        if host_ip == host:
            pin = host_config['pin']
            mac = host_config['mac']
            name = config.get(CONF_NAME)
            add_devices([BraviaTVDevice(host, mac, name, pin)])
            return

    setup_bravia(config, pin, hass, add_devices)


def setup_bravia(config, pin, hass, add_devices):
    """Set up a Sony Bravia TV based on host parameter."""
    host = config.get(CONF_HOST)
    name = config.get(CONF_NAME)

    if pin is None:
        request_configuration(config, hass, add_devices)
        return
    else:
        mac = _get_mac_address(host)
        if mac is not None:
            mac = mac.decode('utf8')
        # If we came here and configuring this host, mark as done
        if host in _CONFIGURING:
            request_id = _CONFIGURING.pop(host)
            configurator = hass.components.configurator
            configurator.request_done(request_id)
            _LOGGER.info("Discovery configuration done")

        # Save config
        save_json(
            hass.config.path(BRAVIA_CONFIG_FILE),
            {host: {'pin': pin, 'host': host, 'mac': mac}})

        add_devices([BraviaTVDevice(host, mac, name, pin)])


def request_configuration(config, hass, add_devices):
    """Request configuration steps from the user."""
    host = config.get(CONF_HOST)
    name = config.get(CONF_NAME)

    configurator = hass.components.configurator

    # We got an error if this method is called while we are configuring
    if host in _CONFIGURING:
        configurator.notify_errors(
            _CONFIGURING[host], "Failed to register, please try again.")
        return

    def bravia_configuration_callback(data):
        """Handle the entry of user PIN."""
        from braviarc import braviarc

        pin = data.get('pin')
        braviarc = braviarc.BraviaRC(host)
        braviarc.connect(pin, CLIENTID_PREFIX, NICKNAME)
        if braviarc.is_connected():
            setup_bravia(config, pin, hass, add_devices)
        else:
            request_configuration(config, hass, add_devices)

    _CONFIGURING[host] = configurator.request_config(
        name, bravia_configuration_callback,
        description='Enter the Pin shown on your Sony Bravia TV.' +
        'If no Pin is shown, enter 0000 to let TV show you a Pin.',
        description_image="/static/images/smart-tv.png",
        submit_caption="Confirm",
        fields=[{'id': 'pin', 'name': 'Enter the pin', 'type': ''}]
    )


class BraviaTVDevice(MediaPlayerDevice):
    """Representation of a Sony Bravia TV."""

    def __init__(self, host, mac, name, pin):
        """Initialize the Sony Bravia device."""
        from braviarc import braviarc

        self._pin = pin
        self._braviarc = braviarc.BraviaRC(host, mac)
        self._name = name
        self._state = STATE_OFF
        self._muted = False
        self._program_name = None
        self._channel_name = None
        self._channel_number = None
        self._source = None
        self._source_list = []
        self._original_content_list = []
        self._content_mapping = {}
        self._duration = None
        self._content_uri = None
        self._id = None
        self._playing = False
        self._start_date_time = None
        self._program_media_type = None
        self._min_volume = None
        self._max_volume = None
        self._volume = None

        self._braviarc.connect(pin, CLIENTID_PREFIX, NICKNAME)
        if self._braviarc.is_connected():
            self.update()
        else:
            self._state = STATE_OFF

    def update(self):
        """Update TV info."""
        if not self._braviarc.is_connected():
            if self._braviarc.get_power_status() != 'off':
                self._braviarc.connect(self._pin, CLIENTID_PREFIX, NICKNAME)
            if not self._braviarc.is_connected():
                return

        # Retrieve the latest data.
        try:
            if self._state == STATE_ON:
                # refresh volume info:
                self._refresh_volume()
                self._refresh_channels()

            power_status = self._braviarc.get_power_status()
            if power_status == 'active':
                self._state = STATE_ON
                playing_info = self._braviarc.get_playing_info()
                self._reset_playing_info()
                if playing_info is None or not playing_info:
                    self._channel_name = 'App'
                else:
                    self._program_name = playing_info.get('programTitle')
                    self._channel_name = playing_info.get('title')
                    self._program_media_type = playing_info.get(
                        'programMediaType')
                    self._channel_number = playing_info.get('dispNum')
                    self._source = playing_info.get('source')
                    self._content_uri = playing_info.get('uri')
                    self._duration = playing_info.get('durationSec')
                    self._start_date_time = playing_info.get('startDateTime')
            else:
                self._state = STATE_OFF

        except Exception as exception_instance:  # pylint: disable=broad-except
            _LOGGER.error(exception_instance)
            self._state = STATE_OFF

    def _reset_playing_info(self):
        self._program_name = None
        self._channel_name = None
        self._program_media_type = None
        self._channel_number = None
        self._source = None
        self._content_uri = None
        self._duration = None
        self._start_date_time = None

    def _refresh_volume(self):
        """Refresh volume information."""
        volume_info = self._braviarc.get_volume_info()
        if volume_info is not None:
            self._volume = volume_info.get('volume')
            self._min_volume = volume_info.get('minVolume')
            self._max_volume = volume_info.get('maxVolume')
            self._muted = volume_info.get('mute')

    def _refresh_channels(self):
        if not self._source_list:
            self._content_mapping = self._braviarc. \
                load_source_list()
            self._source_list = []
            for key in self._content_mapping:
                self._source_list.append(key)

    @property
    def name(self):
        """Return the name of the device."""
        return self._name

    @property
    def state(self):
        """Return the state of the device."""
        return self._state

    @property
    def source(self):
        """Return the current input source."""
        return self._source

    @property
    def source_list(self):
        """List of available input sources."""
        return self._source_list

    @property
    def volume_level(self):
        """Volume level of the media player (0..1)."""
        if self._volume is not None:
            return self._volume / 100
        return None

    @property
    def is_volume_muted(self):
        """Boolean if volume is currently muted."""
        return self._muted

    @property
    def supported_features(self):
        """Flag media player features that are supported."""
        return SUPPORT_BRAVIA

    @property
    def media_title(self):
        """Title of current playing media."""
        return_value = None
        if self._channel_name is not None:
            return_value = self._channel_name
            if self._program_name is not None:
                return_value = return_value + ': ' + self._program_name
        return return_value

    @property
    def media_content_id(self):
        """Content ID of current playing media."""
        return self._channel_name

    @property
    def media_duration(self):
        """Duration of current playing media in seconds."""
        return self._duration

    def set_volume_level(self, volume):
        """Set volume level, range 0..1."""
        self._braviarc.set_volume_level(volume)

    def turn_on(self):
        """Turn the media player on."""
        self._braviarc.turn_on()

    def turn_off(self):
        """Turn off media player."""
        self._braviarc.turn_off()

    def volume_up(self):
        """Volume up the media player."""
        self._braviarc.volume_up()

    def volume_down(self):
        """Volume down media player."""
        self._braviarc.volume_down()

    def mute_volume(self, mute):
        """Send mute command."""
        self._braviarc.mute_volume(mute)

    def select_source(self, source):
        """Set the input source."""
        if source in self._content_mapping:
            uri = self._content_mapping[source]
            self._braviarc.play_content(uri)

    def media_play_pause(self):
        """Simulate play pause media player."""
        if self._playing:
            self.media_pause()
        else:
            self.media_play()

    def media_play(self):
        """Send play command."""
        self._playing = True
        self._braviarc.media_play()

    def media_pause(self):
        """Send media pause command to media player."""
        self._playing = False
        self._braviarc.media_pause()

    def media_next_track(self):
        """Send next track command."""
        self._braviarc.media_next_track()

    def media_previous_track(self):
        """Send the previous track command."""
        self._braviarc.media_previous_track()

请大神教一下,我也会多思考,多学习,举一反三滴,谢谢


回复

使用道具 举报

35

主题

533

帖子

2887

积分

金牌会员

Rank: 6Rank: 6

积分
2887
金钱
2354
HASS币
0
发表于 2018-6-13 13:04:19 | 显示全部楼层
    - service_template: >
        {% if is_state("media_player.sony_bravia_tv", "off") %}
        media_player.turn_on
        {% endif %}
      entity_id: media_player.sony_bravia_tv
    - delay: 00:00:01
    - service: media_player.select_source
      data:
        entity_id: media_player.sony_bravia_tv
        source: HDMI 4/ARC
截取了我自动化里一部分内容,供参考。
回复

使用道具 举报

16

主题

324

帖子

2274

积分

金牌会员

Rank: 6Rank: 6

积分
2274
金钱
1950
HASS币
0
 楼主| 发表于 2018-6-13 14:38:24 来自手机 | 显示全部楼层
debitus 发表于 2018-6-13 13:04
[code]    - service_template: >
        {% if is_state("media_player.sony_br ...

好的晚上下班回家研究研究,谢谢大神
回复

使用道具 举报

26

主题

553

帖子

2726

积分

金牌会员

Rank: 6Rank: 6

积分
2726
金钱
2148
HASS币
100

教程狂人

发表于 2018-6-13 17:33:22 | 显示全部楼层
建几个template,引用原来的实体中的服务、状态等
回复

使用道具 举报

16

主题

324

帖子

2274

积分

金牌会员

Rank: 6Rank: 6

积分
2274
金钱
1950
HASS币
0
 楼主| 发表于 2018-6-13 19:09:41 | 显示全部楼层
debitus 发表于 2018-6-13 13:04
[code]    - service_template: >
        {% if is_state("media_player.sony_br ...

大神能不能发一个全一点的我学习学习
回复

使用道具 举报

35

主题

533

帖子

2887

积分

金牌会员

Rank: 6Rank: 6

积分
2887
金钱
2354
HASS币
0
发表于 2018-6-13 22:35:40 | 显示全部楼层
本帖最后由 debitus 于 2018-6-13 22:38 编辑
wyh260595711 发表于 2018-6-13 19:09
大神能不能发一个全一点的我学习学习

我不是大神,只是小白爱好者。发全的其实也没意义,前后有很多关联。
这个有个前置的input_boolean,后面还有一个script代码。

      
- alias: easy_film_on
  initial_state: True # 默认为开启态
  trigger:
    - platform: state
      entity_id: input_boolean.easy_film # 按钮触发
      to: 'on'
  action:
    - service_template: >
        {% if is_state("media_player.den", "off") %} # 检测功放机状态,如果关闭的,则调用打开服务
        media_player.turn_on
        {% endif %}
      entity_id: media_player.den # 打开功放机
    - delay: 00:00:01
    - service: media_player.select_source # 选择功放机的输入信号源
      data:
        entity_id: media_player.den
        source: 'Media Player' # 需要在HA页面的“开发者工具”里的“状态”项里,看具体设备的值是什么
    - delay: 00:00:01
    - service_template: >
        {% if is_state("media_player.sony_bravia_tv", "off") %}
        media_player.turn_on
        {% endif %}
      entity_id: media_player.sony_bravia_tv
    - delay: 00:00:01
    - service: media_player.select_source
      data:
        entity_id: media_player.sony_bravia_tv
        source: 'HDMI 4/ARC'

- alias: easy_film_off
  initial_state: True
  trigger:
    - platform: state
      entity_id: input_boolean.easy_film
      to: 'off'
  action:
    - service: media_player.turn_off
      entity_id: media_player.den
    - delay: 00:00:01
    - service: script.turn_on
      entity_id: script.tv_mode_select

回复

使用道具 举报

8

主题

329

帖子

1763

积分

金牌会员

Rank: 6Rank: 6

积分
1763
金钱
1434
HASS币
0
发表于 2019-11-25 17:50:22 | 显示全部楼层
本帖最后由 12512310 于 2019-11-25 17:54 编辑

我也是sony的,接入后自动化的话就是直接在ha自带的自动化里面建立的,不是太复杂的要求的话,建立还是很方便的,比如ps4开启自动开电视开功放切换信号源什么的,就按照顺序选择服务等就可以了。我截图里面是开电视后开启功放以及切换电视及功放源。 1.jpg 2.jpg 3.jpg
回复

使用道具 举报

1

主题

23

帖子

119

积分

注册会员

Rank: 2

积分
119
金钱
96
HASS币
0
发表于 2021-4-14 07:08:15 | 显示全部楼层
回去研究下,谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-27 03:24 , Processed in 0.059451 second(s), 33 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表