From 0fa39135ef3f45af5c2b11ec3d846f596636d91b Mon Sep 17 00:00:00 2001 From: Jacob Tomlinson Date: Mon, 6 Jan 2020 09:07:44 +0000 Subject: [PATCH] Add more examples (#6) * Add more examples * Fix class name --- docs/examples.md | 89 ++++++++++++++++++++++-- opsdroid_homeassistant/skill/__init__.py | 4 +- 2 files changed, 86 insertions(+), 7 deletions(-) diff --git a/docs/examples.md b/docs/examples.md index 2c17115..25a98e0 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -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://.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") ``` diff --git a/opsdroid_homeassistant/skill/__init__.py b/opsdroid_homeassistant/skill/__init__.py index 79df3f4..2e600dd 100644 --- a/opsdroid_homeassistant/skill/__init__.py +++ b/opsdroid_homeassistant/skill/__init__.py @@ -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.