Remove relative time sensor from cert_expiry (#42338)

This commit is contained in:
Simone Chemelli 2020-11-13 13:15:37 +01:00 committed by GitHub
parent 890d740093
commit 7bcd92172a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 81 deletions

View file

@ -10,13 +10,11 @@ from homeassistant.const import (
CONF_PORT,
DEVICE_CLASS_TIMESTAMP,
EVENT_HOMEASSISTANT_START,
TIME_DAYS,
)
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.event import async_call_later
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt
from .const import DEFAULT_PORT, DOMAIN
@ -55,7 +53,6 @@ async def async_setup_entry(hass, entry, async_add_entities):
coordinator = hass.data[DOMAIN][entry.entry_id]
sensors = [
SSLCertificateDays(coordinator),
SSLCertificateTimestamp(coordinator),
]
@ -79,34 +76,6 @@ class CertExpiryEntity(CoordinatorEntity):
}
class SSLCertificateDays(CertExpiryEntity):
"""Implementation of the Cert Expiry days sensor."""
@property
def name(self):
"""Return the name of the sensor."""
return f"Cert Expiry ({self.coordinator.name})"
@property
def state(self):
"""Return the state of the sensor."""
if not self.coordinator.is_cert_valid:
return 0
expiry = self.coordinator.data - dt.utcnow()
return expiry.days
@property
def unique_id(self):
"""Return a unique id for the sensor."""
return f"{self.coordinator.host}:{self.coordinator.port}"
@property
def unit_of_measurement(self):
"""Return the unit this state is expressed in."""
return TIME_DAYS
class SSLCertificateTimestamp(CertExpiryEntity):
"""Implementation of the Cert Expiry timestamp sensor."""

View file

@ -76,21 +76,22 @@ async def test_unload_config_entry(mock_now, hass):
assert len(config_entries) == 1
assert entry is config_entries[0]
timestamp = future_timestamp(100)
with patch(
"homeassistant.components.cert_expiry.get_cert_expiry_timestamp",
return_value=future_timestamp(100),
return_value=timestamp,
):
assert await async_setup_component(hass, DOMAIN, {}) is True
await hass.async_block_till_done()
assert entry.state == ENTRY_STATE_LOADED
state = hass.states.get("sensor.cert_expiry_example_com")
assert state.state == "100"
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state.state == timestamp.isoformat()
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
await hass.config_entries.async_unload(entry.entry_id)
assert entry.state == ENTRY_STATE_NOT_LOADED
state = hass.states.get("sensor.cert_expiry_example_com")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is None

View file

@ -34,13 +34,6 @@ async def test_async_setup_entry(mock_now, hass):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
assert state.state == "100"
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
@ -65,10 +58,9 @@ async def test_async_setup_entry_bad_cert(hass):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
assert state.state == "0"
assert state.attributes.get("error") == "some error"
assert not state.attributes.get("is_valid")
@ -99,7 +91,7 @@ async def test_async_setup_entry_host_unavailable(hass):
):
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is None
@ -122,13 +114,6 @@ async def test_update_sensor(hass):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
assert state.state == "100"
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
@ -144,13 +129,6 @@ async def test_update_sensor(hass):
async_fire_time_changed(hass, utcnow() + timedelta(hours=24))
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
assert state.state == "99"
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
@ -178,13 +156,6 @@ async def test_update_sensor_network_errors(hass):
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
assert state.state == "100"
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
@ -203,7 +174,7 @@ async def test_update_sensor_network_errors(hass):
next_update = starting_time + timedelta(hours=48)
state = hass.states.get("sensor.cert_expiry_example_com")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state.state == STATE_UNAVAILABLE
with patch("homeassistant.util.dt.utcnow", return_value=next_update), patch(
@ -213,12 +184,12 @@ async def test_update_sensor_network_errors(hass):
async_fire_time_changed(hass, utcnow() + timedelta(hours=48))
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
assert state.state == "98"
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
assert state.state == timestamp.isoformat()
assert state.attributes.get("error") == "None"
assert state.attributes.get("is_valid")
next_update = starting_time + timedelta(hours=72)
@ -229,13 +200,6 @@ async def test_update_sensor_network_errors(hass):
async_fire_time_changed(hass, utcnow() + timedelta(hours=72))
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
assert state is not None
assert state.state != STATE_UNAVAILABLE
assert state.state == "0"
assert state.attributes.get("error") == "something bad"
assert not state.attributes.get("is_valid")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state is not None
assert state.state == STATE_UNKNOWN
@ -250,5 +214,5 @@ async def test_update_sensor_network_errors(hass):
async_fire_time_changed(hass, utcnow() + timedelta(hours=96))
await hass.async_block_till_done()
state = hass.states.get("sensor.cert_expiry_example_com")
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
assert state.state == STATE_UNAVAILABLE