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

* Fix linode I/O in state property

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

View file

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