Merge pull request #1343 from balloob/random-cleanup

Random cleanup
This commit is contained in:
Paulus Schoutsen 2016-02-20 12:16:54 -08:00
commit 6532eae3d5
14 changed files with 231 additions and 113 deletions

View file

@ -1,13 +1,4 @@
"""
homeassistant.bootstrap
~~~~~~~~~~~~~~~~~~~~~~~
Provides methods to bootstrap a home assistant instance.
Each method will return a tuple (bus, statemachine).
After bootstrapping you can add your own components or
start by calling homeassistant.start_home_assistant(bus)
"""
"""Provides methods to bootstrap a home assistant instance."""
import logging
import logging.handlers
@ -15,6 +6,7 @@ import os
import shutil
import sys
from collections import defaultdict
from threading import RLock
import homeassistant.components as core_components
import homeassistant.components.group as group
@ -32,6 +24,8 @@ from homeassistant.helpers import event_decorators, service
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
_SETUP_LOCK = RLock()
_CURRENT_SETUP = []
ATTR_COMPONENT = 'component'
@ -78,42 +72,57 @@ def _handle_requirements(hass, component, name):
def _setup_component(hass, domain, config):
""" Setup a component for Home Assistant. """
"""Setup a component for Home Assistant."""
# pylint: disable=too-many-return-statements
if domain in hass.config.components:
return True
component = loader.get_component(domain)
missing_deps = [dep for dep in getattr(component, 'DEPENDENCIES', [])
if dep not in hass.config.components]
with _SETUP_LOCK:
# It might have been loaded while waiting for lock
if domain in hass.config.components:
return True
if missing_deps:
_LOGGER.error(
'Not initializing %s because not all dependencies loaded: %s',
domain, ", ".join(missing_deps))
return False
if not _handle_requirements(hass, component, domain):
return False
try:
if not component.setup(hass, config):
_LOGGER.error('component %s failed to initialize', domain)
if domain in _CURRENT_SETUP:
_LOGGER.error('Attempt made to setup %s during setup of %s',
domain, domain)
return False
except Exception: # pylint: disable=broad-except
_LOGGER.exception('Error during setup of component %s', domain)
return False
hass.config.components.append(component.DOMAIN)
component = loader.get_component(domain)
missing_deps = [dep for dep in getattr(component, 'DEPENDENCIES', [])
if dep not in hass.config.components]
# Assumption: if a component does not depend on groups
# it communicates with devices
if group.DOMAIN not in getattr(component, 'DEPENDENCIES', []):
hass.pool.add_worker()
if missing_deps:
_LOGGER.error(
'Not initializing %s because not all dependencies loaded: %s',
domain, ", ".join(missing_deps))
return False
hass.bus.fire(
EVENT_COMPONENT_LOADED, {ATTR_COMPONENT: component.DOMAIN})
if not _handle_requirements(hass, component, domain):
return False
return True
_CURRENT_SETUP.append(domain)
try:
if not component.setup(hass, config):
_LOGGER.error('component %s failed to initialize', domain)
return False
except Exception: # pylint: disable=broad-except
_LOGGER.exception('Error during setup of component %s', domain)
return False
finally:
_CURRENT_SETUP.remove(domain)
hass.config.components.append(component.DOMAIN)
# Assumption: if a component does not depend on groups
# it communicates with devices
if group.DOMAIN not in getattr(component, 'DEPENDENCIES', []):
hass.pool.add_worker()
hass.bus.fire(
EVENT_COMPONENT_LOADED, {ATTR_COMPONENT: component.DOMAIN})
return True
def prepare_setup_platform(hass, config, domain, platform_name):

View file

