mirror of
https://github.com/home-assistant/core.git
synced 2025-06-25 01:21:51 +02:00
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:
@ -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"
|
||||
|
@ -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)
|
||||
|
@ -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))
|
||||
|
||||
|
@ -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": []
|
||||
|
@ -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
|
||||
|
||||
|
Reference in New Issue
Block a user