mirror of
https://github.com/home-assistant/core.git
synced 2025-07-25 14:17:45 +00:00
Code quality improvements for goalzero (#53260)
This commit is contained in:
parent
2cf930f3bd
commit
930db7167e
@ -1,4 +1,6 @@
|
|||||||
"""The Goal Zero Yeti integration."""
|
"""The Goal Zero Yeti integration."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from goalzero import Yeti, exceptions
|
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.sensor import DOMAIN as DOMAIN_SENSOR
|
||||||
from homeassistant.components.switch import DOMAIN as DOMAIN_SWITCH
|
from homeassistant.components.switch import DOMAIN as DOMAIN_SWITCH
|
||||||
from homeassistant.config_entries import ConfigEntry
|
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.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
@ -41,7 +53,7 @@ async def async_setup_entry(hass, entry):
|
|||||||
try:
|
try:
|
||||||
await api.init_connect()
|
await api.init_connect()
|
||||||
except exceptions.ConnectError as ex:
|
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
|
raise ConfigEntryNotReady from ex
|
||||||
|
|
||||||
async def async_update_data():
|
async def async_update_data():
|
||||||
@ -88,23 +100,19 @@ class YetiEntity(CoordinatorEntity):
|
|||||||
self.api = api
|
self.api = api
|
||||||
self._name = name
|
self._name = name
|
||||||
self._server_unique_id = server_unique_id
|
self._server_unique_id = server_unique_id
|
||||||
self._device_class = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self) -> DeviceInfo:
|
||||||
"""Return the device information of the entity."""
|
"""Return the device information of the entity."""
|
||||||
info = {
|
model = sw_version = None
|
||||||
"identifiers": {(DOMAIN, self._server_unique_id)},
|
|
||||||
"manufacturer": "Goal Zero",
|
|
||||||
"name": self._name,
|
|
||||||
}
|
|
||||||
if self.api.sysdata:
|
if self.api.sysdata:
|
||||||
info["model"] = self.api.sysdata["model"]
|
model = self.api.sysdata[ATTR_MODEL]
|
||||||
if self.api.data:
|
if self.api.data:
|
||||||
info["sw_version"] = self.api.data["firmwareVersion"]
|
sw_version = self.api.data["firmwareVersion"]
|
||||||
return info
|
return {
|
||||||
|
ATTR_IDENTIFIERS: {(DOMAIN, self._server_unique_id)},
|
||||||
@property
|
ATTR_MANUFACTURER: "Goal Zero",
|
||||||
def device_class(self):
|
ATTR_NAME: self._name,
|
||||||
"""Return the class of this device."""
|
ATTR_MODEL: str(model),
|
||||||
return self._device_class
|
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."""
|
"""Set up the Goal Zero Yeti sensor."""
|
||||||
name = entry.data[CONF_NAME]
|
name = entry.data[CONF_NAME]
|
||||||
goalzero_data = hass.data[DOMAIN][entry.entry_id]
|
goalzero_data = hass.data[DOMAIN][entry.entry_id]
|
||||||
sensors = [
|
async_add_entities(
|
||||||
YetiBinarySensor(
|
YetiBinarySensor(
|
||||||
goalzero_data[DATA_KEY_API],
|
goalzero_data[DATA_KEY_API],
|
||||||
goalzero_data[DATA_KEY_COORDINATOR],
|
goalzero_data[DATA_KEY_COORDINATOR],
|
||||||
@ -21,8 +21,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
entry.entry_id,
|
entry.entry_id,
|
||||||
)
|
)
|
||||||
for sensor_name in BINARY_SENSOR_DICT
|
for sensor_name in BINARY_SENSOR_DICT
|
||||||
]
|
)
|
||||||
async_add_entities(sensors)
|
|
||||||
|
|
||||||
|
|
||||||
class YetiBinarySensor(YetiEntity, BinarySensorEntity):
|
class YetiBinarySensor(YetiEntity, BinarySensorEntity):
|
||||||
@ -47,23 +46,23 @@ class YetiBinarySensor(YetiEntity, BinarySensorEntity):
|
|||||||
self._device_class = variable_info[1]
|
self._device_class = variable_info[1]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return f"{self._name} {self._condition_name}"
|
return f"{self._name} {self._condition_name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self) -> str:
|
||||||
"""Return the unique id of the sensor."""
|
"""Return the unique id of the sensor."""
|
||||||
return f"{self._server_unique_id}/{self._condition_name}"
|
return f"{self._server_unique_id}/{self._condition_name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self) -> bool:
|
||||||
"""Return if the service is on."""
|
"""Return if the service is on."""
|
||||||
if self.api.data:
|
if self.api.data:
|
||||||
return self.api.data[self._condition] == 1
|
return self.api.data[self._condition] == 1
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self) -> str:
|
||||||
"""Icon to use in the frontend, if any."""
|
"""Icon to use in the frontend, if any."""
|
||||||
return self._icon
|
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."""
|
"""Handle a flow initiated by the user."""
|
||||||
errors = {}
|
errors = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
@ -98,7 +98,7 @@ class GoalZeroFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _async_try_connect(self, host):
|
async def _async_try_connect(self, host) -> tuple:
|
||||||
"""Try connecting to Goal Zero Yeti."""
|
"""Try connecting to Goal Zero Yeti."""
|
||||||
try:
|
try:
|
||||||
session = async_get_clientsession(self.hass)
|
session = async_get_clientsession(self.hass)
|
||||||
|
@ -34,10 +34,6 @@ from homeassistant.const import (
|
|||||||
ATTRIBUTION = "Data provided by Goal Zero"
|
ATTRIBUTION = "Data provided by Goal Zero"
|
||||||
ATTR_DEFAULT_ENABLED = "default_enabled"
|
ATTR_DEFAULT_ENABLED = "default_enabled"
|
||||||
|
|
||||||
CONF_IDENTIFIERS = "identifiers"
|
|
||||||
CONF_MANUFACTURER = "manufacturer"
|
|
||||||
CONF_MODEL = "model"
|
|
||||||
CONF_SW_VERSION = "sw_version"
|
|
||||||
DATA_KEY_COORDINATOR = "coordinator"
|
DATA_KEY_COORDINATOR = "coordinator"
|
||||||
DOMAIN = "goalzero"
|
DOMAIN = "goalzero"
|
||||||
DEFAULT_NAME = "Yeti"
|
DEFAULT_NAME = "Yeti"
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""Support for Goal Zero Yeti Sensors."""
|
"""Support for Goal Zero Yeti Sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.sensor import ATTR_LAST_RESET, ATTR_STATE_CLASS
|
from homeassistant.components.sensor import ATTR_LAST_RESET, ATTR_STATE_CLASS
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
ATTR_DEVICE_CLASS,
|
ATTR_DEVICE_CLASS,
|
||||||
@ -40,20 +42,19 @@ class YetiSensor(YetiEntity):
|
|||||||
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."""
|
"""Initialize a Goal Zero Yeti sensor."""
|
||||||
super().__init__(api, coordinator, name, server_unique_id)
|
super().__init__(api, coordinator, name, server_unique_id)
|
||||||
|
|
||||||
self._condition = sensor_name
|
self._condition = sensor_name
|
||||||
|
|
||||||
sensor = SENSOR_DICT[sensor_name]
|
sensor = SENSOR_DICT[sensor_name]
|
||||||
self._attr_name = f"{name} {sensor.get(ATTR_NAME)}"
|
self._attr_name = f"{name} {sensor.get(ATTR_NAME)}"
|
||||||
self._attr_unique_id = f"{self._server_unique_id}/{sensor_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_unit_of_measurement = sensor.get(ATTR_UNIT_OF_MEASUREMENT)
|
||||||
self._attr_entity_registry_enabled_default = sensor.get(ATTR_DEFAULT_ENABLED)
|
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_last_reset = sensor.get(ATTR_LAST_RESET)
|
||||||
self._attr_state_class = sensor.get(ATTR_STATE_CLASS)
|
self._attr_state_class = sensor.get(ATTR_STATE_CLASS)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self) -> str | None:
|
||||||
"""Return the state."""
|
"""Return the state."""
|
||||||
if self.api.data:
|
if self.api.data:
|
||||||
return self.api.data.get(self._condition)
|
return self.api.data.get(self._condition)
|
||||||
|
return None
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
"""Support for Goal Zero Yeti Switches."""
|
"""Support for Goal Zero Yeti Switches."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.switch import SwitchEntity
|
from homeassistant.components.switch import SwitchEntity
|
||||||
from homeassistant.const import CONF_NAME
|
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."""
|
"""Set up the Goal Zero Yeti switch."""
|
||||||
name = entry.data[CONF_NAME]
|
name = entry.data[CONF_NAME]
|
||||||
goalzero_data = hass.data[DOMAIN][entry.entry_id]
|
goalzero_data = hass.data[DOMAIN][entry.entry_id]
|
||||||
switches = [
|
async_add_entities(
|
||||||
YetiSwitch(
|
YetiSwitch(
|
||||||
goalzero_data[DATA_KEY_API],
|
goalzero_data[DATA_KEY_API],
|
||||||
goalzero_data[DATA_KEY_COORDINATOR],
|
goalzero_data[DATA_KEY_COORDINATOR],
|
||||||
@ -19,8 +21,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
entry.entry_id,
|
entry.entry_id,
|
||||||
)
|
)
|
||||||
for switch_name in SWITCH_DICT
|
for switch_name in SWITCH_DICT
|
||||||
]
|
)
|
||||||
async_add_entities(switches)
|
|
||||||
|
|
||||||
|
|
||||||
class YetiSwitch(YetiEntity, SwitchEntity):
|
class YetiSwitch(YetiEntity, SwitchEntity):
|
||||||
@ -36,27 +37,25 @@ class YetiSwitch(YetiEntity, SwitchEntity):
|
|||||||
):
|
):
|
||||||
"""Initialize a Goal Zero Yeti switch."""
|
"""Initialize a Goal Zero Yeti switch."""
|
||||||
super().__init__(api, coordinator, name, server_unique_id)
|
super().__init__(api, coordinator, name, server_unique_id)
|
||||||
|
|
||||||
self._condition = switch_name
|
self._condition = switch_name
|
||||||
|
|
||||||
self._condition_name = SWITCH_DICT[switch_name]
|
self._condition_name = SWITCH_DICT[switch_name]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self) -> str:
|
||||||
"""Return the name of the switch."""
|
"""Return the name of the switch."""
|
||||||
return f"{self._name} {self._condition_name}"
|
return f"{self._name} {self._condition_name}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unique_id(self):
|
def unique_id(self) -> str:
|
||||||
"""Return the unique id of the switch."""
|
"""Return the unique id of the switch."""
|
||||||
return f"{self._server_unique_id}/{self._condition}"
|
return f"{self._server_unique_id}/{self._condition}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self) -> bool:
|
||||||
"""Return state of the switch."""
|
"""Return state of the switch."""
|
||||||
if self.api.data:
|
if self.api.data:
|
||||||
return self.api.data[self._condition]
|
return self.api.data[self._condition]
|
||||||
return None
|
return False
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
"""Turn off the switch."""
|
"""Turn off the switch."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user