From 930db7167e3bf6e140ecfda8743626fdb6c06ac9 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Wed, 21 Jul 2021 02:53:53 -0400 Subject: [PATCH] Code quality improvements for goalzero (#53260) --- homeassistant/components/goalzero/__init__.py | 42 +++++++++++-------- .../components/goalzero/binary_sensor.py | 13 +++--- .../components/goalzero/config_flow.py | 4 +- homeassistant/components/goalzero/const.py | 4 -- homeassistant/components/goalzero/sensor.py | 9 ++-- homeassistant/components/goalzero/switch.py | 17 ++++---- 6 files changed, 46 insertions(+), 43 deletions(-) diff --git a/homeassistant/components/goalzero/__init__.py b/homeassistant/components/goalzero/__init__.py index fe2d5cc695e..308934819cd 100644 --- a/homeassistant/components/goalzero/__init__.py +++ b/homeassistant/components/goalzero/__init__.py @@ -1,4 +1,6 @@ """The Goal Zero Yeti integration.""" +from __future__ import annotations + import logging from goalzero import Yeti, exceptions @@ -7,10 +9,20 @@ from homeassistant.components.binary_sensor import DOMAIN as DOMAIN_BINARY_SENSO from homeassistant.components.sensor import DOMAIN as DOMAIN_SENSOR from homeassistant.components.switch import DOMAIN as DOMAIN_SWITCH from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_ATTRIBUTION, CONF_HOST, CONF_NAME +from homeassistant.const import ( + ATTR_ATTRIBUTION, + ATTR_IDENTIFIERS, + ATTR_MANUFACTURER, + ATTR_MODEL, + ATTR_NAME, + ATTR_SW_VERSION, + CONF_HOST, + CONF_NAME, +) from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryNotReady from homeassistant.helpers.aiohttp_client import async_get_clientsession +from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, DataUpdateCoordinator, @@ -41,7 +53,7 @@ async def async_setup_entry(hass, entry): try: await api.init_connect() except exceptions.ConnectError as ex: - _LOGGER.warning("Failed to connect: %s", ex) + _LOGGER.warning("Failed to connect to device %s", ex) raise ConfigEntryNotReady from ex async def async_update_data(): @@ -88,23 +100,19 @@ class YetiEntity(CoordinatorEntity): self.api = api self._name = name self._server_unique_id = server_unique_id - self._device_class = None @property - def device_info(self): + def device_info(self) -> DeviceInfo: """Return the device information of the entity.""" - info = { - "identifiers": {(DOMAIN, self._server_unique_id)}, - "manufacturer": "Goal Zero", - "name": self._name, - } + model = sw_version = None if self.api.sysdata: - info["model"] = self.api.sysdata["model"] + model = self.api.sysdata[ATTR_MODEL] if self.api.data: - info["sw_version"] = self.api.data["firmwareVersion"] - return info - - @property - def device_class(self): - """Return the class of this device.""" - return self._device_class + sw_version = self.api.data["firmwareVersion"] + return { + ATTR_IDENTIFIERS: {(DOMAIN, self._server_unique_id)}, + ATTR_MANUFACTURER: "Goal Zero", + ATTR_NAME: self._name, + ATTR_MODEL: str(model), + ATTR_SW_VERSION: str(sw_version), + } diff --git a/homeassistant/components/goalzero/binary_sensor.py b/homeassistant/components/goalzero/binary_sensor.py index aec9fdb0354..74776eb51b5 100644 --- a/homeassistant/components/goalzero/binary_sensor.py +++ b/homeassistant/components/goalzero/binary_sensor.py @@ -12,7 +12,7 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up the Goal Zero Yeti sensor.""" name = entry.data[CONF_NAME] goalzero_data = hass.data[DOMAIN][entry.entry_id] - sensors = [ + async_add_entities( YetiBinarySensor( goalzero_data[DATA_KEY_API], goalzero_data[DATA_KEY_COORDINATOR], @@ -21,8 +21,7 @@ async def async_setup_entry(hass, entry, async_add_entities): entry.entry_id, ) for sensor_name in BINARY_SENSOR_DICT - ] - async_add_entities(sensors) + ) class YetiBinarySensor(YetiEntity, BinarySensorEntity): @@ -47,23 +46,23 @@ class YetiBinarySensor(YetiEntity, BinarySensorEntity): self._device_class = variable_info[1] @property - def name(self): + def name(self) -> str: """Return the name of the sensor.""" return f"{self._name} {self._condition_name}" @property - def unique_id(self): + def unique_id(self) -> str: """Return the unique id of the sensor.""" return f"{self._server_unique_id}/{self._condition_name}" @property - def is_on(self): + def is_on(self) -> bool: """Return if the service is on.""" if self.api.data: return self.api.data[self._condition] == 1 return False @property - def icon(self): + def icon(self) -> str: """Icon to use in the frontend, if any.""" return self._icon diff --git a/homeassistant/components/goalzero/config_flow.py b/homeassistant/components/goalzero/config_flow.py index 50450f95a69..4c525de9c7d 100644 --- a/homeassistant/components/goalzero/config_flow.py +++ b/homeassistant/components/goalzero/config_flow.py @@ -63,7 +63,7 @@ class GoalZeroFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): }, ) - async def async_step_user(self, user_input=None): + async def async_step_user(self, user_input=None) -> FlowResult: """Handle a flow initiated by the user.""" errors = {} if user_input is not None: @@ -98,7 +98,7 @@ class GoalZeroFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): errors=errors, ) - async def _async_try_connect(self, host): + async def _async_try_connect(self, host) -> tuple: """Try connecting to Goal Zero Yeti.""" try: session = async_get_clientsession(self.hass) diff --git a/homeassistant/components/goalzero/const.py b/homeassistant/components/goalzero/const.py index 4c860a5e0e4..da4a6ee4ad6 100644 --- a/homeassistant/components/goalzero/const.py +++ b/homeassistant/components/goalzero/const.py @@ -34,10 +34,6 @@ from homeassistant.const import ( ATTRIBUTION = "Data provided by Goal Zero" ATTR_DEFAULT_ENABLED = "default_enabled" -CONF_IDENTIFIERS = "identifiers" -CONF_MANUFACTURER = "manufacturer" -CONF_MODEL = "model" -CONF_SW_VERSION = "sw_version" DATA_KEY_COORDINATOR = "coordinator" DOMAIN = "goalzero" DEFAULT_NAME = "Yeti" diff --git a/homeassistant/components/goalzero/sensor.py b/homeassistant/components/goalzero/sensor.py index ccb39ae0813..f64d6d772c8 100644 --- a/homeassistant/components/goalzero/sensor.py +++ b/homeassistant/components/goalzero/sensor.py @@ -1,4 +1,6 @@ """Support for Goal Zero Yeti Sensors.""" +from __future__ import annotations + from homeassistant.components.sensor import ATTR_LAST_RESET, ATTR_STATE_CLASS from homeassistant.const import ( ATTR_DEVICE_CLASS, @@ -40,20 +42,19 @@ class YetiSensor(YetiEntity): 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) - self._condition = sensor_name - sensor = SENSOR_DICT[sensor_name] self._attr_name = f"{name} {sensor.get(ATTR_NAME)}" self._attr_unique_id = f"{self._server_unique_id}/{sensor_name}" self._attr_unit_of_measurement = sensor.get(ATTR_UNIT_OF_MEASUREMENT) self._attr_entity_registry_enabled_default = sensor.get(ATTR_DEFAULT_ENABLED) - self._device_class = sensor.get(ATTR_DEVICE_CLASS) + self._attr_device_class = sensor.get(ATTR_DEVICE_CLASS) self._attr_last_reset = sensor.get(ATTR_LAST_RESET) self._attr_state_class = sensor.get(ATTR_STATE_CLASS) @property - def state(self): + def state(self) -> str | None: """Return the state.""" if self.api.data: return self.api.data.get(self._condition) + return None diff --git a/homeassistant/components/goalzero/switch.py b/homeassistant/components/goalzero/switch.py index dd4c9deae3e..92808ef5f43 100644 --- a/homeassistant/components/goalzero/switch.py +++ b/homeassistant/components/goalzero/switch.py @@ -1,4 +1,6 @@ """Support for Goal Zero Yeti Switches.""" +from __future__ import annotations + from homeassistant.components.switch import SwitchEntity from homeassistant.const import CONF_NAME @@ -10,7 +12,7 @@ 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 = [ + async_add_entities( YetiSwitch( goalzero_data[DATA_KEY_API], goalzero_data[DATA_KEY_COORDINATOR], @@ -19,8 +21,7 @@ async def async_setup_entry(hass, entry, async_add_entities): entry.entry_id, ) for switch_name in SWITCH_DICT - ] - async_add_entities(switches) + ) class YetiSwitch(YetiEntity, SwitchEntity): @@ -36,27 +37,25 @@ class YetiSwitch(YetiEntity, SwitchEntity): ): """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): + def name(self) -> str: """Return the name of the switch.""" return f"{self._name} {self._condition_name}" @property - def unique_id(self): + def unique_id(self) -> str: """Return the unique id of the switch.""" return f"{self._server_unique_id}/{self._condition}" @property - def is_on(self): + def is_on(self) -> bool: """Return state of the switch.""" if self.api.data: return self.api.data[self._condition] - return None + return False async def async_turn_off(self, **kwargs): """Turn off the switch."""