From ccbef5513655aae654d4cc406cce84fb342df2b2 Mon Sep 17 00:00:00 2001 From: Fabian Affolter Date: Mon, 22 Feb 2016 16:11:39 +0100 Subject: [PATCH] Update code examples --- .../python_component_basic_service.markdown | 12 +++++------ .../python_component_basic_state.markdown | 8 ++++---- .../python_component_mqtt_basic.markdown | 20 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/source/_cookbook/python_component_basic_service.markdown b/source/_cookbook/python_component_basic_service.markdown index 56e95cbcf02..e6f6bd6b367 100644 --- a/source/_cookbook/python_component_basic_service.markdown +++ b/source/_cookbook/python_component_basic_service.markdown @@ -10,20 +10,20 @@ footer: true ha_category: Custom Python Component Examples --- -This is a simple hello world example to show the basics of registering a service. To use this example, create the file `/custom_components/hello_service.py` and copy the below example code. +This is a simple "hello world" example to show the basics of registering a service. To use this example, create the file `/custom_components/hello_service.py` and copy the below example code. -Services can be called from automation and from the service developer tools in the frontend. +Services can be called from automation and from the service "Developer tools" in the frontend. ```python -# The domain of your component. Should be equal to the name of your component -DOMAIN = "hello_service" +# The domain of your component. Should be equal to the name of your component. +DOMAIN = 'hello_service' ATTR_NAME = 'name' DEFAULT_NAME = 'World' def setup(hass, config): - """ Setup is called when Home Assistant is loading our component. """ + """Setup is called when Home Assistant is loading our component.""" def handle_hello(call): name = call.data.get(ATTR_NAME, DEFAULT_NAME) @@ -32,7 +32,7 @@ def setup(hass, config): hass.services.register(DOMAIN, 'hello', handle_hello) - # return boolean to indicate that initialization was successful + # Return boolean to indicate that initialization was successfully. return True ``` diff --git a/source/_cookbook/python_component_basic_state.markdown b/source/_cookbook/python_component_basic_state.markdown index 1b5171ec127..3eca0d8bbab 100644 --- a/source/_cookbook/python_component_basic_state.markdown +++ b/source/_cookbook/python_component_basic_state.markdown @@ -13,15 +13,15 @@ ha_category: Custom Python Component Examples This is a simple hello world example to show the basics for setting a state. To use this example, create the file `/custom_components/hello_state.py` and copy the below example code. ```python -# The domain of your component. Should be equal to the name of your component -DOMAIN = "hello_state" +# The domain of your component. Should be equal to the name of your component. +DOMAIN = 'hello_state' CONF_NAME = 'name' DEFAULT_NAME = 'World' def setup(hass, config): - """ Setup is called when Home Assistant is loading our component. """ + """Setup is called when Home Assistant is loading our component.""" # Get the name from the configuration. Use DEFAULT_NAME if no name provided. name = config[DOMAIN].get(CONF_NAME, DEFAULT_NAME) @@ -29,7 +29,7 @@ def setup(hass, config): # States are in the format DOMAIN.OBJECT_ID hass.states.set('hello_state.hello', name) - # return boolean to indicate that initialization was successful + # Return boolean to indicate that initialization was successfully. return True ``` diff --git a/source/_cookbook/python_component_mqtt_basic.markdown b/source/_cookbook/python_component_mqtt_basic.markdown index 801ba13e4d8..a4f4603dd43 100644 --- a/source/_cookbook/python_component_mqtt_basic.markdown +++ b/source/_cookbook/python_component_mqtt_basic.markdown @@ -21,10 +21,10 @@ This example follows a topic on MQTT and updates the state of an entity to the l ```python 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" -# List of component names (string) your component depends upon +# List of component names (string) your component depends upon. DEPENDENCIES = ['mqtt'] @@ -33,31 +33,31 @@ DEFAULT_TOPIC = 'home-assistant/hello_mqtt' def setup(hass, config): - """ Setup our hello_mqtt component. """ + """Setup the Hello MQTT component.""" mqtt = loader.get_component('mqtt') topic = config[DOMAIN].get('topic', DEFAULT_TOPIC) 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): - """ A new MQTT message has been received. """ + """A new MQTT message has been received.""" hass.states.set(entity_id, payload) - # Subscribe our listener to a topic + # Subscribe our listener to a topic. mqtt.subscribe(hass, topic, message_received) # Set the intial state hass.states.set(entity_id, 'No messages') - # Service to publish a message on MQTT + # Service to publish a message on MQTT. def set_state_service(call): - """ Service to send a message. """ + """Service to send a message.""" mqtt.publish(hass, topic, call.data.get('new_state')) - # Register our service with Home Assistant + # Register our service with Home Assistant. hass.services.register(DOMAIN, 'set_state', set_state_service) - # return boolean to indicate that initialization was successful + # Return boolean to indicate that initialization was successfully. return True ```