mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-20 15:56:51 +00:00
Update code
This commit is contained in:
parent
3bef0ad30f
commit
07112d8772
@ -23,7 +23,7 @@ DEFAULT_NAME = 'World'
|
|||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Setup is called when Home Assistant is loading our component."""
|
"""Set up is called when Home Assistant is loading our component."""
|
||||||
|
|
||||||
def handle_hello(call):
|
def handle_hello(call):
|
||||||
name = call.data.get(ATTR_NAME, DEFAULT_NAME)
|
name = call.data.get(ATTR_NAME, DEFAULT_NAME)
|
||||||
|
@ -82,7 +82,7 @@ CONF_TEXT = 'text'
|
|||||||
DEFAULT_TEXT = 'No text!'
|
DEFAULT_TEXT = 'No text!'
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Setup the Hello State component. """
|
"""Set up the Hello State component. """
|
||||||
# Get the text from the configuration. Use DEFAULT_TEXT if no name is provided.
|
# Get the text from the configuration. Use DEFAULT_TEXT if no name is provided.
|
||||||
text = config[DOMAIN].get(CONF_TEXT, DEFAULT_TEXT)
|
text = config[DOMAIN].get(CONF_TEXT, DEFAULT_TEXT)
|
||||||
|
|
||||||
|
@ -22,7 +22,7 @@ This example follows a topic on MQTT and updates the state of an entity to the l
|
|||||||
import homeassistant.loader as loader
|
import homeassistant.loader as loader
|
||||||
|
|
||||||
# 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 = "hello_mqtt"
|
DOMAIN = 'hello_mqtt'
|
||||||
|
|
||||||
# List of component names (string) your component depends upon.
|
# List of component names (string) your component depends upon.
|
||||||
DEPENDENCIES = ['mqtt']
|
DEPENDENCIES = ['mqtt']
|
||||||
@ -33,14 +33,14 @@ DEFAULT_TOPIC = 'home-assistant/hello_mqtt'
|
|||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Setup the Hello MQTT component."""
|
"""Set up the Hello MQTT component."""
|
||||||
mqtt = loader.get_component('mqtt')
|
mqtt = loader.get_component('mqtt')
|
||||||
topic = config[DOMAIN].get('topic', DEFAULT_TOPIC)
|
topic = config[DOMAIN].get('topic', DEFAULT_TOPIC)
|
||||||
entity_id = 'hello_mqtt.last_message'
|
entity_id = 'hello_mqtt.last_message'
|
||||||
|
|
||||||
# Listener to be called when we receive a message.
|
# Listener to be called when we receive a message.
|
||||||
def message_received(topic, payload, qos):
|
def message_received(topic, payload, qos):
|
||||||
"""A new MQTT message has been received."""
|
"""Handle new MQTT messages."""
|
||||||
hass.states.set(entity_id, payload)
|
hass.states.set(entity_id, payload)
|
||||||
|
|
||||||
# Subscribe our listener to a topic.
|
# Subscribe our listener to a topic.
|
||||||
|
@ -39,35 +39,35 @@ from homeassistant.components import device_tracker, light, notify
|
|||||||
from homeassistant.helpers.event import track_state_change
|
from homeassistant.helpers.event import track_state_change
|
||||||
from homeassistant.const import STATE_ON, STATE_OFF, STATE_HOME, STATE_NOT_HOME
|
from homeassistant.const import STATE_ON, STATE_OFF, STATE_HOME, STATE_NOT_HOME
|
||||||
|
|
||||||
DOMAIN = "simple_alarm"
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
DOMAIN = 'simple_alarm"'
|
||||||
|
|
||||||
DEPENDENCIES = ['group', 'device_tracker', 'light']
|
DEPENDENCIES = ['group', 'device_tracker', 'light']
|
||||||
|
|
||||||
# Attribute to tell which light has to flash when a known person comes home
|
# Attribute to tell which light has to flash when a known person comes home
|
||||||
# If omitted will flash all.
|
# If omitted will flash all.
|
||||||
CONF_KNOWN_LIGHT = "known_light"
|
CONF_KNOWN_LIGHT = 'known_light'
|
||||||
|
|
||||||
# Attribute to tell which light has to flash when an unknown person comes home
|
# Attribute to tell which light has to flash when an unknown person comes home
|
||||||
# If omitted will flash all.
|
# If omitted will flash all.
|
||||||
CONF_UNKNOWN_LIGHT = "unknown_light"
|
CONF_UNKNOWN_LIGHT = 'unknown_light'
|
||||||
|
|
||||||
# Services to test the alarms
|
# Services to test the alarms
|
||||||
SERVICE_TEST_KNOWN_ALARM = "test_known"
|
SERVICE_TEST_KNOWN_ALARM = 'test_known'
|
||||||
SERVICE_TEST_UNKNOWN_ALARM = "test_unknown"
|
SERVICE_TEST_UNKNOWN_ALARM = 'test_unknown'
|
||||||
|
|
||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
""" Sets up the simple alarms. """
|
"""Set up the simple alarms."""
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
light_ids = []
|
light_ids = []
|
||||||
|
|
||||||
for conf_key in (CONF_KNOWN_LIGHT, CONF_UNKNOWN_LIGHT):
|
for conf_key in (CONF_KNOWN_LIGHT, CONF_UNKNOWN_LIGHT):
|
||||||
light_id = config[DOMAIN].get(conf_key, light.ENTITY_ID_ALL_LIGHTS)
|
light_id = config[DOMAIN].get(conf_key, light.ENTITY_ID_ALL_LIGHTS)
|
||||||
|
|
||||||
if hass.states.get(light_id) is None:
|
if hass.states.get(light_id) is None:
|
||||||
logger.error(
|
_LOGGER.error(
|
||||||
'Light id %s could not be found in state machine', light_id)
|
"Light id %s could not be found in state machine", light_id)
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ def setup(hass, config):
|
|||||||
known_light_id, unknown_light_id = light_ids
|
known_light_id, unknown_light_id = light_ids
|
||||||
|
|
||||||
if hass.states.get(device_tracker.ENTITY_ID_ALL_DEVICES) is None:
|
if hass.states.get(device_tracker.ENTITY_ID_ALL_DEVICES) is None:
|
||||||
logger.error('No devices are being tracked, cannot setup alarm')
|
_LOGGER.error("No devices are being tracked, cannot setup alarm")
|
||||||
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -102,7 +102,7 @@ def setup(hass, config):
|
|||||||
DOMAIN, SERVICE_TEST_UNKNOWN_ALARM, lambda call: unknown_alarm())
|
DOMAIN, SERVICE_TEST_UNKNOWN_ALARM, lambda call: unknown_alarm())
|
||||||
|
|
||||||
def unknown_alarm_if_lights_on(entity_id, old_state, new_state):
|
def unknown_alarm_if_lights_on(entity_id, old_state, new_state):
|
||||||
""" Called when a light has been turned on. """
|
"""Called when a light has been turned on."""
|
||||||
if not device_tracker.is_on(hass):
|
if not device_tracker.is_on(hass):
|
||||||
unknown_alarm()
|
unknown_alarm()
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ def setup(hass, config):
|
|||||||
unknown_alarm_if_lights_on, STATE_OFF, STATE_ON)
|
unknown_alarm_if_lights_on, STATE_OFF, STATE_ON)
|
||||||
|
|
||||||
def ring_known_alarm(entity_id, old_state, new_state):
|
def ring_known_alarm(entity_id, old_state, new_state):
|
||||||
""" Called when a known person comes home. """
|
"""Called when a known person comes home."""
|
||||||
if light.is_on(hass, known_light_id):
|
if light.is_on(hass, known_light_id):
|
||||||
known_alarm()
|
known_alarm()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user