core/homeassistant/components/shodan/sensor.py

84 lines
2.3 KiB
Python
Raw Normal View History

2019-02-24 04:14:26 +00:00
"""Sensor for displaying the number of result on Shodan.io."""
from __future__ import annotations
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, SensorEntity
2022-01-19 22:04:06 +00:00
from homeassistant.const import CONF_API_KEY, CONF_NAME
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2017-08-10 15:27:49 +00:00
_LOGGER = logging.getLogger(__name__)
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
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: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
2017-08-10 15:27:49 +00:00
"""Set up the Shodan sensor."""
2022-01-19 22:04:06 +00:00
api_key = config[CONF_API_KEY]
name = config[CONF_NAME]
query = config[CONF_QUERY]
2017-08-10 15:27:49 +00:00
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
2017-08-10 15:27:49 +00:00
add_entities([ShodanSensor(data, name)], True)
2017-08-10 15:27:49 +00:00
class ShodanSensor(SensorEntity):
2017-08-10 15:27:49 +00:00
"""Representation of the Shodan sensor."""
2022-01-19 22:04:06 +00:00
_attr_attribution = "Data provided by Shodan"
_attr_icon = "mdi:tooltip-text"
_attr_native_unit_of_measurement = "Hits"
def __init__(self, data: ShodanData, name: str) -> None:
2017-08-10 15:27:49 +00:00
"""Initialize the Shodan sensor."""
self.data = data
2022-01-19 22:04:06 +00:00
self._attr_name = name
2017-08-10 15:27:49 +00:00
2022-01-19 22:04:06 +00:00
def update(self) -> None:
2017-08-10 15:27:49 +00:00
"""Get the latest data and updates the states."""
2022-01-19 22:04:06 +00:00
data = self.data.update()
2022-02-02 19:44:16 +00:00
self._attr_native_value = data["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."""
2022-01-19 22:04:06 +00:00
def __init__(self, api: shodan.Shodan, query: str) -> None:
2017-08-10 15:27:49 +00:00
"""Initialize the data object."""
self._api = api
self._query = query
def update(self):
"""Get the latest data from shodan.io."""
2022-01-19 22:04:06 +00:00
return self._api.count(self._query)