mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-11-11 20:10:40 +00:00
Code blocks (#11648)
* Run black on Python code blocks, syntax fixes https://github.com/scop/misc/blob/master/black_markdown.py * Code block language marker fixes * String formatting style tweaks
This commit is contained in:
committed by
Fabian Affolter
parent
0e49f98121
commit
b6a904933b
@@ -41,12 +41,20 @@ import voluptuous as vol
|
||||
import homeassistant.components as core
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.components import device_tracker, light
|
||||
from homeassistant.const import (ATTR_ENTITY_ID, SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON, STATE_HOME, STATE_NOT_HOME,
|
||||
STATE_OFF, STATE_ON)
|
||||
from homeassistant.const import (
|
||||
ATTR_ENTITY_ID,
|
||||
SERVICE_TURN_OFF,
|
||||
SERVICE_TURN_ON,
|
||||
STATE_HOME,
|
||||
STATE_NOT_HOME,
|
||||
STATE_OFF,
|
||||
STATE_ON,
|
||||
)
|
||||
from homeassistant.core import split_entity_id
|
||||
from homeassistant.helpers.event import (async_track_state_change,
|
||||
async_track_time_change)
|
||||
from homeassistant.helpers.event import (
|
||||
async_track_state_change,
|
||||
async_track_time_change,
|
||||
)
|
||||
|
||||
# The domain of your component. Should be equal to the name of your component.
|
||||
DOMAIN = "example"
|
||||
@@ -54,26 +62,26 @@ DOMAIN = "example"
|
||||
# List of integration names (string) your integration depends upon.
|
||||
# We depend on group because group will be loaded after all the integrations that
|
||||
# initialize devices have been setup.
|
||||
DEPENDENCIES = ['group', 'device_tracker', 'light']
|
||||
DEPENDENCIES = ["group", "device_tracker", "light"]
|
||||
|
||||
# Configuration key for the entity id we are targeting.
|
||||
CONF_TARGET = 'target'
|
||||
CONF_TARGET = "target"
|
||||
|
||||
# Variable for storing configuration parameters.
|
||||
TARGET_ID = None
|
||||
|
||||
# Name of the service that we expose.
|
||||
SERVICE_FLASH = 'flash'
|
||||
SERVICE_FLASH = "flash"
|
||||
|
||||
# Shortcut for the logger
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
# Validate that all required config options are given.
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Optional(CONF_TARGET): cv.entity_id
|
||||
})
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{DOMAIN: vol.Schema({vol.Optional(CONF_TARGET): cv.entity_id})},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup(hass, config):
|
||||
"""Setup example component."""
|
||||
@@ -84,8 +92,7 @@ async def async_setup(hass, config):
|
||||
|
||||
# Validate that the target entity id exists.
|
||||
if hass.states.get(TARGET_ID) is None:
|
||||
_LOGGER.error("Target entity id %s does not exist",
|
||||
TARGET_ID)
|
||||
_LOGGER.error("Target entity id %s does not exist", TARGET_ID)
|
||||
|
||||
# Tell the bootstrapper that we failed to initialize and clear the
|
||||
# stored target id so our functions don't run.
|
||||
@@ -120,7 +127,7 @@ async def async_setup(hass, config):
|
||||
return
|
||||
|
||||
if device_tracker.is_on(hass) and not core.is_on(hass, TARGET_ID):
|
||||
_LOGGER.info('People home at 7AM, turning target on')
|
||||
_LOGGER.info("People home at 7AM, turning target on")
|
||||
await hass.services.async_call(domain, SERVICE_TURN_ON, data)
|
||||
|
||||
async def async_flash_service(service):
|
||||
@@ -133,7 +140,9 @@ async def async_setup(hass, config):
|
||||
|
||||
if core.is_on(hass, TARGET_ID):
|
||||
# We need this call to run blocking, as we want to wait 10s after it finished
|
||||
await hass.services.async_call(domain, SERVICE_TURN_OFF, data, blocking=True)
|
||||
await hass.services.async_call(
|
||||
domain, SERVICE_TURN_OFF, data, blocking=True
|
||||
)
|
||||
time.sleep(10)
|
||||
await hass.services.async_call(domain, SERVICE_TURN_ON, data)
|
||||
else:
|
||||
@@ -146,15 +155,26 @@ async def async_setup(hass, config):
|
||||
|
||||
# If all lights turn off, turn off.
|
||||
async_track_state_change(
|
||||
hass, light.ENTITY_ID_ALL_LIGHTS, async_switch_off, STATE_ON, STATE_OFF)
|
||||
hass, light.ENTITY_ID_ALL_LIGHTS, async_switch_off, STATE_ON, STATE_OFF
|
||||
)
|
||||
|
||||
# If all people leave the house and the entity is on, turn it off.
|
||||
async_track_state_change(
|
||||
hass, device_tracker.ENTITY_ID_ALL_DEVICES, async_switch_off, STATE_HOME, STATE_NOT_HOME)
|
||||
hass,
|
||||
device_tracker.ENTITY_ID_ALL_DEVICES,
|
||||
async_switch_off,
|
||||
STATE_HOME,
|
||||
STATE_NOT_HOME,
|
||||
)
|
||||
|
||||
# If anyone comes home and the entity is not on, turn it on.
|
||||
async_track_state_change(
|
||||
hass, device_tracker.ENTITY_ID_ALL_DEVICES, async_switch_on, STATE_NOT_HOME, STATE_HOME)
|
||||
hass,
|
||||
device_tracker.ENTITY_ID_ALL_DEVICES,
|
||||
async_switch_on,
|
||||
STATE_NOT_HOME,
|
||||
STATE_HOME,
|
||||
)
|
||||
|
||||
# Call wakeup callback at 7 AM
|
||||
async_track_time_change(hass, async_wake_up, hour=7, minute=00, second=00)
|
||||
|
||||
Reference in New Issue
Block a user