Skip to content

Commit

Permalink
Add service to retrieve execution history (#365)
Browse files Browse the repository at this point in the history
Co-authored-by: Vincent Le Bourlot <vlebourl@gmail.com>
  • Loading branch information
iMicknl and vlebourl authored Jan 31, 2021
1 parent 2657d9f commit 89ce9a4
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,23 @@ logger:
custom_components.tahoma: debug
```
### Device not supported / working correctly
### Device not supported
If your device is not visible in the device list of Home Assistant (/config/devices/dashboard), you need to turn on [debug logging](#enable-debug-logging). Copy the debug string from your log and create a [new issue](https://github.com/iMicknl/ha-tahoma/issues/new/choose)
`DEBUG (MainThread) [custom_components.tahoma] Unsupported TaHoma device (io:DimmableLightIOComponent - Light - DimmableLight).`

### Device not working correctly

If your device is listed in the device list, create a [new issue](https://github.com/iMicknl/ha-tahoma/issues/new/choose) and fill in all details of the issue template.

In order to gather more information, you can use the `tahoma.get_execution_history` service which will print your execution history to the Home Assistant log. Run the commands via the official vendor app (e.g. TaHoma) and capture the commands.

```
2021-01-28 09:20:22 INFO (MainThread) [custom_components.tahoma] 2021-01-27 21:30:00: off executed via Home Assistant on io://xxxx, with [].
2021-01-28 09:20:22 INFO (MainThread) [custom_components.tahoma] 2021-01-27 16:23:29: setIntensity executed via Home Assistant on io://xxxx, with [70].
```
### Exclude devices
The previous version had functionality to exclude devices from Home Assistant. Since we moved to the entity registry, this functionality is now offered by default in Home Assistant core. You can now disable entities via the interface.
Expand Down
64 changes: 63 additions & 1 deletion custom_components/tahoma/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""The TaHoma integration."""
import asyncio
from collections import defaultdict
from datetime import timedelta
from datetime import datetime, timedelta
from enum import Enum
import logging

Expand Down Expand Up @@ -207,6 +207,47 @@ async def handle_execute_command(call):

device_registry = await dr.async_get_registry(hass)

for gateway in gateways:
_LOGGER.debug(
"Added gateway (%s - %s - %s)",
gateway.id,
gateway.type,
gateway.sub_type,
)

gateway_model = (
beautify_name(gateway.sub_type.name)
if isinstance(gateway.sub_type, Enum)
else None
)
gateway_name = (
f"{beautify_name(gateway.type.name)} hub"
if isinstance(gateway.type, Enum)
else None
)

device_registry.async_get_or_create(
config_entry_id=entry.entry_id,
identifiers={(DOMAIN, gateway.id)},
model=gateway_model,
manufacturer="Somfy",
name=gateway_name,
sw_version=gateway.connectivity.protocol_version,
)

async def handle_get_execution_history(call):
"""Handle get execution history service."""
await write_execution_history_to_log(tahoma_coordinator.client)

service.async_register_admin_service(
hass,
DOMAIN,
"get_execution_history",
handle_get_execution_history,
)

device_registry = await dr.async_get_registry(hass)

for gateway in gateways:
_LOGGER.debug(
"Added gateway (%s - %s - %s)",
Expand Down Expand Up @@ -278,6 +319,27 @@ def print_homekit_setup_code(device: Device):
_LOGGER.info("HomeKit support detected with setup code %s.", homekit.value)


async def write_execution_history_to_log(client: TahomaClient):
"""Retrieve execution history and write output to log."""
history = await client.get_execution_history()

for item in history:
timestamp = datetime.fromtimestamp(int(item.event_time) / 1000)

for command in item.commands:
date = timestamp.strftime("%Y-%m-%d %H:%M:%S")

_LOGGER.info(
"{timestamp}: {command} executed via {app} on {device}, with {parameters}.".format(
command=command.command,
timestamp=date,
device=command.deviceurl,
parameters=command.parameters,
app=item.label,
)
)


def beautify_name(name: str):
"""Return human readable string."""
return name.replace("_", " ").title()
2 changes: 1 addition & 1 deletion custom_components/tahoma/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/tahoma",
"requirements": [
"pyhoma==0.5.4"
"pyhoma==0.5.5"
],
"codeowners": [
"@philklei",
Expand Down
3 changes: 3 additions & 0 deletions custom_components/tahoma/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ execute_command:
args:
description: List of arguments to pass to the command if necessary
example: 100

get_execution_history:
description: Retrieve execution history and write output to log.
2 changes: 1 addition & 1 deletion requirements.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ pytest-cov<3.0.0
pytest-homeassistant

# from our manifest.json for our Custom Component
pyhoma==0.5.4
pyhoma==0.5.5

0 comments on commit 89ce9a4

Please sign in to comment.