8000 Yamaha MusicCast: check known_hosts by jalmeroth · Pull Request #9580 · home-assistant/core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Yamaha MusicCast: check known_hosts #9580

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

Merged
merged 2 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions homeassistant/components/media_player/yamaha_musiccast.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
SUPPORT_SELECT_SOURCE
)

REQUIREMENTS = ['pymusiccast==0.1.0']
KNOWN_HOSTS_KEY = 'data_yamaha_musiccast'

REQUIREMENTS = ['pymusiccast==0.1.2']

DEFAULT_NAME = "Yamaha Receiver"
DEFAULT_PORT = 5005
Expand All @@ -47,16 +49,48 @@

def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Yamaha MusicCast platform."""
import socket
import pymusiccast

known_hosts = hass.data.get(KNOWN_HOSTS_KEY)
if known_hosts is None:
known_hosts = hass.data[KNOWN_HOSTS_KEY] = []
Copy link
Member

Choose a reason for hiding this comment

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

know_hosts = hass.data.get() or []

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pvizeli thanks for you comment, but I disagree here:

.get() expects an argument. We could use hass.data.get(KNOWN_HOSTS_KEY, []) instead.

But then again line 57 sets known_hosts AND hass.data[KNOWN_HOSTS_KEY] = [].

_LOGGER.debug("known_hosts: %s", known_hosts)

name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
port = config.get(CONF_PORT)

receiver = pymusiccast.McDevice(host, udp_port=port)
_LOGGER.debug("receiver: %s / Port: %d", receiver, port)

add_devices([YamahaDevice(receiver, name)], True)
# Get IP of host to prevent duplicates
try:
ipaddr = socket.gethostbyname(host)
except (OSError) as error:
_LOGGER.error(
"Could not communicate with %s:%d: %s", host, port, error)
return

if [item for item in known_hosts if item[0] == ipaddr]:
_LOGGER.warning("Host %s:%d already registered.", host, port)
return

if [item for item in known_hosts if item[1] == port]:
_LOGGER.warning("Port %s:%d already registered.", host, port)
return

reg_host = (ipaddr, port)
known_hosts.append(reg_host)

try:
receiver = pymusiccast.McDevice(ipaddr, udp_port=port)
except pymusiccast.exceptions.YMCInitError as err:
_LOGGER.error(err)
receiver = None

if receiver:
_LOGGER.debug("receiver: %s / Port: %d", receiver, port)
add_devices([YamahaDevice(receiver, name)], True)
else:
known_hosts.remove(reg_host)


class YamahaDevice(MediaPlayerDevice):
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ pymochad==0.1.1
pymodbus==1.3.1

# homeassistant.components.media_player.yamaha_musiccast
pymusiccast==0.1.0
pymusiccast==0.1.2

# homeassistant.components.cover.myq
pymyq==0.0.8
Expand Down
0