Fix linode I/O in state property (#15010)

* Fix linode I/O in state property

* Move update of all attrs to update
pull/13562/merge
Martin Hjelmare 2018-06-18 15:21:41 +02:00 committed by Paulus Schoutsen
parent 7bfa81c592
commit f9a21dbfda
2 changed files with 35 additions and 33 deletions

View File

@ -52,19 +52,18 @@ class LinodeBinarySensor(BinarySensorDevice):
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."""
if self.data is not None:
return self.data.label
return self._name
@property
def is_on(self):
"""Return true if the binary sensor is on."""
if self.data is not None:
return self.data.status == 'running'
return False
return self._state
@property
def device_class(self):
@ -74,8 +73,18 @@ class LinodeBinarySensor(BinarySensorDevice):
@property
def device_state_attributes(self):
"""Return the state attributes of the Linode Node."""
if self.data:
return {
return self._attrs
def update(self):
"""Update state of sensor."""
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,
@ -85,12 +94,4 @@ class LinodeBinarySensor(BinarySensorDevice):
ATTR_REGION: self.data.region.country,
ATTR_VCPUS: self.data.specs.vcpus,
}
return {}
def update(self):
"""Update state of sensor."""
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
self._name = self.data.label

View File

@ -51,35 +51,23 @@ class LinodeSwitch(SwitchDevice):
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."""
if self.data is not None:
return self.data.label
return self._name
@property
def is_on(self):
"""Return true if switch is on."""
if self.data is not None:
return self.data.status == 'running'
return False
return self._state
@property
def device_state_attributes(self):
"""Return the state attributes of the Linode Node."""
if self.data:
return {
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,
}
return {}
return self._attrs
def turn_on(self, **kwargs):
"""Boot-up the Node."""
@ -98,3 +86,16 @@ class LinodeSwitch(SwitchDevice):
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,
}
self._name = self.data.label