Skip to content

Instantly share code, notes, and snippets.

@balloob
Last active November 23, 2021 16:31
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balloob/3e8ae00a2354f4e889c0 to your computer and use it in GitHub Desktop.
Save balloob/3e8ae00a2354f4e889c0 to your computer and use it in GitHub Desktop.
Example platforms and automation component for Home Assistant
"""
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
"""
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)
"""
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
@LucaMannella
Copy link

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 inside config/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!

@balloob
Copy link
Author

balloob commented Nov 19, 2021

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

@LucaMannella
Copy link

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