mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-27 19:26:50 +00:00
Updated example component (#6772)
This commit is contained in:
parent
1136cb2a2f
commit
0fc2f0da0a
@ -26,28 +26,33 @@ example:
|
|||||||
target: TARGET_ENTITY
|
target: TARGET_ENTITY
|
||||||
```
|
```
|
||||||
|
|
||||||
Configuration variables:
|
{% configuration %}
|
||||||
|
target:
|
||||||
- **target** (*Required*): TARGET_ENTITY should be one of your devices that can be turned on and off, e.g., a light or a switch. Example value could be light.Ceiling or switch.AC (if you have these devices with those names).
|
description: TARGET_ENTITY should be one of your devices that can be turned on and off, e.g., a light or a switch. Example value could be light.Ceiling or switch.AC (if you have these devices with those names).
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
{% endconfiguration %}
|
||||||
|
|
||||||
Create the file `<config dir>/custom_components/example.py` and copy paste the content below:
|
Create the file `<config dir>/custom_components/example.py` and copy paste the content below:
|
||||||
|
|
||||||
|
|
||||||
```python
|
```python
|
||||||
"""
|
"""
|
||||||
Example of a custom component.
|
Example of a custom component.
|
||||||
"""
|
"""
|
||||||
import time
|
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import STATE_HOME, STATE_NOT_HOME, STATE_ON, STATE_OFF
|
|
||||||
from homeassistant.helpers import validate_config
|
|
||||||
from homeassistant.helpers.event_decorators import \
|
|
||||||
track_state_change, track_time_change
|
|
||||||
from homeassistant.helpers.service import service
|
|
||||||
import homeassistant.components as core
|
import homeassistant.components as core
|
||||||
from homeassistant.components import device_tracker
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components import light
|
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.core import split_entity_id
|
||||||
|
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.
|
# The domain of your component. Should be equal to the name of your component.
|
||||||
DOMAIN = "example"
|
DOMAIN = "example"
|
||||||
@ -69,17 +74,20 @@ SERVICE_FLASH = 'flash'
|
|||||||
# Shortcut for the logger
|
# Shortcut for the logger
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
|
||||||
"""Setup example component."""
|
|
||||||
global TARGET_ID
|
|
||||||
|
|
||||||
# Validate that all required config options are given.
|
# Validate that all required config options are given.
|
||||||
if not validate_config(config, {DOMAIN: [CONF_TARGET]}, _LOGGER):
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
return False
|
DOMAIN: vol.Schema({
|
||||||
|
vol.Optional(CONF_TARGET): cv.entity_id
|
||||||
|
})
|
||||||
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
|
async def async_setup(hass, config):
|
||||||
|
"""Setup example component."""
|
||||||
TARGET_ID = config[DOMAIN][CONF_TARGET]
|
TARGET_ID = config[DOMAIN][CONF_TARGET]
|
||||||
|
|
||||||
|
domain = split_entity_id(TARGET_ID)[0]
|
||||||
|
data = {ATTR_ENTITY_ID: TARGET_ID}
|
||||||
|
|
||||||
# Validate that the target entity id exists.
|
# Validate that the target entity id exists.
|
||||||
if hass.states.get(TARGET_ID) is None:
|
if hass.states.get(TARGET_ID) is None:
|
||||||
_LOGGER.error("Target entity id %s does not exist",
|
_LOGGER.error("Target entity id %s does not exist",
|
||||||
@ -90,57 +98,39 @@ def setup(hass, config):
|
|||||||
TARGET_ID = None
|
TARGET_ID = None
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Tell the bootstrapper that we initialized successfully.
|
async def async_switch_on(entity_id, old_state, new_state):
|
||||||
return True
|
"""Callback to turn on our target entity"""
|
||||||
|
|
||||||
|
|
||||||
@track_state_change(device_tracker.ENTITY_ID_ALL_DEVICES)
|
|
||||||
def track_devices(hass, entity_id, old_state, new_state):
|
|
||||||
"""Called when the group.all devices change state."""
|
|
||||||
# If the target id is not set, return
|
# If the target id is not set, return
|
||||||
if not TARGET_ID:
|
if not TARGET_ID:
|
||||||
return
|
return
|
||||||
|
|
||||||
# If anyone comes home and the entity is not on, turn it on.
|
if not core.is_on(hass, TARGET_ID):
|
||||||
if new_state.state == STATE_HOME and not core.is_on(hass, TARGET_ID):
|
await hass.services.async_call(domain, SERVICE_TURN_ON, data)
|
||||||
|
|
||||||
core.turn_on(hass, TARGET_ID)
|
async def async_switch_off(entity_id, old_state, new_state):
|
||||||
|
"""Callback to turn off our target entity"""
|
||||||
|
# If the target id is not set, return
|
||||||
|
if not TARGET_ID:
|
||||||
|
return
|
||||||
|
|
||||||
# If all people leave the house and the entity is on, turn it off.
|
if core.is_on(hass, TARGET_ID):
|
||||||
elif new_state.state == STATE_NOT_HOME and core.is_on(hass, TARGET_ID):
|
await hass.services.async_call(domain, SERVICE_TURN_OFF, data)
|
||||||
|
|
||||||
core.turn_off(hass, TARGET_ID)
|
async def async_wake_up(service):
|
||||||
|
|
||||||
|
|
||||||
@track_time_change(hour=7, minute=0, second=0)
|
|
||||||
def wake_up(hass, now):
|
|
||||||
"""Turn light on in the morning.
|
"""Turn light on in the morning.
|
||||||
|
|
||||||
Turn the light on at 7 AM if there are people home and it is not already
|
Turn the light on when called, but only if there are people home
|
||||||
on.
|
and it is not already on.
|
||||||
"""
|
"""
|
||||||
if not TARGET_ID:
|
if not TARGET_ID:
|
||||||
return
|
return
|
||||||
|
|
||||||
if device_tracker.is_on(hass) and not core.is_on(hass, TARGET_ID):
|
if device_tracker.is_on(hass) and not core.is_on(hass, TARGET_ID):
|
||||||
_LOGGER.info('People home at 7AM, turning it on')
|
_LOGGER.info('People home at 7AM, turning target on')
|
||||||
core.turn_on(hass, TARGET_ID)
|
await hass.services.async_call(domain, SERVICE_TURN_ON, data)
|
||||||
|
|
||||||
|
async def async_flash_service(service):
|
||||||
@track_state_change(light.ENTITY_ID_ALL_LIGHTS, STATE_ON, STATE_OFF)
|
"""Service callback that will toggle the target.
|
||||||
def all_lights_off(hass, entity_id, old_state, new_state):
|
|
||||||
"""If all lights turn off, turn off."""
|
|
||||||
if not TARGET_ID:
|
|
||||||
return
|
|
||||||
|
|
||||||
if core.is_on(hass, TARGET_ID):
|
|
||||||
_LOGGER.info('All lights have been turned off, turning it off')
|
|
||||||
core.turn_off(hass, TARGET_ID)
|
|
||||||
|
|
||||||
|
|
||||||
@service(DOMAIN, SERVICE_FLASH)
|
|
||||||
def flash_service(hass, call):
|
|
||||||
"""Service that will toggle the target.
|
|
||||||
|
|
||||||
Set the light to off for 10 seconds if on and vice versa.
|
Set the light to off for 10 seconds if on and vice versa.
|
||||||
"""
|
"""
|
||||||
@ -148,12 +138,33 @@ def flash_service(hass, call):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if core.is_on(hass, TARGET_ID):
|
if core.is_on(hass, TARGET_ID):
|
||||||
core.turn_off(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)
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
core.turn_on(hass, TARGET_ID)
|
await hass.services.async_call(domain, SERVICE_TURN_ON, data)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
core.turn_on(hass, TARGET_ID)
|
await hass.services.async_call(domain, SERVICE_TURN_ON, data, blocking=True)
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
core.turn_off(hass, TARGET_ID)
|
await hass.services.async_call(domain, SERVICE_TURN_OFF, data)
|
||||||
|
|
||||||
|
# register the example.flash service
|
||||||
|
hass.services.async_register(DOMAIN, SERVICE_FLASH, async_flash_service)
|
||||||
|
|
||||||
|
# If all lights turn off, turn off.
|
||||||
|
async_track_state_change(
|
||||||
|
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)
|
||||||
|
|
||||||
|
# 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)
|
||||||
|
|
||||||
|
# Call wakeup callback at 7 AM
|
||||||
|
async_track_time_change(hass, async_wake_up, hour=7, minute=00, second=00)
|
||||||
|
|
||||||
|
# Tell the bootstrapper that we initialized successfully.
|
||||||
|
return True
|
||||||
```
|
```
|
||||||
|
Loading…
x
Reference in New Issue
Block a user