Skip to content

Commit

Permalink
Use attrs instead of properties in Bravia TV integration (#52045)
Browse files Browse the repository at this point in the history
* Use attrs instead of properties

* Revert to using properties for dynamic data

* Move volume_level to coordinator

* Move media_title to coordinator

* Remove unused variables

* Fix variable name

* Revert removed variables
  • Loading branch information
bieniu committed Jun 23, 2021
1 parent 80ae346 commit 75faee4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 49 deletions.
15 changes: 11 additions & 4 deletions homeassistant/components/braviatv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def __init__(self, hass, host, mac, pin, ignored_sources):
self.pin = pin
self.ignored_sources = ignored_sources
self.muted = False
self.program_name = None
self.channel_name = None
self.channel_number = None
self.media_title = None
self.source = None
self.source_list = []
self.original_content_list = []
Expand All @@ -85,7 +85,7 @@ def __init__(self, hass, host, mac, pin, ignored_sources):
self.audio_output = None
self.min_volume = None
self.max_volume = None
self.volume = None
self.volume_level = None
self.is_on = False
# Assume that the TV is in Play mode
self.playing = True
Expand Down Expand Up @@ -117,8 +117,9 @@ def _refresh_volume(self):
"""Refresh volume information."""
volume_info = self.braviarc.get_volume_info(self.audio_output)
if volume_info is not None:
volume = volume_info.get("volume")
self.volume_level = volume / 100 if volume is not None else None
self.audio_output = volume_info.get("target")
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")
Expand All @@ -140,7 +141,7 @@ def _refresh_channels(self):
def _refresh_playing_info(self):
"""Refresh playing information."""
playing_info = self.braviarc.get_playing_info()
self.program_name = playing_info.get("programTitle")
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")
Expand All @@ -150,6 +151,12 @@ def _refresh_playing_info(self):
self.start_date_time = playing_info.get("startDateTime")
if not playing_info:
self.channel_name = "App"
if self.channel_name is not None:
self.media_title = self.channel_name
if program_name is not None:
self.media_title = f"{self.media_title}: {program_name}"
else:
self.media_title = None

def _update_tv_data(self):
"""Connect and update TV info."""
Expand Down
32 changes: 5 additions & 27 deletions homeassistant/components/braviatv/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,12 @@ class BraviaTVMediaPlayer(CoordinatorEntity, MediaPlayerEntity):
def __init__(self, coordinator, name, unique_id, device_info):
"""Initialize the entity."""

self._name = name
self._unique_id = unique_id
self._device_info = device_info
self._attr_device_info = device_info
self._attr_name = name
self._attr_unique_id = unique_id

super().__init__(coordinator)

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

@property
def unique_id(self):
"""Return a unique_id for this entity."""
return self._unique_id

@property
def device_info(self):
"""Return the device info."""
return self._device_info

@property
def state(self):
"""Return the state of the device."""
Expand All @@ -157,9 +142,7 @@ def source_list(self):
@property
def volume_level(self):
"""Volume level of the media player (0..1)."""
if self.coordinator.volume is not None:
return self.coordinator.volume / 100
return None
return self.coordinator.volume_level

@property
def is_volume_muted(self):
Expand All @@ -169,12 +152,7 @@ def is_volume_muted(self):
@property
def media_title(self):
"""Title of current playing media."""
return_value = None
if self.coordinator.channel_name is not None:
return_value = self.coordinator.channel_name
if self.coordinator.program_name is not None:
return_value = f"{return_value}: {self.coordinator.program_name}"
return return_value
return self.coordinator.media_title

@property
def media_content_id(self):
Expand Down
21 changes: 3 additions & 18 deletions homeassistant/components/braviatv/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,12 @@ class BraviaTVRemote(CoordinatorEntity, RemoteEntity):
def __init__(self, coordinator, name, unique_id, device_info):
"""Initialize the entity."""

self._name = name
self._unique_id = unique_id
self._device_info = device_info
self._attr_device_info = device_info
self._attr_name = name
self._attr_unique_id = unique_id

super().__init__(coordinator)

@property
def unique_id(self):
"""Return the unique ID of the device."""
return self._unique_id

@property
def device_info(self):
"""Return device specific attributes."""
return self._device_info

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

@property
def is_on(self):
"""Return true if device is on."""
Expand Down

0 comments on commit 75faee4

Please sign in to comment.