Use device_state_attributes() for platforms (#5649)

* Use device_state_attributes() for platforms

* Update test

* Fix lint issue
This commit is contained in:
Fabian Affolter 2017-01-30 16:16:49 +01:00 committed by GitHub
parent ca558f6485
commit 87764a51ba
10 changed files with 45 additions and 59 deletions

View file

@ -65,7 +65,7 @@ class DeutscheBahnSensor(Entity):
return self._state return self._state
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
connections = self.data.connections[0] connections = self.data.connections[0]
connections['next'] = self.data.connections[1]['departure'] connections['next'] = self.data.connections[1]['departure']

View file

@ -98,7 +98,7 @@ class GpsdSensor(Entity):
return STATE_UNKNOWN return STATE_UNKNOWN
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the GPS.""" """Return the state attributes of the GPS."""
return { return {
ATTR_LATITUDE: self.agps_thread.data_stream.lat, ATTR_LATITUDE: self.agps_thread.data_stream.lat,

View file

@ -83,7 +83,7 @@ class HddTempSensor(Entity):
return TEMP_FAHRENHEIT return TEMP_FAHRENHEIT
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
return { return {
ATTR_DEVICE: self.details[1], ATTR_DEVICE: self.details[1],

View file

@ -110,7 +110,7 @@ class HpIloSensor(Entity):
return self._state return self._state
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
return self._data return self._data

View file

@ -159,7 +159,7 @@ class EmailContentSensor(Entity):
return self._message return self._message
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return other state attributes for the message.""" """Return other state attributes for the message."""
return self._state_attributes return self._state_attributes

View file

@ -98,7 +98,7 @@ class LinuxBatterySensor(Entity):
return ICON return ICON
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
return { return {
ATTR_NAME: self._battery_stat.name, ATTR_NAME: self._battery_stat.name,

View file

@ -238,7 +238,7 @@ class MoldIndicator(Entity):
return self._state return self._state
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
if self._is_metric: if self._is_metric:
return { return {

View file

@ -116,7 +116,7 @@ class StatisticsSensor(Entity):
return False return False
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the sensor.""" """Return the state attributes of the sensor."""
if not self.is_binary: if not self.is_binary:
return { return {

View file

@ -112,7 +112,7 @@ class ZamgSensor(Entity):
return SENSOR_TYPES[self.variable][1] return SENSOR_TYPES[self.variable][1]
@property @property
def state_attributes(self): def device_state_attributes(self):
"""Return the state attributes.""" """Return the state attributes."""
return { return {
ATTR_WEATHER_ATTRIBUTION: ATTRIBUTION, ATTR_WEATHER_ATTRIBUTION: ATTRIBUTION,

View file

@ -46,8 +46,8 @@ class EmailContentSensor(unittest.TestCase):
def test_allowed_sender(self): def test_allowed_sender(self):
"""Test emails from allowed sender.""" """Test emails from allowed sender."""
test_message = email.message.Message() test_message = email.message.Message()
test_message['From'] = "sender@test.com" test_message['From'] = 'sender@test.com'
test_message['Subject'] = "Test" test_message['Subject'] = 'Test'
test_message['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57)
test_message.set_payload("Test Message") test_message.set_payload("Test Message")
@ -58,19 +58,20 @@ class EmailContentSensor(unittest.TestCase):
["sender@test.com"], ["sender@test.com"],
None) None)
sensor.entity_id = "sensor.emailtest" sensor.entity_id = 'sensor.emailtest'
sensor.update() sensor.update()
self.assertEqual("Test Message", sensor.state) self.assertEqual("Test Message", sensor.state)
self.assertEqual("sender@test.com", sensor.state_attributes["from"]) self.assertEqual('sender@test.com',
self.assertEqual("Test", sensor.state_attributes["subject"]) sensor.device_state_attributes['from'])
self.assertEqual('Test', sensor.device_state_attributes['subject'])
self.assertEqual(datetime.datetime(2016, 1, 1, 12, 44, 57), self.assertEqual(datetime.datetime(2016, 1, 1, 12, 44, 57),
sensor.state_attributes["date"]) sensor.device_state_attributes['date'])
def test_multi_part_with_text(self): def test_multi_part_with_text(self):
"""Test multi part emails.""" """Test multi part emails."""
msg = MIMEMultipart('alternative') msg = MIMEMultipart('alternative')
msg['Subject'] = "Link" msg['Subject'] = 'Link'
msg['From'] = "sender@test.com" msg['From'] = 'sender@test.com'
text = "Test Message" text = "Test Message"
html = "<html><head></head><body>Test Message</body></html>" html = "<html><head></head><body>Test Message</body></html>"
@ -82,11 +83,8 @@ class EmailContentSensor(unittest.TestCase):
msg.attach(htmlPart) msg.attach(htmlPart)
sensor = imap_email_content.EmailContentSensor( sensor = imap_email_content.EmailContentSensor(
self.hass, self.hass, FakeEMailReader(deque([msg])), 'test_emails_sensor',
FakeEMailReader(deque([msg])), ['sender@test.com'], None)
"test_emails_sensor",
["sender@test.com"],
None)
sensor.entity_id = "sensor.emailtest" sensor.entity_id = "sensor.emailtest"
sensor.update() sensor.update()
@ -107,11 +105,11 @@ class EmailContentSensor(unittest.TestCase):
sensor = imap_email_content.EmailContentSensor( sensor = imap_email_content.EmailContentSensor(
self.hass, self.hass,
FakeEMailReader(deque([msg])), FakeEMailReader(deque([msg])),
"test_emails_sensor", 'test_emails_sensor',
["sender@test.com"], ['sender@test.com'],
None) None)
sensor.entity_id = "sensor.emailtest" sensor.entity_id = 'sensor.emailtest'
sensor.update() sensor.update()
self.assertEqual( self.assertEqual(
"<html><head></head><body>Test Message</body></html>", "<html><head></head><body>Test Message</body></html>",
@ -120,8 +118,8 @@ class EmailContentSensor(unittest.TestCase):
def test_multi_part_only_other_text(self): def test_multi_part_only_other_text(self):
"""Test multi part emails with only other text.""" """Test multi part emails with only other text."""
msg = MIMEMultipart('alternative') msg = MIMEMultipart('alternative')
msg['Subject'] = "Link" msg['Subject'] = 'Link'
msg['From'] = "sender@test.com" msg['From'] = 'sender@test.com'
other = "Test Message" other = "Test Message"
@ -130,13 +128,10 @@ class EmailContentSensor(unittest.TestCase):
msg.attach(htmlPart) msg.attach(htmlPart)
sensor = imap_email_content.EmailContentSensor( sensor = imap_email_content.EmailContentSensor(
self.hass, self.hass, FakeEMailReader(deque([msg])), 'test_emails_sensor',
FakeEMailReader(deque([msg])), ['sender@test.com'], None)
"test_emails_sensor",
["sender@test.com"],
None)
sensor.entity_id = "sensor.emailtest" sensor.entity_id = 'sensor.emailtest'
sensor.update() sensor.update()
self.assertEqual("Test Message", sensor.state) self.assertEqual("Test Message", sensor.state)
@ -145,14 +140,14 @@ class EmailContentSensor(unittest.TestCase):
states = [] states = []
test_message1 = email.message.Message() test_message1 = email.message.Message()
test_message1['From'] = "sender@test.com" test_message1['From'] = 'sender@test.com'
test_message1['Subject'] = "Test" test_message1['Subject'] = 'Test'
test_message1['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message1['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57)
test_message1.set_payload("Test Message") test_message1.set_payload("Test Message")
test_message2 = email.message.Message() test_message2 = email.message.Message()
test_message2['From'] = "sender@test.com" test_message2['From'] = 'sender@test.com'
test_message2['Subject'] = "Test 2" test_message2['Subject'] = 'Test 2'
test_message2['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message2['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57)
test_message2.set_payload("Test Message 2") test_message2.set_payload("Test Message 2")
@ -164,18 +159,14 @@ class EmailContentSensor(unittest.TestCase):
states_received.set() states_received.set()
track_state_change( track_state_change(
self.hass, self.hass, ['sensor.emailtest'], state_changed_listener)
["sensor.emailtest"],
state_changed_listener)
sensor = imap_email_content.EmailContentSensor( sensor = imap_email_content.EmailContentSensor(
self.hass, self.hass,
FakeEMailReader(deque([test_message1, test_message2])), FakeEMailReader(deque([test_message1, test_message2])),
"test_emails_sensor", 'test_emails_sensor', ['sender@test.com'], None)
["sender@test.com"],
None)
sensor.entity_id = "sensor.emailtest" sensor.entity_id = 'sensor.emailtest'
sensor.update() sensor.update()
self.hass.block_till_done() self.hass.block_till_done()
@ -189,39 +180,34 @@ class EmailContentSensor(unittest.TestCase):
def test_sender_not_allowed(self): def test_sender_not_allowed(self):
"""Test not whitelisted emails.""" """Test not whitelisted emails."""
test_message = email.message.Message() test_message = email.message.Message()
test_message['From'] = "sender@test.com" test_message['From'] = 'sender@test.com'
test_message['Subject'] = "Test" test_message['Subject'] = 'Test'
test_message['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57)
test_message.set_payload("Test Message") test_message.set_payload("Test Message")
sensor = imap_email_content.EmailContentSensor( sensor = imap_email_content.EmailContentSensor(
self.hass, self.hass, FakeEMailReader(deque([test_message])),
FakeEMailReader(deque([test_message])), 'test_emails_sensor', ['other@test.com'], None)
"test_emails_sensor",
["other@test.com"],
None)
sensor.entity_id = "sensor.emailtest" sensor.entity_id = 'sensor.emailtest'
sensor.update() sensor.update()
self.assertEqual(None, sensor.state) self.assertEqual(None, sensor.state)
def test_template(self): def test_template(self):
"""Test value template.""" """Test value template."""
test_message = email.message.Message() test_message = email.message.Message()
test_message['From'] = "sender@test.com" test_message['From'] = 'sender@test.com'
test_message['Subject'] = "Test" test_message['Subject'] = 'Test'
test_message['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57) test_message['Date'] = datetime.datetime(2016, 1, 1, 12, 44, 57)
test_message.set_payload("Test Message") test_message.set_payload("Test Message")
sensor = imap_email_content.EmailContentSensor( sensor = imap_email_content.EmailContentSensor(
self.hass, self.hass, FakeEMailReader(deque([test_message])),
FakeEMailReader(deque([test_message])), 'test_emails_sensor', ['sender@test.com'],
"test_emails_sensor",
["sender@test.com"],
Template("{{ subject }} from {{ from }} with message {{ body }}", Template("{{ subject }} from {{ from }} with message {{ body }}",
self.hass)) self.hass))
sensor.entity_id = "sensor.emailtest" sensor.entity_id = 'sensor.emailtest'
sensor.update() sensor.update()
self.assertEqual( self.assertEqual(
"Test from sender@test.com with message Test Message", "Test from sender@test.com with message Test Message",