From 0ad5ad5ca7fb5702161401c372fe09e550f151e5 Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Fri, 15 Oct 2021 17:34:13 -0400 Subject: [PATCH] Don't use cast when possible for goalzero (#57742) * Don't use cast when possible for goalzero * tweak * tweak * tweak * Call first refresh on coordinator * don't use dict.get if not needed * tweak --- homeassistant/components/goalzero/__init__.py | 5 +++-- homeassistant/components/goalzero/binary_sensor.py | 4 ++-- homeassistant/components/goalzero/sensor.py | 5 +++-- homeassistant/components/goalzero/switch.py | 2 +- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/goalzero/__init__.py b/homeassistant/components/goalzero/__init__.py index 53daa29de8a..08ad70c0a79 100644 --- a/homeassistant/components/goalzero/__init__.py +++ b/homeassistant/components/goalzero/__init__.py @@ -68,6 +68,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: update_method=async_update_data, update_interval=MIN_TIME_BETWEEN_UPDATES, ) + await coordinator.async_config_entry_first_refresh() hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN][entry.entry_id] = { DATA_KEY_API: api, @@ -112,6 +113,6 @@ class YetiEntity(CoordinatorEntity): ATTR_IDENTIFIERS: {(DOMAIN, self._server_unique_id)}, ATTR_MANUFACTURER: "Goal Zero", ATTR_NAME: self._name, - ATTR_MODEL: self.api.sysdata.get(ATTR_MODEL), - ATTR_SW_VERSION: self.api.data.get("firmwareVersion"), + ATTR_MODEL: self.api.sysdata[ATTR_MODEL], + ATTR_SW_VERSION: self.api.data["firmwareVersion"], } diff --git a/homeassistant/components/goalzero/binary_sensor.py b/homeassistant/components/goalzero/binary_sensor.py index 0e61c7178bb..ddde8c80e96 100644 --- a/homeassistant/components/goalzero/binary_sensor.py +++ b/homeassistant/components/goalzero/binary_sensor.py @@ -82,5 +82,5 @@ class YetiBinarySensor(YetiEntity, BinarySensorEntity): @property def is_on(self) -> bool: - """Return if the service is on.""" - return cast(bool, self.api.data.get(self.entity_description.key) == 1) + """Return True if the service is on.""" + return cast(bool, self.api.data[self.entity_description.key] == 1) diff --git a/homeassistant/components/goalzero/sensor.py b/homeassistant/components/goalzero/sensor.py index bbf3fba753f..bd775ab82bb 100644 --- a/homeassistant/components/goalzero/sensor.py +++ b/homeassistant/components/goalzero/sensor.py @@ -31,6 +31,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback +from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import DataUpdateCoordinator from . import Yeti, YetiEntity @@ -170,6 +171,6 @@ class YetiSensor(YetiEntity, SensorEntity): self._attr_unique_id = f"{server_unique_id}/{description.key}" @property - def native_value(self) -> str: + def native_value(self) -> StateType: """Return the state.""" - return cast(str, self.api.data.get(self.entity_description.key)) + return cast(StateType, self.api.data[self.entity_description.key]) diff --git a/homeassistant/components/goalzero/switch.py b/homeassistant/components/goalzero/switch.py index 2932413465a..6c80a773a74 100644 --- a/homeassistant/components/goalzero/switch.py +++ b/homeassistant/components/goalzero/switch.py @@ -67,7 +67,7 @@ class YetiSwitch(YetiEntity, SwitchEntity): @property def is_on(self) -> bool: """Return state of the switch.""" - return cast(bool, self.api.data.get(self.entity_description.key)) + return cast(bool, self.api.data[self.entity_description.key] == 1) async def async_turn_off(self, **kwargs: Any) -> None: """Turn off the switch."""