Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: numerous issues #57

Merged
merged 6 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions ovos_PHAL_plugin_homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,34 @@
from os.path import dirname, join
from typing import Optional

from ovos_utils.log import LOG
from ovos_bus_client import Message
from ovos_plugin_manager.phal import PHALPlugin
from ovos_bus_client.apis.gui import GUIInterface
from ovos_config.config import update_mycroft_config
from ovos_plugin_manager.phal import PHALPlugin
from ovos_utils import classproperty
from ovos_utils.log import LOG
from ovos_utils.parse import match_one
from ovos_utils.process_utils import RuntimeRequirements

from ovos_PHAL_plugin_homeassistant.logic.connector import HomeAssistantRESTConnector, HomeAssistantWSConnector
from ovos_PHAL_plugin_homeassistant.logic.device import (HomeAssistantSensor,
HomeAssistantBinarySensor,
HomeAssistantLight, HomeAssistantAutomation,
HomeAssistantMediaPlayer, HomeAssistantScene,
HomeAssistantVacuum, HomeAssistantSwitch,
HomeAssistantClimate, HomeAssistantCamera)
from ovos_PHAL_plugin_homeassistant.logic.device import (
HomeAssistantAutomation,
HomeAssistantBinarySensor,
HomeAssistantCamera,
HomeAssistantClimate,
HomeAssistantLight,
HomeAssistantMediaPlayer,
HomeAssistantScene,
HomeAssistantSensor,
HomeAssistantSwitch,
HomeAssistantVacuum,
)
from ovos_PHAL_plugin_homeassistant.logic.integration import Integrator
from ovos_PHAL_plugin_homeassistant.logic.utils import (map_entity_to_device_type,
check_if_device_type_is_group,
get_percentage_brightness_from_ha_value)
from ovos_config.config import update_mycroft_config
from ovos_PHAL_plugin_homeassistant.logic.utils import (
check_if_device_type_is_group,
get_percentage_brightness_from_ha_value,
map_entity_to_device_type,
)

SUPPORTED_DEVICES = {
"sensor": HomeAssistantSensor,
Expand Down Expand Up @@ -115,6 +126,15 @@ def __init__(self, bus=None, config=None):

self.init_configuration()

@classproperty
def runtime_requirements(self):
return RuntimeRequirements(internet_before_load=False,
network_before_load=True,
requires_internet=False,
requires_network=True,
no_internet_fallback=True,
no_network_fallback=False)

def handle_check_connected(self, message: Message):
"""Return a bus response indicating whether the plugin is connected to a Home Assistant instance."""
self.bus.emit(message.response(data={"connected": self.instance_available}))
Expand Down
10 changes: 9 additions & 1 deletion ovos_PHAL_plugin_homeassistant/logic/socketclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def authenticate(self):
async def _connect(self):
try:
uri = f"{self.url}/api/websocket"
self.websocket = await websockets.connect(uri)
self.websocket = await websockets.connect(uri=uri, close_timeout=5, open_timeout=5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to make open_timeout configurable? 5s seems like plenty but I'm not sure if that includes authentication/handshaking that could take longer for some reason

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like a lot of extra work for minimal gain, but I'm open to the idea


# Wait for the auth_required message
message = await self.websocket.recv()
Expand All @@ -61,6 +61,14 @@ async def _connect(self):
return
else:
raise Exception("Expected auth_required message")
except asyncio.CancelledError:
LOG.exception("Connection cancelled, likely due to change in network configuration")
await self._disconnect()
return
except TimeoutError:
LOG.exception("Connection timed out, disconnecting")
await self._disconnect()
return
except Exception as e:
LOG.exception(e)
await self._disconnect()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ovos-utils<0.2.0,>=0.0.27
ovos-config~=0.0.5
youtube-search~=2.1
pytube~=12.1
websockets~=10.4
websockets>=0.54.0,<13.0
ovos-PHAL-plugin-oauth~=0.0.1,>=0.0.3a2
nested-lookup~=0.2
ovos-bus-client<0.2.0,>=0.0.8
Expand Down
Loading