Advertisement
techblog

device_status

Feb 5th, 2019
1,034
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 4.10 KB | None | 0 0
  1. """////////////////////////////////////////////////////////////////////////////////////////////////
  2. This sensor checks the status of network objects such as Computers, Routers, Servers, Esp8266 based sensors, Smart TV and more
  3. The sensor is checking the object status by using fping commans wich needs to be installs.
  4. to install fping, run the fallowing command:
  5. sudo apt-get install fping --yes
  6.  
  7. Date: 26/04/2018
  8. Author: Tomer Klein
  9. Many thanks to Tomer Figenblat (https://github.com/TomerFi) for all the help.
  10.  
  11. installation notes:
  12. place this file in the following folder and restart home assistant:
  13. /config/custom_components/sensor or /home/homeassistant/.homeassistant/custom_components/sensor
  14.  
  15. yaml configuration example:
  16. sensor:
  17.  - platform: device_status
  18.    scan_interval: 10
  19.    devices:
  20.    internet_connection:
  21.       host: 8.8.8.8 8
  22.       name: "Internet Connection"
  23.    
  24.  
  25. ////////////////////////////////////////////////////////////////////////////////////////////////"""
  26. import datetime
  27. import logging
  28. import re
  29. import subprocess
  30. import voluptuous as vol
  31. from homeassistant.helpers.entity import Entity, generate_entity_id
  32. import homeassistant.helpers.config_validation as cv
  33. from homeassistant.components.sensor import DOMAIN, PLATFORM_SCHEMA
  34. from homeassistant.const import (CONF_HOST, CONF_NAME, CONF_SCAN_INTERVAL, CONF_ICON, CONF_DEVICES)
  35.  
  36.  
  37. REQUIREMENTS = []
  38. DEPENDENCIES = []
  39. _LOGGER = logging.getLogger(__name__)
  40. #DEFAULT_NAME = 'Check Status' #
  41. SCAN_INTERVAL_DEFAULT = datetime.timedelta(seconds=10)
  42. DEFAULT_ICON = "mdi:desktop-classic"
  43. #DOMAIN = "sensor" #
  44. PLATFORM_NAME = "device_status"
  45. ENTITY_ID_FORMAT = DOMAIN + '.' + PLATFORM_NAME + '_{}'
  46.  
  47. DEVICE_SCHEMA = vol.Schema({
  48.     vol.Required(CONF_HOST): cv.string,
  49.     vol.Optional(CONF_NAME): cv.string,
  50.     vol.Required(CONF_ICON, default=DEFAULT_ICON): cv.icon
  51. })
  52.  
  53. PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
  54.     vol.Required(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL_DEFAULT): cv.positive_timedelta,
  55.     vol.Required(CONF_DEVICES):
  56.         vol.Schema({cv.slug: DEVICE_SCHEMA})
  57. })
  58.  
  59.  
  60. def setup_platform(hass, config, add_devices, discovery_info=None):
  61.    
  62.     scan_interval = config.get(CONF_SCAN_INTERVAL)
  63.     devices = config.get(CONF_DEVICES)
  64.  
  65.     entities = []
  66.  
  67.     for slug_id, config in devices.items():
  68.        name = config.get(CONF_NAME, None)
  69.         host = config.get(CONF_HOST)
  70.         icon = config.get(CONF_ICON)
  71.        
  72.         device = CHECK_STATUS(slug_id, hass, name, host, icon)
  73.  
  74.         entities.append(device)
  75.  
  76.     add_devices(entities, True)
  77.     return True
  78.  
  79. def get_device_status(host):
  80.    p = subprocess.Popen("fping -C1 -q "+ host +"  2>&1 | grep -v '-' | wc -l", stdout=subprocess.PIPE, shell=True)
  81.     (output, err) = p.communicate()
  82.     p_status = p.wait()
  83.     status = re.findall('\d+', str(output))[0]
  84.     if status=="1":
  85.        return 'online'
  86.     else:
  87.        return 'offline'
  88.  
  89. class CHECK_STATUS(Entity):
  90.    """representation of the sensor entity"""
  91.     def __init__(self, slug_id, hass, name, host, icon):
  92.        """initialize the sensor entity"""
  93.         self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, slug_id, hass=hass)
  94.         self.hass = hass
  95.         self._name = name
  96.         self._host = host
  97.         self._icon = icon
  98.         self._state = get_device_status(host)
  99.        
  100.  
  101.     @property
  102.     def name(self):
  103.        """friendly name"""
  104.         return self._name
  105.  
  106.     @property
  107.     def host(self):
  108.        """host"""
  109.         return self._host
  110.  
  111.     @property
  112.     def should_poll(self):
  113.        """entity should not be polled for updates"""
  114.         return True
  115.  
  116.     @property
  117.     def state(self):
  118.        """sensor state"""
  119.         return self._state
  120.  
  121.     @property
  122.     def icon(self):
  123.        """sensor icon"""
  124.         if self._state == 'online':
  125.            return 'mdi:arrow-up-bold-circle-outline'
  126.         else:
  127.            return 'mdi:arrow-down-bold-circle-outline'
  128.            
  129.         return self._icon
  130.      
  131.     def update(self):
  132.        """handling state updates"""
  133.         self._state = get_device_status(self._host)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement