braviatv, nmap_tracker: use getmac for getting MAC addresses (#24628)

* braviatv, nmap_tracker: use getmac for getting MAC addresses

Refs https://github.com/home-assistant/home-assistant/pull/24601

* Move getmac imports to top level
This commit is contained in:
Ville Skyttä 2019-06-20 23:35:02 +03:00 committed by GitHub
parent ecfbfb4527
commit f1cbb2a0b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 34 deletions

View File

@ -3,7 +3,8 @@
"name": "Braviatv", "name": "Braviatv",
"documentation": "https://www.home-assistant.io/components/braviatv", "documentation": "https://www.home-assistant.io/components/braviatv",
"requirements": [ "requirements": [
"braviarc-homeassistant==0.3.7.dev0" "braviarc-homeassistant==0.3.7.dev0",
"getmac==0.8.1"
], ],
"dependencies": [ "dependencies": [
"configurator" "configurator"

View File

@ -1,7 +1,8 @@
"""Support for interface with a Sony Bravia TV.""" """Support for interface with a Sony Bravia TV."""
import ipaddress
import logging import logging
import re
from getmac import get_mac_address
import voluptuous as vol import voluptuous as vol
from homeassistant.components.media_player import ( 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): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Sony Bravia TV platform.""" """Set up the Sony Bravia TV platform."""
host = config.get(CONF_HOST) host = config.get(CONF_HOST)
@ -84,9 +72,15 @@ def setup_bravia(config, pin, hass, add_entities):
request_configuration(config, hass, add_entities) request_configuration(config, hass, add_entities)
return return
mac = _get_mac_address(host) try:
if mac is not None: if ipaddress.ip_address(host).version == 6:
mac = mac.decode('utf8') 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 we came here and configuring this host, mark as done
if host in _CONFIGURING: if host in _CONFIGURING:
request_id = _CONFIGURING.pop(host) request_id = _CONFIGURING.pop(host)

View File

@ -1,10 +1,9 @@
"""Support for scanning a network with nmap.""" """Support for scanning a network with nmap."""
import logging import logging
import re
import subprocess
from collections import namedtuple from collections import namedtuple
from datetime import timedelta from datetime import timedelta
from getmac import get_mac_address
import voluptuous as vol import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -40,18 +39,6 @@ def get_scanner(hass, config):
Device = namedtuple('Device', ['mac', 'name', 'ip', 'last_update']) 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): class NmapDeviceScanner(DeviceScanner):
"""This class scans for devices using nmap.""" """This class scans for devices using nmap."""
@ -132,8 +119,9 @@ class NmapDeviceScanner(DeviceScanner):
continue continue
name = info['hostnames'][0]['name'] if info['hostnames'] else ipv4 name = info['hostnames'][0]['name'] if info['hostnames'] else ipv4
# Mac address only returned if nmap ran as root # 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: if mac is None:
_LOGGER.info('No MAC address found for %s', ipv4)
continue continue
last_results.append(Device(mac.upper(), name, ipv4, now)) last_results.append(Device(mac.upper(), name, ipv4, now))

View File

@ -3,7 +3,8 @@
"name": "Nmap tracker", "name": "Nmap tracker",
"documentation": "https://www.home-assistant.io/components/nmap_tracker", "documentation": "https://www.home-assistant.io/components/nmap_tracker",
"requirements": [ "requirements": [
"python-nmap==0.6.1" "python-nmap==0.6.1",
"getmac==0.8.1"
], ],
"dependencies": [], "dependencies": [],
"codeowners": [] "codeowners": []

View File

@ -513,6 +513,10 @@ georss_ign_sismologia_client==0.2
# homeassistant.components.qld_bushfire # homeassistant.components.qld_bushfire
georss_qld_bushfire_alert_client==0.3 georss_qld_bushfire_alert_client==0.3
# homeassistant.components.braviatv
# homeassistant.components.nmap_tracker
getmac==0.8.1
# homeassistant.components.gitter # homeassistant.components.gitter
gitterpy==0.1.7 gitterpy==0.1.7