Skip to content

Commit

Permalink
Add more examples (#6)
Browse files Browse the repository at this point in the history
* Add more examples

* Fix class name
  • Loading branch information
jacobtomlinson authored Jan 6, 2020
1 parent b81731a commit 0fa3913
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 7 deletions.
89 changes: 83 additions & 6 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,87 @@ class MotionLights(HassSkill):

@match_hass_state_changed("binary_sensor.drive", state="on")
async def motion_lights(self, event):
await self.turn_on("light.drive")
for _ in range(10):
await self.toggle("light.living_room_lamp")
await sleep(1)
await sleep(50)
await self.turn_off("light.drive")
if await self.sun_down():
await self.turn_on("light.drive")
for _ in range(10):
await self.toggle("light.living_room_lamp")
await sleep(1)
await sleep(50)
await self.turn_off("light.drive")
```

## Motion camera notification

The following skill takes a snapshot of a camera when motion is detected and pushes it to an Android device as a notification (in this example a notification target called `notify.mobile_app_pixel_4`).

```eval_rst
.. note::
This skill assumes you have Home Assistant Cloud as notification images need to be accessible on the internet.
```

```python
from asyncio import sleep

from opsdroid_homeassistant import HassSkill, match_hass_state_changed


class MotionCamera(HassSkill):

@match_hass_state_changed("binary_sensor.drive", state="on")
async def motion_camera(self, event):

# Snapshot camera to a the local `www` folder
await self.call_service(
"camera",
"snapshot",
entity_id="camera.drive",
filename="/config/www/cameras/camera.drive.jpg",
)

# Wait for the snapshot to save
await sleep(1)

# Send a notification with the image linked via Home Assistant Cloud
await self.notify(
"Camera Update",
title="Motion detected",
target="mobile_app_pixel_4",
data={
"android": {
"notification": {
"image": "https://<Your Home Assistant Cloud ID>.ui.nabu.casa/local/cameras/camera.drive.jpg"
}
}
},
)
```

## Tortoise lights

I own a Mediterranean tortoise but live in a slightly colder climate and therefore he needs some artificial UV lighting on dull days. But if the natural sunlight is strong enough I don't want to waste power on the lighting.

This is an example of the skill I use to automate the lights.

```eval_rst
.. note::
I use the Met Office sensor to get the UV information.
```

```python
from asyncio import sleep

from opsdroid_homeassistant import HassSkill, match_hass_state_changed, natch_sunrise, match_sunset


class TortoiseSkill(HassSkill):

@match_sunrise
@match_sunset
@match_hass_state_changed("sensor.met_office_uv")
async def tortoise_lamp(self, event):
uv = int(await self.get_state("sensor.met_office_uv"))
if await self.sun_up() and uv < 5:
self.turn_on("switch.tortoise")
else:
self.turn_off("switch.tortoise")
```
4 changes: 3 additions & 1 deletion opsdroid_homeassistant/skill/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ async def get_state(self, entity: str):
>>> await self.get_state("sun.sun")
"above_horizon"
"""
return (await self.hass.query_api("states/" + entity)).get("state", None)
state = await self.hass.query_api("states/" + entity)
_LOGGER.debug(state)
return state.get("state", None)

async def turn_on(self, entity_id: str, **kwargs):
"""Turn on an entity in Home Assistant.
Expand Down

0 comments on commit 0fa3913

Please sign in to comment.