From 74f95ac3381a643fd13c5de62cca208c79fa7902 Mon Sep 17 00:00:00 2001 From: tkdrob Date: Wed, 28 Apr 2021 15:24:56 -0400 Subject: [PATCH] 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 --- .coveragerc | 1 + homeassistant/components/goalzero/__init__.py | 18 ++++- .../components/goalzero/binary_sensor.py | 9 ++- homeassistant/components/goalzero/const.py | 10 ++- .../components/goalzero/manifest.json | 2 +- .../components/goalzero/strings.json | 2 +- homeassistant/components/goalzero/switch.py | 71 +++++++++++++++++++ .../components/goalzero/translations/en.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 10 files changed, 107 insertions(+), 12 deletions(-) create mode 100644 homeassistant/components/goalzero/switch.py diff --git a/.coveragerc b/.coveragerc index 05a752764c3..181c551e02a 100644 --- a/.coveragerc +++ b/.coveragerc @@ -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 diff --git a/homeassistant/components/goalzero/__init__.py b/homeassistant/components/goalzero/__init__.py index 34e57eeeac9..b0883d42a5f 100644 --- a/homeassistant/components/goalzero/__init__.py +++ b/homeassistant/components/goalzero/__init__.py @@ -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 diff --git a/homeassistant/components/goalzero/binary_sensor.py b/homeassistant/components/goalzero/binary_sensor.py index a2af8a18546..59a8a6b3443 100644 --- a/homeassistant/components/goalzero/binary_sensor.py +++ b/homeassistant/components/goalzero/binary_sensor.py @@ -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) diff --git a/homeassistant/components/goalzero/const.py b/homeassistant/components/goalzero/const.py index 3afa1e537c1..826c2621e23 100644 --- a/homeassistant/components/goalzero/const.py +++ b/homeassistant/components/goalzero/const.py @@ -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", } diff --git a/homeassistant/components/goalzero/manifest.json b/homeassistant/components/goalzero/manifest.json index 405fbaf7342..0a1bc4df70d 100644 --- a/homeassistant/components/goalzero/manifest.json +++ b/homeassistant/components/goalzero/manifest.json @@ -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" } diff --git a/homeassistant/components/goalzero/strings.json b/homeassistant/components/goalzero/strings.json index bd59cd5e7f5..92813337e77 100644 --- a/homeassistant/components/goalzero/strings.json +++ b/homeassistant/components/goalzero/strings.json @@ -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%]" diff --git a/homeassistant/components/goalzero/switch.py b/homeassistant/components/goalzero/switch.py new file mode 100644 index 00000000000..dd4c9deae3e --- /dev/null +++ b/homeassistant/components/goalzero/switch.py @@ -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) diff --git a/homeassistant/components/goalzero/translations/en.json b/homeassistant/components/goalzero/translations/en.json index 25aa32e4b75..e6c6e4a7298 100644 --- a/homeassistant/components/goalzero/translations/en.json +++ b/homeassistant/components/goalzero/translations/en.json @@ -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" } } diff --git a/requirements_all.txt b/requirements_all.txt index c93f3745100..c6883125d99 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -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 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 04f1f90b03c..de06e914303 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -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