Skip to content

Commit

Permalink
Revert "Errors when off (#394)"
Browse files Browse the repository at this point in the history
This reverts commit d116c86.
  • Loading branch information
marcolivierarsenault committed Aug 27, 2024
1 parent da7ce47 commit 7b6b206
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 112 deletions.
40 changes: 12 additions & 28 deletions custom_components/moonraker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import os.path
import uuid
from datetime import timedelta
import socket

import async_timeout
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import Config, HomeAssistant
from homeassistant.exceptions import PlatformNotReady
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
Expand Down Expand Up @@ -55,17 +54,6 @@ def get_user_name(hass: HomeAssistant, entry: ConfigEntry):
return device_entries[0].name_by_user


def is_open(ip, port):
"""Check if port is open."""
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, int(port)))
s.shutdown(2)
return True
except Exception:
return False


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
"""Set up this integration using UI."""

Expand Down Expand Up @@ -99,24 +87,20 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):

try:
async with async_timeout.timeout(TIMEOUT):
if is_open(url, port):
await api.start()
printer_info = await api.client.call_method("printer.info")
_LOGGER.debug(printer_info)
await api.start()
printer_info = await api.client.call_method("printer.info")
_LOGGER.debug(printer_info)

if printer_name == "":
api_device_name = printer_info[HOSTNAME]
else:
api_device_name = printer_name

hass.config_entries.async_update_entry(entry, title=api_device_name)
if printer_name == "":
api_device_name = printer_info[HOSTNAME]
else:
_LOGGER.warning("Cannot connect to moonraker instance")
raise PlatformNotReady(f"Error connecting to {url}:{port}")
api_device_name = printer_name

hass.config_entries.async_update_entry(entry, title=api_device_name)

except Exception:
except Exception as exc:
_LOGGER.warning("Cannot configure moonraker instance")
raise PlatformNotReady(f"Error connecting to {url}:{port}")
raise ConfigEntryNotReady(f"Error connecting to {url}:{port}") from exc

coordinator = MoonrakerDataUpdateCoordinator(
hass, client=api, config_entry=entry, api_device_name=api_device_name
Expand All @@ -125,7 +109,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
await coordinator.async_refresh()

if not coordinator.last_update_success:
raise PlatformNotReady
raise ConfigEntryNotReady

hass.data[DOMAIN][entry.entry_id] = coordinator
for platform in PLATFORMS:
Expand Down
9 changes: 0 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,12 +442,3 @@ def get_moonraker_default_mock(get_default_api_response):
return_value=get_default_api_response,
):
yield


@pytest.fixture(name="skip_connection_check")
def skip_connection_check_fixture():
"""Skip skip_connection_check ."""
with (
patch("custom_components.moonraker.is_open"),
):
yield
6 changes: 0 additions & 6 deletions tests/test_binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ def bypass_connect_client_fixture():
yield


@pytest.fixture(name="bypass_connection_test", autouse=True)
def bypass_connection_test_fixture(skip_connection_check):
"""Skip calls to get data from API."""
yield


async def test_runout_filament_sensor_missing(hass, get_data, get_printer_objects_list):
"""Test."""
get_data["status"].pop("filament_switch_sensor filament_sensor_1", None)
Expand Down
6 changes: 0 additions & 6 deletions tests/test_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ def bypass_connect_client_fixture():
yield


@pytest.fixture(name="bypass_connection_test", autouse=True)
def bypass_connection_test_fixture(skip_connection_check):
"""Skip calls to get data from API."""
yield


