Code quality improvements for goalzero (#53260)
This commit is contained in:
parent
2cf930f3bd
commit
930db7167e
6 changed files with 46 additions and 43 deletions
|
@ -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),
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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."""
|
||||
|
|
Loading…
Add table
Reference in a new issue