Skip to content
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
34 changes: 3 additions & 31 deletions homeassistant/components/goalzero/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,22 @@
import logging

from goalzero import Yeti, exceptions
import voluptuous as vol

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)

from .const import (
DATA_KEY_API,
DATA_KEY_COORDINATOR,
DEFAULT_NAME,
DOMAIN,
MIN_TIME_BETWEEN_UPDATES,
)
from .const import DATA_KEY_API, DATA_KEY_COORDINATOR, DOMAIN, MIN_TIME_BETWEEN_UPDATES

_LOGGER = logging.getLogger(__name__)

GOALZERO_SCHEMA = vol.Schema(
vol.All(
{
vol.Required(CONF_HOST): cv.matches_regex(
r"\A(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2 \
[0-4][0-9]|[01]?[0-9][0-9]?)\Z"
),
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
},
)
)

CONFIG_SCHEMA = vol.Schema(
{DOMAIN: vol.Schema(vol.All(cv.ensure_list, [GOALZERO_SCHEMA]))},
extra=vol.ALLOW_EXTRA,
)


PLATFORMS = ["binary_sensor"]

Expand All @@ -61,8 +36,6 @@ async def async_setup_entry(hass, entry):
name = entry.data[CONF_NAME]
host = entry.data[CONF_HOST]

_LOGGER.debug("Setting up %s integration with host %s", DOMAIN, host)

session = async_get_clientsession(hass)
api = Yeti(host, hass.loop, session)
try:
Expand All @@ -76,7 +49,6 @@ async def async_update_data():
try:
await api.get_state()
except exceptions.ConnectError as err:
_LOGGER.warning("Failed to update data from Yeti")
raise UpdateFailed(f"Failed to communicating with API: {err}") from err

coordinator = DataUpdateCoordinator(
Expand Down Expand Up @@ -117,10 +89,10 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry):
class YetiEntity(CoordinatorEntity):
"""Representation of a Goal Zero Yeti entity."""

def __init__(self, _api, coordinator, name, sensor_name, server_unique_id):
def __init__(self, api, coordinator, name, server_unique_id):
"""Initialize a Goal Zero Yeti entity."""
super().__init__(coordinator)
self.api = _api
self.api = api
self._name = name
self._server_unique_id = server_unique_id
self._device_class = None
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/goalzero/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ class YetiBinarySensor(YetiEntity, BinarySensorEntity):

def __init__(self, api, coordinator, name, sensor_name, server_unique_id):
"""Initialize a Goal Zero Yeti sensor."""
super().__init__(api, coordinator, name, sensor_name, server_unique_id)
super().__init__(api, coordinator, name, server_unique_id)

self._condition = sensor_name

variable_info = BINARY_SENSOR_DICT[sensor_name]
self._condition_name = variable_info[0]
self._icon = variable_info[2]
self.api = api
self._device_class = variable_info[1]

@property
Expand All @@ -55,6 +54,7 @@ def is_on(self):
"""Return if the service is on."""
if self.api.data:
return self.api.data[self._condition] == 1
return False

@property
def icon(self):
Expand Down
16 changes: 9 additions & 7 deletions homeassistant/components/goalzero/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,20 @@ async def async_step_user(self, user_input=None):

try:
await self._async_try_connect(host)
return self.async_create_entry(
title=name,
data={CONF_HOST: host, CONF_NAME: name},
)
except exceptions.ConnectError:
errors["base"] = "cannot_connect"
_LOGGER.exception("Error connecting to device at %s", host)
_LOGGER.error("Error connecting to device at %s", host)
except exceptions.InvalidHost:
errors["base"] = "invalid_host"
_LOGGER.exception("Invalid data received from device at %s", host)
_LOGGER.error("Invalid host at %s", host)
except Exception: # pylint: disable=broad-except
_LOGGER.exception("Unexpected exception")
errors["base"] = "unknown"
else:
return self.async_create_entry(
title=name,
data={CONF_HOST: host, CONF_NAME: name},
)

user_input = user_input or {}
return self.async_show_form(
Expand All @@ -67,7 +68,8 @@ async def async_step_user(self, user_input=None):
async def _async_endpoint_existed(self, endpoint):
for entry in self._async_current_entries():
if endpoint == entry.data.get(CONF_HOST):
return endpoint
return True
return False

async def _async_try_connect(self, host):
session = async_get_clientsession(self.hass)
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/goalzero/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"error": {
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
"invalid_host": "This is not the Yeti you are looking for",
"invalid_host": "Invalid host provided",
"unknown": "Unknown Error"
},
"abort": {
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/goalzero/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"error": {
"cannot_connect": "Failed to connect",
"invalid_host": "This is not the Yeti you are looking for",
"invalid_host": "Invalid host provided",
"unknown": "Unknown Error"
},
"step": {
Expand Down
5 changes: 0 additions & 5 deletions tests/components/goalzero/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ async def test_flow_user(hass):
DOMAIN,
context={"source": SOURCE_USER},
)
assert result["type"] == RESULT_TYPE_FORM
assert result["step_id"] == "user"
assert result["errors"] == {}
_flow_next(hass, result["flow_id"])

result = await hass.config_entries.flow.async_configure(
result["flow_id"],
user_input=CONF_CONFIG_FLOW,
Expand Down