@pytest.mark.parametrize(
"button, method",
[
Expand Down
6 changes: 0 additions & 6 deletions tests/test_camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ def bypass_connect_client_fixture():
yield


@pytest.fixture(name="bypass_connection_test", autouse=True)
def bypass_connection_test_fixture(skip_connection_check):
"""Skip calls to get data from API."""
yield


async def test_camera_services(hass, caplog):
"""Test camera services."""

Expand Down
6 changes: 0 additions & 6 deletions tests/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@
from .const import MOCK_CONFIG, MOCK_OPTIONS


@pytest.fixture(name="bypass_connection_test", autouse=True)
def bypass_connection_test_fixture(skip_connection_check):
"""Skip calls to get data from API."""
yield


@pytest.fixture(name="bypass_connect_client")
def bypass_connect_client_fixture():
"""Skip calls to get data from API."""
Expand Down
41 changes: 8 additions & 33 deletions tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest.mock import patch

import pytest
from homeassistant.exceptions import PlatformNotReady
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import UpdateFailed
from pytest_homeassistant_custom_component.common import MockConfigEntry

Expand All @@ -25,13 +25,7 @@ def bypass_connect_client_fixture():
yield


@pytest.fixture(name="bypass_connection_test")
def bypass_connection_test_fixture(skip_connection_check):
"""Skip calls to get data from API."""
yield


async def test_setup_unload_and_reload_entry(hass, bypass_connection_test):
async def test_setup_unload_and_reload_entry(hass):
"""Test entry setup and unload."""
# Create a mock entry so we don't have to go through config flow

Expand All @@ -57,7 +51,7 @@ async def test_setup_unload_and_reload_entry(hass, bypass_connection_test):
assert config_entry.entry_id not in hass.data[DOMAIN]


async def test_setup_unload_and_reload_entry_with_name(hass, bypass_connection_test):
async def test_setup_unload_and_reload_entry_with_name(hass):
"""Test entry setup with name and unload."""
# Create a mock entry so we don't have to go through config flow

Expand Down Expand Up @@ -85,7 +79,7 @@ async def test_setup_unload_and_reload_entry_with_name(hass, bypass_connection_t
assert config_entry.entry_id not in hass.data[DOMAIN]


async def test_async_send_data_exception(hass, bypass_connection_test):
async def test_async_send_data_exception(hass):
"""Test async_post_exception."""

config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
Expand All @@ -106,7 +100,7 @@ async def test_async_send_data_exception(hass, bypass_connection_test):
assert await async_unload_entry(hass, config_entry)


async def test_setup_entry_exception(hass, bypass_connection_test):
async def test_setup_entry_exception(hass):
"""Test ConfigEntryNotReady when API raises an exception during entry setup."""
with patch(
"moonraker_api.MoonrakerClient.call_method",
Expand All @@ -115,7 +109,7 @@ async def test_setup_entry_exception(hass, bypass_connection_test):
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)

with pytest.raises(PlatformNotReady):
with pytest.raises(ConfigEntryNotReady):
assert await async_setup_entry(hass, config_entry)


Expand All @@ -127,7 +121,7 @@ def load_data(endpoint, *args, **kwargs):
raise Exception


async def test_failed_first_refresh(hass, bypass_connection_test):
async def test_failed_first_refresh(hass):
"""Test ConfigEntryNotReady when API raises an exception during entry setup."""
with patch(
"moonraker_api.MoonrakerClient.call_method",
Expand All @@ -136,24 +130,5 @@ async def test_failed_first_refresh(hass, bypass_connection_test):
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)

with pytest.raises(PlatformNotReady):
assert await async_setup_entry(hass, config_entry)


async def test_is_on(hass):
"""Test connection is working."""
with patch("socket.socket"):
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()


async def test_is_off(hass):
"""Test connection is working."""
with patch("socket.socket", side_effect=Exception("mocked error")):
config_entry = MockConfigEntry(domain=DOMAIN, data=MOCK_CONFIG, entry_id="test")
config_entry.add_to_hass(hass)

with pytest.raises(PlatformNotReady):
with pytest.raises(ConfigEntryNotReady):
assert await async_setup_entry(hass, config_entry)
6 changes: 0 additions & 6 deletions tests/test_number.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ def bypass_connect_client_fixture():
yield


@pytest.fixture(name="bypass_connection_test", autouse=True)
def bypass_connection_test_fixture(skip_connection_check):
"""Skip calls to get data from API."""
yield


# test number
@pytest.mark.parametrize(
"number",
Expand Down
6 changes: 0 additions & 6 deletions tests/test_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ def bypass_connect_client_fixture():
yield


@pytest.fixture(name="bypass_connection_test", autouse=True)
def bypass_connection_test_fixture(skip_connection_check):
"""Skip calls to get data from API."""
yield


@pytest.fixture(name="data_for_calculate_pct")
def data_for_calculate_pct_fixture():
"""data_for_calculate_pct."""
Expand Down
6 changes: 0 additions & 6 deletions tests/test_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,6 @@ def bypass_connect_client_fixture():
yield


@pytest.fixture(name="bypass_connection_test", autouse=True)
def bypass_connection_test_fixture(skip_connection_check):
"""Skip calls to get data from API."""
yield


# test switches
@pytest.mark.parametrize(
"switch, switch_type",
Expand Down

0 comments on commit 7b6b206

Please sign in to comment.