diff --git a/homeassistant/components/braviatv/manifest.json b/homeassistant/components/braviatv/manifest.json index d8a835676b8..52e8e1bec76 100644 --- a/homeassistant/components/braviatv/manifest.json +++ b/homeassistant/components/braviatv/manifest.json @@ -3,7 +3,8 @@ "name": "Braviatv", "documentation": "https://www.home-assistant.io/components/braviatv", "requirements": [ - "braviarc-homeassistant==0.3.7.dev0" + "braviarc-homeassistant==0.3.7.dev0", + "getmac==0.8.1" ], "dependencies": [ "configurator" diff --git a/homeassistant/components/braviatv/media_player.py b/homeassistant/components/braviatv/media_player.py index 6377561009d..637e2922222 100644 --- a/homeassistant/components/braviatv/media_player.py +++ b/homeassistant/components/braviatv/media_player.py @@ -1,7 +1,8 @@ """Support for interface with a Sony Bravia TV.""" +import ipaddress import logging -import re +from getmac import get_mac_address import voluptuous as vol from homeassistant.components.media_player import ( @@ -40,19 +41,6 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ }) -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 - - def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the Sony Bravia TV platform.""" host = config.get(CONF_HOST) @@ -84,9 +72,15 @@ def setup_bravia(config, pin, hass, add_entities): request_configuration(config, hass, add_entities) return - mac = _get_mac_address(host) - if mac is not None: - mac = mac.decode('utf8') + try: + if ipaddress.ip_address(host).version == 6: + mode = 'ip6' + else: + mode = 'ip' + except ValueError: + mode = 'hostname' + mac = get_mac_address(**{mode: host}) + # If we came here and configuring this host, mark as done if host in _CONFIGURING: request_id = _CONFIGURING.pop(host) diff --git a/homeassistant/components/nmap_tracker/device_tracker.py b/homeassistant/components/nmap_tracker/device_tracker.py index 1b528b0af7e..913ae98ab89 100644 --- a/homeassistant/components/nmap_tracker/device_tracker.py +++ b/homeassistant/components/nmap_tracker/device_tracker.py @@ -1,10 +1,9 @@ """Support for scanning a network with nmap.""" import logging -import re -import subprocess from collections import namedtuple from datetime import timedelta +from getmac import get_mac_address import voluptuous as vol import homeassistant.helpers.config_validation as cv @@ -40,18 +39,6 @@ def get_scanner(hass, config): Device = namedtuple('Device', ['mac', 'name', 'ip', 'last_update']) -def _arp(ip_address): - """Get the MAC address for a given IP.""" - cmd = ['arp', '-n', ip_address] - arp = subprocess.Popen(cmd, stdout=subprocess.PIPE) - out, _ = arp.communicate() - match = re.search(r'(([0-9A-Fa-f]{1,2}\:){5}[0-9A-Fa-f]{1,2})', str(out)) - if match: - return ':'.join([i.zfill(2) for i in match.group(0).split(':')]) - _LOGGER.info('No MAC address found for %s', ip_address) - return None - - class NmapDeviceScanner(DeviceScanner): """This class scans for devices using nmap.""" @@ -132,8 +119,9 @@ class NmapDeviceScanner(DeviceScanner): continue name = info['hostnames'][0]['name'] if info['hostnames'] else ipv4 # Mac address only returned if nmap ran as root - mac = info['addresses'].get('mac') or _arp(ipv4) + mac = info['addresses'].get('mac') or get_mac_address(ip=ipv4) if mac is None: + _LOGGER.info('No MAC address found for %s', ipv4) continue last_results.append(Device(mac.upper(), name, ipv4, now)) diff --git a/homeassistant/components/nmap_tracker/manifest.json b/homeassistant/components/nmap_tracker/manifest.json index f4c4d33f036..0380acba1ac 100644 --- a/homeassistant/components/nmap_tracker/manifest.json +++ b/homeassistant/components/nmap_tracker/manifest.json @@ -3,7 +3,8 @@ "name": "Nmap tracker", "documentation": "https://www.home-assistant.io/components/nmap_tracker", "requirements": [ - "python-nmap==0.6.1" + "python-nmap==0.6.1", + "getmac==0.8.1" ], "dependencies": [], "codeowners": [] diff --git a/requirements_all.txt b/requirements_all.txt index 539645549ee..6863078985b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -513,6 +513,10 @@ georss_ign_sismologia_client==0.2 # homeassistant.components.qld_bushfire georss_qld_bushfire_alert_client==0.3 +# homeassistant.components.braviatv +# homeassistant.components.nmap_tracker +getmac==0.8.1 + # homeassistant.components.gitter gitterpy==0.1.7