core/homeassistant/components/shodan/sensor.py

103 lines
2.7 KiB
Python
Raw Normal View History

2019-02-24 04:14:26 +00:00
"""Sensor for displaying the number of result on Shodan.io."""
2017-08-10 15:27:49 +00:00
from datetime import timedelta
import logging
2017-08-10 15:27:49 +00:00
import shodan
2017-08-10 15:27:49 +00:00
import voluptuous as vol
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import ATTR_ATTRIBUTION, CONF_API_KEY, CONF_NAME
import homeassistant.helpers.config_validation as cv
2017-08-10 15:27:49 +00:00
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
ATTRIBUTION = "Data provided by Shodan"
2019-07-31 19:25:30 +00:00
CONF_QUERY = "query"
2017-08-10 15:27:49 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "Shodan Sensor"
2017-08-10 15:27:49 +00:00
2019-07-31 19:25:30 +00:00
ICON = "mdi:tooltip-text"
2017-08-10 15:27:49 +00:00
SCAN_INTERVAL = timedelta(minutes=15)
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_API_KEY): cv.string,
vol.Required(CONF_QUERY): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
2017-08-10 15:27:49 +00:00
def setup_platform(hass, config, add_entities, discovery_info=None):
2017-08-10 15:27:49 +00:00
"""Set up the Shodan sensor."""
api_key = config.get(CONF_API_KEY)
name = config.get(CONF_NAME)
query = config.get(CONF_QUERY)
data = ShodanData(shodan.Shodan(api_key), query)
try:
data.update()
except shodan.exception.APIError as error:
_LOGGER.warning("Unable to connect to Shodan.io: %s", error)
return False
add_entities([ShodanSensor(data, name)], True)
2017-08-10 15:27:49 +00:00
class ShodanSensor(Entity):
"""Representation of the Shodan sensor."""
def __init__(self, data, name):
"""Initialize the Shodan sensor."""
self.data = data
self._name = name
self._state = None
2019-07-31 19:25:30 +00:00
self._unit_of_measurement = "Hits"
2017-08-10 15:27:49 +00:00
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit_of_measurement
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return ICON
@property
def device_state_attributes(self):
"""Return the state attributes of the sensor."""
2019-07-31 19:25:30 +00:00
return {ATTR_ATTRIBUTION: ATTRIBUTION}
2017-08-10 15:27:49 +00:00
def update(self):
"""Get the latest data and updates the states."""
self.data.update()
2019-07-31 19:25:30 +00:00
self._state = self.data.details["total"]
2017-08-10 15:27:49 +00:00
class ShodanData:
2017-08-10 15:27:49 +00:00
"""Get the latest data and update the states."""
def __init__(self, api, query):
"""Initialize the data object."""
self._api = api
self._query = query
self.details = None
def update(self):
"""Get the latest data from shodan.io."""
self.details = self._api.count(self._query)