Add switch platform to goalzero (#48612)

* Add switch platform to goalzero

* fix update interval

* Apply some suggested changes

* pass device class to parent

* Drop passing device_class

* Tweaks

* Drop underscore prefix
This commit is contained in:
tkdrob 2021-04-28 15:24:56 -04:00 committed by GitHub
parent 2908332a4e
commit 74f95ac338
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 107 additions and 12 deletions

View file

@ -358,6 +358,7 @@ omit =
homeassistant/components/goalfeed/*
homeassistant/components/goalzero/__init__.py
homeassistant/components/goalzero/binary_sensor.py
homeassistant/components/goalzero/switch.py
homeassistant/components/google/*
homeassistant/components/google_cloud/tts.py
homeassistant/components/google_maps/device_tracker.py

View file

@ -3,6 +3,8 @@ import logging
from goalzero import Yeti, exceptions
from homeassistant.components.binary_sensor import DOMAIN as DOMAIN_BINARY_SENSOR
from homeassistant.components.switch import DOMAIN as DOMAIN_SWITCH
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant
@ -19,7 +21,7 @@ from .const import DATA_KEY_API, DATA_KEY_COORDINATOR, DOMAIN, MIN_TIME_BETWEEN_
_LOGGER = logging.getLogger(__name__)
PLATFORMS = ["binary_sensor"]
PLATFORMS = [DOMAIN_BINARY_SENSOR, DOMAIN_SWITCH]
async def async_setup_entry(hass, entry):
@ -30,7 +32,7 @@ async def async_setup_entry(hass, entry):
session = async_get_clientsession(hass)
api = Yeti(host, hass.loop, session)
try:
await api.get_state()
await api.init_connect()
except exceptions.ConnectError as ex:
_LOGGER.warning("Failed to connect: %s", ex)
raise ConfigEntryNotReady from ex
@ -82,10 +84,20 @@ class YetiEntity(CoordinatorEntity):
@property
def device_info(self):
"""Return the device information of the entity."""
if self.api.data:
sw_version = self.api.data["firmwareVersion"]
else:
sw_version = None
if self.api.sysdata:
model = self.api.sysdata["model"]
else:
model = model or None
return {
"identifiers": {(DOMAIN, self._server_unique_id)},
"name": self._name,
"manufacturer": "Goal Zero",
"model": model,
"name": self._name,
"sw_version": sw_version,
}
@property

View file

@ -28,7 +28,14 @@ async def async_setup_entry(hass, entry, async_add_entities):
class YetiBinarySensor(YetiEntity, BinarySensorEntity):
"""Representation of a Goal Zero Yeti sensor."""
def __init__(self, api, coordinator, name, sensor_name, server_unique_id):
def __init__(
self,
api,
coordinator,
name,
sensor_name,
server_unique_id,
):
"""Initialize a Goal Zero Yeti sensor."""
super().__init__(api, coordinator, name, server_unique_id)

View file

@ -15,9 +15,6 @@ DATA_KEY_API = "api"
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30)
BINARY_SENSOR_DICT = {
"v12PortStatus": ["12V Port Status", DEVICE_CLASS_POWER, None],
"usbPortStatus": ["USB Port Status", DEVICE_CLASS_POWER, None],
"acPortStatus": ["AC Port Status", DEVICE_CLASS_POWER, None],
"backlight": ["Backlight", None, "mdi:clock-digital"],
"app_online": [
"App Online",
@ -25,4 +22,11 @@ BINARY_SENSOR_DICT = {
None,
],
"isCharging": ["Charging", DEVICE_CLASS_BATTERY_CHARGING, None],
"inputDetected": ["Input Detected", DEVICE_CLASS_POWER, None],
}
SWITCH_DICT = {
"v12PortStatus": "12V Port Status",
"usbPortStatus": "USB Port Status",
"acPortStatus": "AC Port Status",
}

View file

@ -3,7 +3,7 @@
"name": "Goal Zero Yeti",
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/goalzero",
"requirements": ["goalzero==0.1.4"],
"requirements": ["goalzero==0.1.7"],
"codeowners": ["@tkdrob"],
"iot_class": "local_polling"
}

View file

@ -3,7 +3,7 @@
"step": {
"user": {
"title": "Goal Zero Yeti",
"description": "First, you need to download the Goal Zero app: https://www.goalzero.com/product-features/yeti-app/\n\nFollow the instructions to connect your Yeti to your Wifi network. Then get the host IP from your router. DHCP must be set up in your router settings for the device to ensure the host IP does not change. Refer to your router's user manual.",
"description": "First, you need to download the Goal Zero app: https://www.goalzero.com/product-features/yeti-app/\n\nFollow the instructions to connect your Yeti to your Wifi network. DHCP reservation must be set up in your router settings for the device to ensure the host IP does not change. Refer to your router's user manual.",
"data": {
"host": "[%key:common::config_flow::data::host%]",
"name": "[%key:common::config_flow::data::name%]"

View file

@ -0,0 +1,71 @@
"""Support for Goal Zero Yeti Switches."""
from homeassistant.components.switch import SwitchEntity
from homeassistant.const import CONF_NAME
from . import YetiEntity
from .const import DATA_KEY_API, DATA_KEY_COORDINATOR, DOMAIN, SWITCH_DICT
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up the Goal Zero Yeti switch."""
name = entry.data[CONF_NAME]
goalzero_data = hass.data[DOMAIN][entry.entry_id]
switches = [
YetiSwitch(
goalzero_data[DATA_KEY_API],
goalzero_data[DATA_KEY_COORDINATOR],
name,
switch_name,
entry.entry_id,
)
for switch_name in SWITCH_DICT
]
async_add_entities(switches)
class YetiSwitch(YetiEntity, SwitchEntity):
"""Representation of a Goal Zero Yeti switch."""
def __init__(
self,
api,
coordinator,
name,
switch_name,
server_unique_id,
):
"""Initialize a Goal Zero Yeti switch."""
super().__init__(api, coordinator, name, server_unique_id)
self._condition = switch_name
self._condition_name = SWITCH_DICT[switch_name]
@property
def name(self):
"""Return the name of the switch."""
return f"{self._name} {self._condition_name}"
@property
def unique_id(self):
"""Return the unique id of the switch."""
return f"{self._server_unique_id}/{self._condition}"
@property
def is_on(self):
"""Return state of the switch."""
if self.api.data:
return self.api.data[self._condition]
return None
async def async_turn_off(self, **kwargs):
"""Turn off the switch."""
payload = {self._condition: 0}
await self.api.post_state(payload=payload)
self.coordinator.async_set_updated_data(data=payload)
async def async_turn_on(self, **kwargs):
"""Turn on the switch."""
payload = {self._condition: 1}
await self.api.post_state(payload=payload)
self.coordinator.async_set_updated_data(data=payload)

View file

@ -14,7 +14,7 @@
"host": "Host",
"name": "Name"
},
"description": "First, you need to download the Goal Zero app: https://www.goalzero.com/product-features/yeti-app/\n\nFollow the instructions to connect your Yeti to your Wifi network. Then get the host IP from your router. DHCP must be set up in your router settings for the device to ensure the host IP does not change. Refer to your router's user manual.",
"description": "First, you need to download the Goal Zero app: https://www.goalzero.com/product-features/yeti-app/\n\nFollow the instructions to connect your Yeti to your Wifi network. DHCP reservation must be set up in your router settings for the device to ensure the host IP does not change. Refer to your router's user manual.",
"title": "Goal Zero Yeti"
}
}

View file

@ -669,7 +669,7 @@ glances_api==0.2.0
gntp==1.0.3
# homeassistant.components.goalzero
goalzero==0.1.4
goalzero==0.1.7
# homeassistant.components.gogogate2
gogogate2-api==3.0.0

View file

@ -366,7 +366,7 @@ gios==0.2.1
glances_api==0.2.0
# homeassistant.components.goalzero
goalzero==0.1.4
goalzero==0.1.7
# homeassistant.components.gogogate2
gogogate2-api==3.0.0