mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Remove relative time sensor from cert_expiry (#42338)
This commit is contained in:
parent
890d740093
commit
7bcd92172a
@ -10,13 +10,11 @@ from homeassistant.const import (
|
|||||||
CONF_PORT,
|
CONF_PORT,
|
||||||
DEVICE_CLASS_TIMESTAMP,
|
DEVICE_CLASS_TIMESTAMP,
|
||||||
EVENT_HOMEASSISTANT_START,
|
EVENT_HOMEASSISTANT_START,
|
||||||
TIME_DAYS,
|
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.event import async_call_later
|
from homeassistant.helpers.event import async_call_later
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
from homeassistant.util import dt
|
|
||||||
|
|
||||||
from .const import DEFAULT_PORT, DOMAIN
|
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]
|
coordinator = hass.data[DOMAIN][entry.entry_id]
|
||||||
|
|
||||||
sensors = [
|
sensors = [
|
||||||
SSLCertificateDays(coordinator),
|
|
||||||
SSLCertificateTimestamp(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):
|
class SSLCertificateTimestamp(CertExpiryEntity):
|
||||||
"""Implementation of the Cert Expiry timestamp sensor."""
|
"""Implementation of the Cert Expiry timestamp sensor."""
|
||||||
|
|
||||||
|
@ -76,21 +76,22 @@ async def test_unload_config_entry(mock_now, hass):
|
|||||||
assert len(config_entries) == 1
|
assert len(config_entries) == 1
|
||||||
assert entry is config_entries[0]
|
assert entry is config_entries[0]
|
||||||
|
|
||||||
|
timestamp = future_timestamp(100)
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.cert_expiry.get_cert_expiry_timestamp",
|
"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
|
assert await async_setup_component(hass, DOMAIN, {}) is True
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert entry.state == ENTRY_STATE_LOADED
|
assert entry.state == ENTRY_STATE_LOADED
|
||||||
state = hass.states.get("sensor.cert_expiry_example_com")
|
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
|
||||||
assert state.state == "100"
|
assert state.state == timestamp.isoformat()
|
||||||
assert state.attributes.get("error") == "None"
|
assert state.attributes.get("error") == "None"
|
||||||
assert state.attributes.get("is_valid")
|
assert state.attributes.get("is_valid")
|
||||||
|
|
||||||
await hass.config_entries.async_unload(entry.entry_id)
|
await hass.config_entries.async_unload(entry.entry_id)
|
||||||
|
|
||||||
assert entry.state == ENTRY_STATE_NOT_LOADED
|
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
|
assert state is None
|
||||||
|
@ -34,13 +34,6 @@ async def test_async_setup_entry(mock_now, hass):
|
|||||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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")
|
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state != STATE_UNAVAILABLE
|
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)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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 is not None
|
||||||
assert state.state != STATE_UNAVAILABLE
|
assert state.state != STATE_UNAVAILABLE
|
||||||
assert state.state == "0"
|
|
||||||
assert state.attributes.get("error") == "some error"
|
assert state.attributes.get("error") == "some error"
|
||||||
assert not state.attributes.get("is_valid")
|
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()
|
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
|
assert state is None
|
||||||
|
|
||||||
|
|
||||||
@ -122,13 +114,6 @@ async def test_update_sensor(hass):
|
|||||||
assert await hass.config_entries.async_setup(entry.entry_id)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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")
|
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state != STATE_UNAVAILABLE
|
assert state.state != STATE_UNAVAILABLE
|
||||||
@ -144,13 +129,6 @@ async def test_update_sensor(hass):
|
|||||||
async_fire_time_changed(hass, utcnow() + timedelta(hours=24))
|
async_fire_time_changed(hass, utcnow() + timedelta(hours=24))
|
||||||
await hass.async_block_till_done()
|
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")
|
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state != STATE_UNAVAILABLE
|
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)
|
assert await hass.config_entries.async_setup(entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
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")
|
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state != STATE_UNAVAILABLE
|
assert state.state != STATE_UNAVAILABLE
|
||||||
@ -203,7 +174,7 @@ async def test_update_sensor_network_errors(hass):
|
|||||||
|
|
||||||
next_update = starting_time + timedelta(hours=48)
|
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
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
|
||||||
with patch("homeassistant.util.dt.utcnow", return_value=next_update), patch(
|
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))
|
async_fire_time_changed(hass, utcnow() + timedelta(hours=48))
|
||||||
await hass.async_block_till_done()
|
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 is not None
|
||||||
assert state.state != STATE_UNAVAILABLE
|
assert state.state != STATE_UNAVAILABLE
|
||||||
assert state.state == "98"
|
assert state.state == timestamp.isoformat()
|
||||||
assert state.attributes.get("error") == "None"
|
assert state.attributes.get("error") == "None"
|
||||||
assert state.attributes.get("is_valid")
|
assert state.attributes.get("is_valid")
|
||||||
|
|
||||||
next_update = starting_time + timedelta(hours=72)
|
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))
|
async_fire_time_changed(hass, utcnow() + timedelta(hours=72))
|
||||||
await hass.async_block_till_done()
|
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")
|
state = hass.states.get("sensor.cert_expiry_timestamp_example_com")
|
||||||
assert state is not None
|
assert state is not None
|
||||||
assert state.state == STATE_UNKNOWN
|
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))
|
async_fire_time_changed(hass, utcnow() + timedelta(hours=96))
|
||||||
await hass.async_block_till_done()
|
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
|
assert state.state == STATE_UNAVAILABLE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user