From c18c58f56063df2ba230d049580c9eb57ada2dcd Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 20 Dec 2021 14:17:23 +0100 Subject: [PATCH] Use attr** in linode (#61882) Co-authored-by: epenet --- .../components/linode/binary_sensor.py | 53 +++++++------------ homeassistant/components/linode/switch.py | 25 ++------- 2 files changed, 23 insertions(+), 55 deletions(-) diff --git a/homeassistant/components/linode/binary_sensor.py b/homeassistant/components/linode/binary_sensor.py index 9e482283042..46e9609aef8 100644 --- a/homeassistant/components/linode/binary_sensor.py +++ b/homeassistant/components/linode/binary_sensor.py @@ -4,8 +4,8 @@ import logging import voluptuous as vol from homeassistant.components.binary_sensor import ( - DEVICE_CLASS_MOVING, PLATFORM_SCHEMA, + BinarySensorDeviceClass, BinarySensorEntity, ) import homeassistant.helpers.config_validation as cv @@ -49,49 +49,34 @@ def setup_platform(hass, config, add_entities, discovery_info=None): class LinodeBinarySensor(BinarySensorEntity): """Representation of a Linode droplet sensor.""" - _attr_device_class = DEVICE_CLASS_MOVING + _attr_device_class = BinarySensorDeviceClass.MOVING def __init__(self, li, node_id): # pylint: disable=invalid-name """Initialize a new Linode sensor.""" self._linode = li self._node_id = node_id - self._state = None - self.data = None - self._attrs = {} - self._name = None - - @property - def name(self): - """Return the name of the sensor.""" - return self._name - - @property - def is_on(self): - """Return true if the binary sensor is on.""" - return self._state - - @property - def extra_state_attributes(self): - """Return the state attributes of the Linode Node.""" - return self._attrs + self._attr_extra_state_attributes = {} + self._attr_name = None def update(self): """Update state of sensor.""" + data = None self._linode.update() if self._linode.data is not None: for node in self._linode.data: if node.id == self._node_id: - self.data = node - if self.data is not None: - self._state = self.data.status == "running" - self._attrs = { - ATTR_CREATED: self.data.created, - ATTR_NODE_ID: self.data.id, - ATTR_NODE_NAME: self.data.label, - ATTR_IPV4_ADDRESS: self.data.ipv4, - ATTR_IPV6_ADDRESS: self.data.ipv6, - ATTR_MEMORY: self.data.specs.memory, - ATTR_REGION: self.data.region.country, - ATTR_VCPUS: self.data.specs.vcpus, + data = node + + if data is not None: + self._attr_is_on = data.status == "running" + self._attr_extra_state_attributes = { + ATTR_CREATED: data.created, + ATTR_NODE_ID: data.id, + ATTR_NODE_NAME: data.label, + ATTR_IPV4_ADDRESS: data.ipv4, + ATTR_IPV6_ADDRESS: data.ipv6, + ATTR_MEMORY: data.specs.memory, + ATTR_REGION: data.region.country, + ATTR_VCPUS: data.specs.vcpus, } - self._name = self.data.label + self._attr_name = data.label diff --git a/homeassistant/components/linode/switch.py b/homeassistant/components/linode/switch.py index cd2efe9c4da..4a45b3fd793 100644 --- a/homeassistant/components/linode/switch.py +++ b/homeassistant/components/linode/switch.py @@ -51,24 +51,7 @@ class LinodeSwitch(SwitchEntity): self._linode = li self._node_id = node_id self.data = None - self._state = None - self._attrs = {} - self._name = None - - @property - def name(self): - """Return the name of the switch.""" - return self._name - - @property - def is_on(self): - """Return true if switch is on.""" - return self._state - - @property - def extra_state_attributes(self): - """Return the state attributes of the Linode Node.""" - return self._attrs + self._attr_extra_state_attributes = {} def turn_on(self, **kwargs): """Boot-up the Node.""" @@ -88,8 +71,8 @@ class LinodeSwitch(SwitchEntity): if node.id == self._node_id: self.data = node if self.data is not None: - self._state = self.data.status == "running" - self._attrs = { + self._attr_is_on = self.data.status == "running" + self._attr_extra_state_attributes = { ATTR_CREATED: self.data.created, ATTR_NODE_ID: self.data.id, ATTR_NODE_NAME: self.data.label, @@ -99,4 +82,4 @@ class LinodeSwitch(SwitchEntity): ATTR_REGION: self.data.region.country, ATTR_VCPUS: self.data.specs.vcpus, } - self._name = self.data.label + self._attr_name = self.data.label