Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Wartner committed May 28, 2024
1 parent 0a27c06 commit 547c6ab
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 33 deletions.
15 changes: 8 additions & 7 deletions custom_components/openwakeword-installer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import logging

from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.typing import ConfigType

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigType):
"""Set up OpenWakeWord from a config entry."""
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up WakeWord Installer from a config entry."""
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, "sensor")
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)
return True

async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigType):
"""Unload a config entry."""
await hass.config_entries.async_forward_entry_unload(config_entry, "sensor")
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Unload WakeWord Installer config entry."""
await hass.config_entries.async_forward_entry_unload(entry, "sensor")
return True
8 changes: 4 additions & 4 deletions custom_components/openwakeword-installer/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.core import callback
import voluptuous as vol

from .const import DOMAIN

@callback
def configured_instances(hass):
return [entry.data["repository_url"] for entry in hass.config_entries.async_entries(DOMAIN)]

class OpenWakeWordConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for OpenWakeWord."""
class WakeWordInstallerConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a config flow for WakeWord Installer."""

VERSION = 1

Expand All @@ -22,7 +22,7 @@ async def async_step_user(self, user_input=None):
else:
# Validate the URL
# Add your validation code here
return self.async_create_entry(title="OpenWakeWord", data=user_input)
return self.async_create_entry(title="WakeWord Installer", data=user_input)

data_schema = vol.Schema({
vol.Required("repository_url"): str,
Expand Down
2 changes: 1 addition & 1 deletion custom_components/openwakeword-installer/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DOMAIN = "openwakeword-installer"
DOMAIN = "wakeword_installer"
DEFAULT_SCAN_INTERVAL = 3600 # 1 hour

CONF_REPOSITORY_URL = "repository_url"
Expand Down
4 changes: 2 additions & 2 deletions custom_components/openwakeword-installer/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"domain": "openwakeword-installer",
"name": "OpenWakeWord Installer",
"domain": "wakeword_installer",
"name": "WakeWord Installer",
"version": "1.0.3",
"documentation": "https://github.com/fwartner/ha-openwakeword-installer",
"issue_tracker": "https://github.com/fwartner/ha-openwakeword-installer/issues",
Expand Down
68 changes: 56 additions & 12 deletions custom_components/openwakeword-installer/sensor.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
from homeassistant.helpers.entity import Entity
import logging
import datetime
import git
import voluptuous as vol

from homeassistant.components.sensor import SensorEntity
from homeassistant.const import CONF_URL
from homeassistant.helpers.event import async_track_time_interval

from .const import DOMAIN

_LOGGER = logging.getLogger(__name__)

SCAN_INTERVAL = datetime.timedelta(minutes=60) # Check every hour

async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the OpenWakeWord sensor."""
async_add_entities([OpenWakeWordSensor(hass, config_entry.data)], True)
"""Set up the WakeWord Installer sensor."""
repository_url = config_entry.data[CONF_URL]
folder_path = config_entry.data.get("folder_path", "")

sensor = WakeWordInstallerSensor(repository_url, folder_path)
async_add_entities([sensor], True)

class OpenWakeWordSensor(Entity):
async_track_time_interval(hass, sensor.async_update, SCAN_INTERVAL)

class WakeWordInstallerSensor(SensorEntity):
"""Representation of a Sensor."""

def __init__(self, hass, config):
def __init__(self, repository_url, folder_path):
"""Initialize the sensor."""
self.hass = hass
self._state = None
self._state = "idle"
self._last_update = None
self._repository_url = repository_url
self._folder_path = folder_path
self._repo = None

@property
def name(self):
"""Return the name of the sensor."""
return "OpenWakeWord Update Status"
return "WakeWord Installer Update Status"

@property
def state(self):
Expand All @@ -32,8 +51,33 @@ def extra_state_attributes(self):
"last_update": self._last_update
}

def update(self):
async def async_update(self, *_):
"""Fetch new state data for the sensor."""
# Logic to update sensor state
self._state = "idle" # Example state
self._last_update = "2023-05-27T12:34:56" # Example last update time
self._state = "checking"
_LOGGER.debug("Checking for updates...")

try:
if self._repo is None:
self._repo = git.Repo.clone_from(self._repository_url, '/tmp/wakeword_installer_repo')
else:
self._repo.remotes.origin.pull()

files = [f for f in self._repo.tree().traverse() if f.path.endswith(".tflite")]
if self._folder_path:
files = [f for f in files if f.path.startswith(self._folder_path)]

if files:
_LOGGER.debug("Wake words found: %s", files)
self._state = "updating"
self._last_update = datetime.datetime.now().isoformat()
# Copy files to the target directory
for file in files:
file_path = f"/share/openwakeword/{file.path.split('/')[-1]}"
with open(file_path, 'wb') as f:
f.write(file.data_stream.read())
else:
_LOGGER.debug("No new wake words found.")
self._state = "idle"
except Exception as e:
_LOGGER.error("Error updating wake words: %s", e)
self._state = "error"
15 changes: 8 additions & 7 deletions custom_components/openwakeword-installer/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"config": {
"step": {
"user": {
"title": "Configure OpenWakeWord",
"description": "Set up the OpenWakeWord integration",
"title": "Configure WakeWord Installer",
"description": "Set up the WakeWord Installer integration",
"data": {
"repository_url": "Repository URL",
"folder_path": "Folder Path (optional)"
Expand All @@ -21,8 +21,8 @@
"options": {
"step": {
"init": {
"title": "Configure OpenWakeWord Options",
"description": "Update the settings for OpenWakeWord",
"title": "Configure WakeWord Installer Options",
"description": "Update the settings for WakeWord Installer",
"data": {
"repository_url": "Repository URL",
"folder_path": "Folder Path (optional)"
Expand All @@ -32,12 +32,13 @@
},
"entity": {
"sensor": {
"openwakeword_update_status": {
"name": "OpenWakeWord Update Status",
"wakeword_installer_update_status": {
"name": "WakeWord Installer Update Status",
"state": {
"idle": "Idle",
"checking": "Checking for updates",
"updating": "Updating wake words"
"updating": "Updating wake words",
"error": "Error"
}
}
}
Expand Down

0 comments on commit 547c6ab

Please sign in to comment.