Last active
November 23, 2021 16:31
-
-
Save balloob/3e8ae00a2354f4e889c0 to your computer and use it in GitHub Desktop.
Example platforms and automation component for Home Assistant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copy this file to <config_dir>/example/sensor.py | |
Add to your configuration.yaml: | |
sensor: | |
platform: example | |
""" | |
from homeassistant.const import TEMP_CELSIUS | |
from homeassistant.helpers.entity import Entity | |
def setup_platform(hass, config, add_devices, discovery_info=None): | |
add_devices([ExampleSensor()]) | |
class ExampleSensor(Entity): | |
@property | |
def name(self): | |
return 'Temperature' | |
@property | |
def state(self): | |
return 23 | |
@property | |
def unit_of_measurement(self): | |
return TEMP_CELSIUS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copy this file to <config_dir>/example/switch.py | |
Add to your configuration.yaml: | |
switch: | |
platform: example | |
file_path: /tmp/HELLO_WORLD | |
""" | |
import os | |
from homeassistant.helpers.entity import ToggleEntity | |
def setup_platform(hass, config, add_devices, discovery_info=None): | |
add_devices([FileSwitch(config['file_path'])]) | |
class FileSwitch(ToggleEntity): | |
def __init__(self, path): | |
self.path = path | |
self.update() | |
@property | |
def name(self): | |
return os.path.basename(self.path) | |
@property | |
def is_on(self): | |
return self._state | |
def turn_on(self, **kwargs): | |
open(self.path, 'a').close() | |
def turn_off(self, **kwargs): | |
os.remove(self.path) | |
def update(self): | |
self._state = os.path.isfile(self.path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copy this file to <config_dir>/xor_automation.py | |
Add to your configuration.yaml: | |
xor_automation: | |
entity_1: swtich.ac | |
entity_2: switch.heater | |
""" | |
from homeassistant.helpers.event import track_state_change | |
from homeassistant.components import is_on, toggle | |
DOMAIN = 'xor_automation' | |
def setup(hass, config): | |
entity_1 = config[DOMAIN]['entity_1'] | |
entity_2 = config[DOMAIN]['entity_2'] | |
def state_changed(entity_id, old_state, new_state): | |
other = entity_2 if entity_id == entity_1 else entity_1 | |
if is_on(hass, entity_id) == is_on(hass, other): | |
toggle(hass, other) | |
# Ensure current state is opposite | |
if is_on(hass, entity_1) == is_on(hass, entity_2): | |
toggle(hass, entity_2) | |
track_state_change(hass, [entity_1, entity_2], state_changed) | |
return True |
The code still works but the format has changed. It now needs to be in a folder. See https://developers.home-assistant.io/docs/creating_integration_file_structure
Thank you for your answer!
So, also the file structure is changed, right? From what I read, I suppose that it is not possible anymore to create a folder with many components inside.
I guess that now it is necessary to create a folder for each gist (e.g., a folder called sensor_example
) and put the content of the gist inside an __init__.py
file. Then, I should add a manifest.json
with a structure similar to this one, right?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I arrived here from your talk at PyCon 2016 linked in Home Assistant documentation!
Are these gists still working?
After modifying
configuration.yaml
as you suggested, should I only copy them insideconfig/custom_components/example
(at least for the firsts two), and execute Home Assistant to see them in action?Because I tried to do so, but I'm not able to correctly execute them.
Thank you for your time!