Skip to content

Instantly share code, notes, and snippets.

@tboyce021
Last active January 23, 2021 16:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tboyce021/a89f9b08cfbb8532203e2be9571f14d2 to your computer and use it in GitHub Desktop.
Save tboyce021/a89f9b08cfbb8532203e2be9571f14d2 to your computer and use it in GitHub Desktop.
sensor.hassio_info
"""Support for Hass.io info."""
import logging
import voluptuous as vol
from homeassistant.components.hassio import DOMAIN as HASSIO_DOMAIN
from homeassistant.helpers.discovery import load_platform
from .handler import extend_hassio
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'hassio_info'
DEPENDENCIES = [HASSIO_DOMAIN]
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
}),
}, extra=vol.ALLOW_EXTRA)
DEFAULT_NAME = 'Hass.io Addon'
async def async_setup(hass, config):
"""Set up the Hass.io info component."""
extend_hassio(hass.data[HASSIO_DOMAIN])
load_platform(hass, 'sensor', DOMAIN, None, config)
load_platform(hass, 'switch', DOMAIN, None, config)
return True
"""Handler for Hass.io."""
from homeassistant.components.hassio.handler import _api_bool, _api_data
def extend_hassio(hassio):
"""Extend the HassIO object to add some currently unavailable helper methods"""
import types
hassio.get_supervisor_info = types.MethodType(get_supervisor_info, hassio)
hassio.start_addon = types.MethodType(start_addon, hassio)
hassio.stop_addon = types.MethodType(stop_addon, hassio)
@_api_data
def get_supervisor_info(self):
"""Return data for Hass.io Supervisor
This method return a coroutine.
"""
return self.send_command("/supervisor/info", method="get")
@_api_bool
def start_addon(self, addon):
"""Start an add-on
This method return a coroutine.
"""
return self.send_command("/addons/{}/start".format(addon))
@_api_bool
def stop_addon(self, addon):
"""Stop an add-on
This method return a coroutine.
"""
return self.send_command("/addons/{}/stop".format(addon))
"""
Support for Hass.io sensors.
"""
import logging
import voluptuous as vol
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from homeassistant.helpers.entity import Entity
from . import DEFAULT_NAME, DOMAIN as HASSIO_INFO_DOMAIN, HASSIO_DOMAIN
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = [HASSIO_INFO_DOMAIN]
ICON = 'mdi:home-assistant'
SENSOR_VERSION = 'version'
SENSOR_LAST_VERSION = 'last_version'
SENSOR_NAMES = {
SENSOR_VERSION: 'Version',
SENSOR_LAST_VERSION: 'Last Version'
}
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the platform."""
hassio = hass.data[HASSIO_DOMAIN]
info = await hassio.get_supervisor_info()
addons = info['addons']
for addon in addons:
for sensor_type in (SENSOR_VERSION, SENSOR_LAST_VERSION):
async_add_entities([AddonSensor(hassio, addon, sensor_type)], True)
class AddonSensor(Entity):
"""Representation of an Addon sensor."""
def __init__(self, hassio, addon, sensor_type):
"""Initialize the Addon sensor."""
self._hassio = hassio
self._addon_slug = addon['slug']
self._name = '{} {} {}'.format(DEFAULT_NAME, addon['name'], SENSOR_NAMES[sensor_type])
self._sensor_type = sensor_type
self._state = None
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return ICON
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def unique_id(self):
"""Return a unique ID for the device."""
return '{}-{}'.format(self._addon_slug, self._sensor_type)
async def async_update(self):
"""Update the state."""
if not self._hassio.is_connected():
self._state = STATE_UNKNOWN
return
info = await self._hassio.get_addon_info(self._addon_slug)
if info[self._sensor_type] is None or info[self._sensor_type] == 'none':
self._state = STATE_UNAVAILABLE
else:
self._state = info[self._sensor_type]
"""
Support for Hass.io switches.
"""
import logging
import voluptuous as vol
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import STATE_UNAVAILABLE, STATE_UNKNOWN
from . import DEFAULT_NAME, DOMAIN as HASSIO_INFO_DOMAIN, HASSIO_DOMAIN
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = [HASSIO_INFO_DOMAIN]
ICON = 'mdi:home-assistant'
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the platform."""
hassio = hass.data[HASSIO_DOMAIN]
info = await hassio.get_supervisor_info()
addons = info['addons']
for addon in addons:
async_add_entities([AddonSwitch(hassio, addon)], True)
class AddonSwitch(SwitchDevice):
"""Representation of an Addon switch."""
def __init__(self, hassio, addon):
self._hassio = hassio
self._addon_slug = addon['slug']
self._name = '{} {}'.format(DEFAULT_NAME, addon['name'])
self._state = STATE_UNKNOWN
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
return ICON
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def is_on(self):
"""Return the boolean response if switch is on."""
return bool(self._state == 'started')
@property
def unique_id(self):
"""Return a unique ID for the device."""
return '{}'.format(self._addon_slug)
async def async_turn_on(self, **kwargs):
"""Turn the entity on."""
await self._hassio.start_addon(self._addon_slug)
async def async_turn_off(self, **kwargs):
"""Turn the entity off."""
await self._hassio.stop_addon(self._addon_slug)
async def async_update(self):
"""Update the state."""
if not self._hassio.is_connected():
self._state = STATE_UNKNOWN
return
info = await self._hassio.get_addon_info(self._addon_slug)
if info['state'] is None or info['state'] == 'none':
self._state = STATE_UNAVAILABLE
else:
self._state = info['state']
@acordill
Copy link

Can this be upgraded to not break home assistant 0.107?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment