Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Uptime Robot sensor #14631

Merged
merged 8 commits into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ omit =
homeassistant/components/binary_sensor/ping.py
homeassistant/components/binary_sensor/rest.py
homeassistant/components/binary_sensor/tapsaff.py
homeassistant/components/binary_sensor/uptimerobot.py
homeassistant/components/browser.py
homeassistant/components/calendar/caldav.py
homeassistant/components/calendar/todoist.py
Expand Down
88 changes: 88 additions & 0 deletions homeassistant/components/binary_sensor/uptimerobot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
"""A component to monitor Uptime Robot monitors.

For more details about this component, please refer to the documentation at
https://www.home-assistant.io/components/binary_sensor.uptimerobot
"""
import logging

import voluptuous as vol

from homeassistant.components.binary_sensor import (
BinarySensorDevice, PLATFORM_SCHEMA)
from homeassistant.const import CONF_API_KEY
import homeassistant.helpers.config_validation as cv

REQUIREMENTS = ['pyuptimerobot==0.0.4']

_LOGGER = logging.getLogger(__name__)

ATTR_TARGET = 'target'

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_API_KEY): cv.string,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Uptime Robot binary_sensors."""
from pyuptimerobot import UptimeRobot

up_robot = UptimeRobot()
apikey = config.get(CONF_API_KEY)
monitors = up_robot.getMonitors(apikey)

devices = []
if not monitors or monitors.get('stat') != 'ok':
_LOGGER.error('Error connecting to uptime robot.')
return

for monitor in monitors['monitors']:
devices.append(UptimeRobotBinarySensor(
apikey, up_robot, monitorid=monitor['id'],
name=monitor['friendly_name'], target=monitor['url']))

add_devices(devices, True)


class UptimeRobotBinarySensor(BinarySensorDevice):
"""Representation of a Uptime Robot binary_sensor."""

def __init__(self, apikey, up_robot, monitorid, name, target):
"""Initialize the binary_sensor."""
self._apikey = apikey
self._monitorid = str(monitorid)
self._name = name
self._target = target
self._up_robot = up_robot
self._state = None

@property
def name(self):
"""Return the name of the binary_sensor."""
return self._name

@property
def is_on(self):
"""Return the state of the binary sensor."""
return self._state

@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return 'connectivity'

@property
def device_state_attributes(self):
"""Return the state attributes of the binary_sensor."""
return {
ATTR_TARGET: self._target,
}

def update(self):
"""Get the latest state of the binary_sensor."""
monitor = self._up_robot.getMonitors(self._apikey, self._monitorid)
if not monitor or monitor.get('stat') != 'ok':
_LOGGER.debug("Failed to get new state, trying again later.")
return
status = monitor['monitors'][0]['status']
self._state = 1 if status == 2 else 0
3 changes: 3 additions & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,9 @@ pyunifi==2.13
# homeassistant.components.upnp
pyupnp-async==0.1.0.2

# homeassistant.components.binary_sensor.uptimerobot
pyuptimerobot==0.0.4

# homeassistant.components.keyboard
# pyuserinput==0.1.11

Expand Down