@ -12,7 +12,7 @@ import logging
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity import Entity
from homeassistant.const import (STATE_ON, STATE_OFF)
from homeassistant.components import (mysensors, )
from homeassistant.components import (bloomsky, mysensors)
DOMAIN = 'binary_sensor'
SCAN_INTERVAL = 30
@ -32,6 +32,7 @@ SENSOR_CLASSES = [
# Maps discovered services to their platforms
DISCOVERY_PLATFORMS = {
bloomsky.DISCOVER_BINARY_SENSORS: 'bloomsky',
mysensors.DISCOVER_BINARY_SENSORS: 'mysensors',
}
@ -61,19 +62,17 @@ class BinarySensorDevice(Entity):
"""Return the state of the binary sensor."""
return STATE_ON if self.is_on else STATE_OFF
@property
def friendly_state(self):
"""Return the friendly state of the binary sensor."""
return None
@property
def sensor_class(self):
"""Return the class of this sensor, from SENSOR_CASSES."""
"""Return the class of this sensor, from SENSOR_CLASSES."""
return None
@property
def state_attributes(self):
"""Return device specific state attributes."""
return {
'sensor_class': self.sensor_class,
}
attr = {}
if self.sensor_class is not None:
attr['sensor_class'] = self.sensor_class
return attr

View file

@ -0,0 +1,74 @@
"""
Support the binary sensors of a BloomSky weather station.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.bloomsky/
"""
import logging
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.loader import get_component
DEPENDENCIES = ["bloomsky"]
# These are the available sensors mapped to binary_sensor class
SENSOR_TYPES = {
"Rain": "moisture",
"Night": None,
}
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the available BloomSky weather binary sensors."""
logger = logging.getLogger(__name__)
bloomsky = get_component('bloomsky')
sensors = config.get('monitored_conditions', SENSOR_TYPES)
for device in bloomsky.BLOOMSKY.devices.values():
for variable in sensors:
if variable in SENSOR_TYPES:
add_devices([BloomSkySensor(bloomsky.BLOOMSKY,
device,
variable)])
else:
logger.error("Cannot find definition for device: %s", variable)
class BloomSkySensor(BinarySensorDevice):
""" Represents a single binary sensor in a BloomSky device. """
def __init__(self, bs, device, sensor_name):
"""Initialize a bloomsky binary sensor."""
self._bloomsky = bs
self._device_id = device["DeviceID"]
self._sensor_name = sensor_name
self._name = "{} {}".format(device["DeviceName"], sensor_name)
self._unique_id = "bloomsky_binary_sensor {}".format(self._name)
self.update()
@property
def name(self):
"""The name of the BloomSky device and this sensor."""
return self._name
@property
def unique_id(self):
"""Unique ID for this sensor."""
return self._unique_id
@property
def sensor_class(self):
"""Return the class of this sensor, from SENSOR_CLASSES."""
return SENSOR_TYPES.get(self._sensor_name)
@property
def is_on(self):
"""If binary sensor is on."""
return self._state
def update(self):
"""Request an update from the BloomSky API."""
self._bloomsky.refresh_devices()
self._state = \
self._bloomsky.devices[self._device_id]["Data"][self._sensor_name]

View file

@ -11,6 +11,7 @@ from datetime import timedelta
import requests
from homeassistant.components import discovery
from homeassistant.const import CONF_API_KEY
from homeassistant.helpers import validate_config
from homeassistant.util import Throttle
@ -24,6 +25,10 @@ _LOGGER = logging.getLogger(__name__)
# no point in polling the API more frequently
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=300)
DISCOVER_SENSORS = 'bloomsky.sensors'
DISCOVER_BINARY_SENSORS = 'bloomsky.binary_sensor'
DISCOVER_CAMERAS = 'bloomsky.camera'
# pylint: disable=unused-argument,too-few-public-methods
def setup(hass, config):
@ -42,6 +47,12 @@ def setup(hass, config):
except RuntimeError:
return False
for component, discovery_service in (
('camera', DISCOVER_CAMERAS), ('sensor', DISCOVER_SENSORS),
('binary_sensor', DISCOVER_BINARY_SENSORS)):
discovery.discover(hass, discovery_service, component=component,
hass_config=config)
return True

View file

@ -13,6 +13,7 @@ import requests
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.components import bloomsky
from homeassistant.const import (
ATTR_ENTITY_PICTURE,
HTTP_NOT_FOUND,
@ -26,7 +27,9 @@ SCAN_INTERVAL = 30
ENTITY_ID_FORMAT = DOMAIN + '.{}'
# Maps discovered services to their platforms
DISCOVERY_PLATFORMS = {}
DISCOVERY_PLATFORMS = {
bloomsky.DISCOVER_CAMERAS: 'bloomsky',
}
STATE_RECORDING = 'recording'
STATE_STREAMING = 'streaming'

View file

@ -1,6 +1,4 @@
"""
homeassistant.components.discovery
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Starts a service to scan in intervals for new devices.
Will emit EVENT_PLATFORM_DISCOVERED whenever a new service has been discovered.
@ -39,24 +37,41 @@ SERVICE_HANDLERS = {
def listen(hass, service, callback):
"""
Setup listener for discovery of specific service.
"""Setup listener for discovery of specific service.
Service can be a string or a list/tuple.
"""
if isinstance(service, str):
service = (service,)
else:
service = tuple(service)
def discovery_event_listener(event):
""" Listens for discovery events. """
"""Listen for discovery events."""
if event.data[ATTR_SERVICE] in service:
callback(event.data[ATTR_SERVICE], event.data[ATTR_DISCOVERED])
callback(event.data[ATTR_SERVICE], event.data.get(ATTR_DISCOVERED))
hass.bus.listen(EVENT_PLATFORM_DISCOVERED, discovery_event_listener)
def discover(hass, service, discovered=None, component=None, hass_config=None):
"""Fire discovery event.
Can ensure a component is loaded.
"""
if component is not None:
bootstrap.setup_component(hass, component, hass_config)
data = {
ATTR_SERVICE: service
}
if discovered is not None:
data[ATTR_DISCOVERED] = discovered
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, data)
def setup(hass, config):
""" Starts a discovery service. """
logger = logging.getLogger(__name__)

View file

@ -1,2 +1,2 @@
""" DO NOT MODIFY. Auto-generated by build_frontend script """
VERSION = "4ae370eaaad6bc779d08713b79ce3cba"
VERSION = "72a593fc8887c08d6f4ad9bd483bd232"

File diff suppressed because one or more lines are too long

@ -1 +1 @@
Subproject commit 4cdefac2fe6f016fa09d872c8cb062ba01442b08
Subproject commit 40ff847f2d0670367a2fd29e3340a4f94ab1ff49

View file

@ -10,7 +10,7 @@ import logging
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.components import (
wink, zwave, isy994, verisure, ecobee, tellduslive, mysensors)
wink, zwave, isy994, verisure, ecobee, tellduslive, mysensors, bloomsky)
DOMAIN = 'sensor'
SCAN_INTERVAL = 30
@ -19,6 +19,7 @@ ENTITY_ID_FORMAT = DOMAIN + '.{}'
# Maps discovered services to their platforms
DISCOVERY_PLATFORMS = {
bloomsky.DISCOVER_SENSORS: 'bloomsky',
wink.DISCOVER_SENSORS: 'wink',
zwave.DISCOVER_SENSORS: 'zwave',
isy994.DISCOVER_SENSORS: 'isy994',

View file

@ -1,6 +1,4 @@
"""
homeassistant.components.sensor.bloomsky
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Support the sensor of a BloomSky weather station.
For more details about this component, please refer to the documentation at
@ -8,6 +6,7 @@ https://home-assistant.io/components/sensor.bloomsky/
"""
import logging
from homeassistant.const import TEMP_FAHRENHEIT
from homeassistant.helpers.entity import Entity
from homeassistant.loader import get_component
@ -16,14 +15,12 @@ DEPENDENCIES = ["bloomsky"]
# These are the available sensors
SENSOR_TYPES = ["Temperature",
"Humidity",
"Rain",
"Pressure",
"Luminance",
"Night",
"UVIndex"]
# Sensor units - these do not currently align with the API documentation
SENSOR_UNITS = {"Temperature": "°F",
SENSOR_UNITS = {"Temperature": TEMP_FAHRENHEIT,
"Humidity": "%",
"Pressure": "inHg",
"Luminance": "cd/m²"}
@ -34,14 +31,13 @@ FORMAT_NUMBERS = ["Temperature", "Pressure"]
# pylint: disable=unused-argument
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Set up the available BloomSky weather sensors. """
"""Set up the available BloomSky weather sensors."""
logger = logging.getLogger(__name__)
bloomsky = get_component('bloomsky')
sensors = config.get('monitored_conditions', SENSOR_TYPES)
for device_key in bloomsky.BLOOMSKY.devices:
device = bloomsky.BLOOMSKY.devices[device_key]
for variable in config["monitored_conditions"]:
for device in bloomsky.BLOOMSKY.devices.values():
for variable in sensors:
if variable in SENSOR_TYPES:
add_devices([BloomSkySensor(bloomsky.BLOOMSKY,
device,
@ -51,56 +47,45 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
class BloomSkySensor(Entity):
""" Represents a single sensor in a BloomSky device. """
"""Represents a single sensor in a BloomSky device."""
def __init__(self, bs, device, sensor_name):
"""Initialize a bloomsky sensor."""
self._bloomsky = bs
self._device_id = device["DeviceID"]
self._client_name = device["DeviceName"]
self._sensor_name = sensor_name
self._state = self.process_state(device)
self._sensor_update = ""
self._name = "{} {}".format(device["DeviceName"], sensor_name)
self._unique_id = "bloomsky_sensor {}".format(self._name)
self.update()
@property
def name(self):
""" The name of the BloomSky device and this sensor. """
return "{} {}".format(self._client_name, self._sensor_name)
"""The name of the BloomSky device and this sensor."""
return self._name
@property
def unique_id(self):
"""Unique ID for this sensor."""
return self._unique_id
@property
def state(self):
""" The current state (i.e. value) of this sensor. """
"""The current state (i.e. value) of this sensor."""
return self._state
@property
def unit_of_measurement(self):
""" This sensor's units. """
"""Return the sensor units."""
return SENSOR_UNITS.get(self._sensor_name, None)
def update(self):
""" Request an update from the BloomSky API. """
"""Request an update from the BloomSky API."""
self._bloomsky.refresh_devices()
# TS is a Unix epoch timestamp for the last time the BloomSky servers
# heard from this device. If that value hasn't changed, the value has
# not been updated.
last_ts = self._bloomsky.devices[self._device_id]["Data"]["TS"]
if last_ts != self._sensor_update:
self.process_state(self._bloomsky.devices[self._device_id])
self._sensor_update = last_ts
def process_state(self, device):
""" Handle the response from the BloomSky API for this sensor. """
data = device["Data"][self._sensor_name]
if self._sensor_name == "Rain":
if data:
self._state = "Raining"
else:
self._state = "Not raining"
elif self._sensor_name == "Night":
if data:
self._state = "Nighttime"
else:
self._state = "Daytime"
elif self._sensor_name in FORMAT_NUMBERS:
self._state = "{0:.2f}".format(data)
state = \
self._bloomsky.devices[self._device_id]["Data"][self._sensor_name]
if self._sensor_name in FORMAT_NUMBERS:
self._state = "{0:.2f}".format(state)
else:
self._state = data
self._state = state

View file

@ -146,7 +146,7 @@ class MockModule(object):
self.DEPENDENCIES = dependencies
# Setup a mock setup if none given.
if setup is None:
self.setup = lambda hass, config: False
self.setup = lambda hass, config: True
else:
self.setup = setup

View file

@ -31,8 +31,7 @@ class TestBinarySensor(unittest.TestCase):
def test_attributes(self):
"""Test binary sensor attributes."""
sensor = binary_sensor.BinarySensorDevice()
self.assertEqual({'sensor_class': None},
sensor.state_attributes)
self.assertEqual({}, sensor.state_attributes)
with mock.patch('homeassistant.components.binary_sensor.'
'BinarySensorDevice.sensor_class',
new='motion'):

View file

@ -9,13 +9,13 @@ import os
import tempfile
import unittest
from homeassistant import bootstrap
from homeassistant import bootstrap, loader
from homeassistant.const import (__version__, CONF_LATITUDE, CONF_LONGITUDE,
CONF_NAME, CONF_CUSTOMIZE)
import homeassistant.util.dt as dt_util
from homeassistant.helpers.entity import Entity
from tests.common import get_test_home_assistant
from tests.common import get_test_home_assistant, MockModule
class TestBootstrap(unittest.TestCase):
@ -61,6 +61,7 @@ class TestBootstrap(unittest.TestCase):
self.assertTrue(os.path.isfile(check_file))
bootstrap.process_ha_config_upgrade(hass)
self.assertFalse(os.path.isfile(check_file))
hass.stop()
def test_not_remove_lib_if_not_upgrade(self):
with tempfile.TemporaryDirectory() as config_dir:
@ -82,6 +83,7 @@ class TestBootstrap(unittest.TestCase):
bootstrap.process_ha_config_upgrade(hass)
self.assertTrue(os.path.isfile(check_file))
hass.stop()
def test_entity_customization(self):
""" Test entity customization through config """
@ -102,3 +104,19 @@ class TestBootstrap(unittest.TestCase):
state = hass.states.get('test.test')
self.assertTrue(state.attributes['hidden'])
hass.stop()
def test_handle_setup_circular_dependency(self):
hass = get_test_home_assistant()
loader.set_component('comp_b', MockModule('comp_b', ['comp_a']))
def setup_a(hass, config):
bootstrap.setup_component(hass, 'comp_b')
return True
loader.set_component('comp_a', MockModule('comp_a', setup=setup_a))
bootstrap.setup_component(hass, 'comp_a')
self.assertEqual(['comp_a'], hass.config.components)
hass.stop()