Use attr** in linode (#61882)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-20 14:17:23 +01:00 committed by GitHub
parent e689afc0b3
commit c18c58f560
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 55 deletions

View file

@ -4,8 +4,8 @@ import logging
import voluptuous as vol import voluptuous as vol
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOVING,
PLATFORM_SCHEMA, PLATFORM_SCHEMA,
BinarySensorDeviceClass,
BinarySensorEntity, BinarySensorEntity,
) )
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
@ -49,49 +49,34 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class LinodeBinarySensor(BinarySensorEntity): class LinodeBinarySensor(BinarySensorEntity):
"""Representation of a Linode droplet sensor.""" """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 def __init__(self, li, node_id): # pylint: disable=invalid-name
"""Initialize a new Linode sensor.""" """Initialize a new Linode sensor."""
self._linode = li self._linode = li
self._node_id = node_id self._node_id = node_id
self._state = None self._attr_extra_state_attributes = {}
self.data = None self._attr_name = 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
def update(self): def update(self):
"""Update state of sensor.""" """Update state of sensor."""
data = None
self._linode.update() self._linode.update()
if self._linode.data is not None: if self._linode.data is not None:
for node in self._linode.data: for node in self._linode.data:
if node.id == self._node_id: if node.id == self._node_id:
self.data = node data = node
if self.data is not None:
self._state = self.data.status == "running" if data is not None:
self._attrs = { self._attr_is_on = data.status == "running"
ATTR_CREATED: self.data.created, self._attr_extra_state_attributes = {
ATTR_NODE_ID: self.data.id, ATTR_CREATED: data.created,
ATTR_NODE_NAME: self.data.label, ATTR_NODE_ID: data.id,
ATTR_IPV4_ADDRESS: self.data.ipv4, ATTR_NODE_NAME: data.label,
ATTR_IPV6_ADDRESS: self.data.ipv6, ATTR_IPV4_ADDRESS: data.ipv4,
ATTR_MEMORY: self.data.specs.memory, ATTR_IPV6_ADDRESS: data.ipv6,
ATTR_REGION: self.data.region.country, ATTR_MEMORY: data.specs.memory,
ATTR_VCPUS: self.data.specs.vcpus, ATTR_REGION: data.region.country,
ATTR_VCPUS: data.specs.vcpus,
} }
self._name = self.data.label self._attr_name = data.label

View file

@ -51,24 +51,7 @@ class LinodeSwitch(SwitchEntity):
self._linode = li self._linode = li
self._node_id = node_id self._node_id = node_id
self.data = None self.data = None
self._state = None self._attr_extra_state_attributes = {}
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
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Boot-up the Node.""" """Boot-up the Node."""
@ -88,8 +71,8 @@ class LinodeSwitch(SwitchEntity):
if node.id == self._node_id: if node.id == self._node_id:
self.data = node self.data = node
if self.data is not None: if self.data is not None:
self._state = self.data.status == "running" self._attr_is_on = self.data.status == "running"
self._attrs = { self._attr_extra_state_attributes = {
ATTR_CREATED: self.data.created, ATTR_CREATED: self.data.created,
ATTR_NODE_ID: self.data.id, ATTR_NODE_ID: self.data.id,
ATTR_NODE_NAME: self.data.label, ATTR_NODE_NAME: self.data.label,
@ -99,4 +82,4 @@ class LinodeSwitch(SwitchEntity):
ATTR_REGION: self.data.region.country, ATTR_REGION: self.data.region.country,
ATTR_VCPUS: self.data.specs.vcpus, ATTR_VCPUS: self.data.specs.vcpus,
} }
self._name = self.data.label self._attr_name = self.data.label