From 3eb3c2824c409feb9e2c68859fa6c04dd2574a7c Mon Sep 17 00:00:00 2001 From: Robert Hillis Date: Wed, 21 Jul 2021 14:52:17 -0400 Subject: [PATCH] Refactor goalzero (#53282) --- .../components/goalzero/binary_sensor.py | 28 +++++-------------- homeassistant/components/goalzero/const.py | 22 +++++++++------ homeassistant/components/goalzero/sensor.py | 8 +++--- homeassistant/components/goalzero/switch.py | 13 ++------- 4 files changed, 27 insertions(+), 44 deletions(-) diff --git a/homeassistant/components/goalzero/binary_sensor.py b/homeassistant/components/goalzero/binary_sensor.py index 74776eb51b5..f9a110eff55 100644 --- a/homeassistant/components/goalzero/binary_sensor.py +++ b/homeassistant/components/goalzero/binary_sensor.py @@ -1,6 +1,6 @@ """Support for Goal Zero Yeti Sensors.""" from homeassistant.components.binary_sensor import BinarySensorEntity -from homeassistant.const import CONF_NAME +from homeassistant.const import ATTR_DEVICE_CLASS, ATTR_ICON, ATTR_NAME, CONF_NAME from . import YetiEntity from .const import BINARY_SENSOR_DICT, DATA_KEY_API, DATA_KEY_COORDINATOR, DOMAIN @@ -39,21 +39,12 @@ class YetiBinarySensor(YetiEntity, BinarySensorEntity): super().__init__(api, coordinator, name, server_unique_id) self._condition = sensor_name - - variable_info = BINARY_SENSOR_DICT[sensor_name] - self._condition_name = variable_info[0] - self._icon = variable_info[2] - self._device_class = variable_info[1] - - @property - def name(self) -> str: - """Return the name of the sensor.""" - return f"{self._name} {self._condition_name}" - - @property - def unique_id(self) -> str: - """Return the unique id of the sensor.""" - return f"{self._server_unique_id}/{self._condition_name}" + self._attr_device_class = BINARY_SENSOR_DICT[sensor_name].get(ATTR_DEVICE_CLASS) + self._attr_icon = BINARY_SENSOR_DICT[sensor_name].get(ATTR_ICON) + self._attr_name = f"{name} {BINARY_SENSOR_DICT[sensor_name].get(ATTR_NAME)}" + self._attr_unique_id = ( + f"{server_unique_id}/{BINARY_SENSOR_DICT[sensor_name].get(ATTR_NAME)}" + ) @property def is_on(self) -> bool: @@ -61,8 +52,3 @@ class YetiBinarySensor(YetiEntity, BinarySensorEntity): if self.api.data: return self.api.data[self._condition] == 1 return False - - @property - def icon(self) -> str: - """Icon to use in the frontend, if any.""" - return self._icon diff --git a/homeassistant/components/goalzero/const.py b/homeassistant/components/goalzero/const.py index da4a6ee4ad6..e9fed7dc52b 100644 --- a/homeassistant/components/goalzero/const.py +++ b/homeassistant/components/goalzero/const.py @@ -18,6 +18,7 @@ from homeassistant.components.sensor import ( ) from homeassistant.const import ( ATTR_DEVICE_CLASS, + ATTR_ICON, ATTR_NAME, ATTR_UNIT_OF_MEASUREMENT, ELECTRIC_CURRENT_AMPERE, @@ -42,14 +43,19 @@ DATA_KEY_API = "api" MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=30) BINARY_SENSOR_DICT = { - "backlight": ["Backlight", None, "mdi:clock-digital"], - "app_online": [ - "App Online", - DEVICE_CLASS_CONNECTIVITY, - None, - ], - "isCharging": ["Charging", DEVICE_CLASS_BATTERY_CHARGING, None], - "inputDetected": ["Input Detected", DEVICE_CLASS_POWER, None], + "backlight": {ATTR_NAME: "Backlight", ATTR_ICON: "mdi:clock-digital"}, + "app_online": { + ATTR_NAME: "App Online", + ATTR_DEVICE_CLASS: DEVICE_CLASS_CONNECTIVITY, + }, + "isCharging": { + ATTR_NAME: "Charging", + ATTR_DEVICE_CLASS: DEVICE_CLASS_BATTERY_CHARGING, + }, + "inputDetected": { + ATTR_NAME: "Input Detected", + ATTR_DEVICE_CLASS: DEVICE_CLASS_POWER, + }, } SENSOR_DICT = { diff --git a/homeassistant/components/goalzero/sensor.py b/homeassistant/components/goalzero/sensor.py index f64d6d772c8..594e1f0046b 100644 --- a/homeassistant/components/goalzero/sensor.py +++ b/homeassistant/components/goalzero/sensor.py @@ -44,13 +44,13 @@ class YetiSensor(YetiEntity): 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._attr_device_class = sensor.get(ATTR_DEVICE_CLASS) + self._attr_entity_registry_enabled_default = sensor.get(ATTR_DEFAULT_ENABLED) self._attr_last_reset = sensor.get(ATTR_LAST_RESET) + self._attr_name = f"{name} {sensor.get(ATTR_NAME)}" self._attr_state_class = sensor.get(ATTR_STATE_CLASS) + self._attr_unique_id = f"{server_unique_id}/{sensor_name}" + self._attr_unit_of_measurement = sensor.get(ATTR_UNIT_OF_MEASUREMENT) @property def state(self) -> str | None: diff --git a/homeassistant/components/goalzero/switch.py b/homeassistant/components/goalzero/switch.py index 92808ef5f43..9d37bcb0b7b 100644 --- a/homeassistant/components/goalzero/switch.py +++ b/homeassistant/components/goalzero/switch.py @@ -38,17 +38,8 @@ 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) -> str: - """Return the name of the switch.""" - return f"{self._name} {self._condition_name}" - - @property - def unique_id(self) -> str: - """Return the unique id of the switch.""" - return f"{self._server_unique_id}/{self._condition}" + self._attr_name = f"{name} {SWITCH_DICT[switch_name]}" + self._attr_unique_id = f"{server_unique_id}/{switch_name}" @property def is_on(self) -> bool: