mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-13 12:26:50 +00:00
Update code examples
This commit is contained in:
parent
56b2ce7cef
commit
ccbef55136
@ -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 `<config dir>/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 `<config dir>/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
|
||||
```
|
||||
|
||||
|
@ -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 `<config dir>/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
|
||||
```
|
||||
|
||||
|
@ -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
|
||||
```
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user