diff --git a/homeassistant/components/fritz/sensor.py b/homeassistant/components/fritz/sensor.py index e2df5dc6e8b..8aa48b216cb 100644 --- a/homeassistant/components/fritz/sensor.py +++ b/homeassistant/components/fritz/sensor.py @@ -20,6 +20,7 @@ from homeassistant.const import ( EntityCategory, UnitOfDataRate, UnitOfInformation, + UnitOfTemperature, ) from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback @@ -142,6 +143,13 @@ def _retrieve_link_attenuation_received_state( return status.attenuation[1] / 10 # type: ignore[no-any-return] +def _retrieve_cpu_temperature_state( + status: FritzStatus, last_value: float | None +) -> float: + """Return the first CPU temperature value.""" + return status.get_cpu_temperatures()[0] # type: ignore[no-any-return] + + @dataclass(frozen=True, kw_only=True) class FritzSensorEntityDescription(SensorEntityDescription, FritzEntityDescription): """Describes Fritz sensor entity.""" @@ -274,6 +282,16 @@ SENSOR_TYPES: tuple[FritzSensorEntityDescription, ...] = ( value_fn=_retrieve_link_attenuation_received_state, is_suitable=lambda info: info.wan_enabled and info.connection == DSL_CONNECTION, ), + FritzSensorEntityDescription( + key="cpu_temperature", + translation_key="cpu_temperature", + native_unit_of_measurement=UnitOfTemperature.CELSIUS, + device_class=SensorDeviceClass.TEMPERATURE, + entity_category=EntityCategory.DIAGNOSTIC, + state_class=SensorStateClass.MEASUREMENT, + value_fn=_retrieve_cpu_temperature_state, + is_suitable=lambda info: True, + ), ) diff --git a/homeassistant/components/fritz/strings.json b/homeassistant/components/fritz/strings.json index 75ce6800aab..87ee28196ad 100644 --- a/homeassistant/components/fritz/strings.json +++ b/homeassistant/components/fritz/strings.json @@ -174,6 +174,9 @@ }, "max_kb_s_sent": { "name": "Max connection upload throughput" + }, + "cpu_temperature": { + "name": "CPU Temperature" } } }, diff --git a/tests/components/fritz/conftest.py b/tests/components/fritz/conftest.py index fa92fa37c04..017328ea0eb 100644 --- a/tests/components/fritz/conftest.py +++ b/tests/components/fritz/conftest.py @@ -27,6 +27,26 @@ class FritzServiceMock(Service): self.serviceId = serviceId +class FritzResponseMock: + """Response mocking.""" + + def json(self): + """Mock json method.""" + return {"CPUTEMP": "69,68,67"} + + +class FritzHttpMock: + """FritzHttp mocking.""" + + def __init__(self) -> None: + """Init Mocking class.""" + self.router_url = "http://fritz.box" + + def call_url(self, *args, **kwargs): + """Mock call_url method.""" + return FritzResponseMock() + + class FritzConnectionMock: """FritzConnection mocking.""" @@ -39,6 +59,7 @@ class FritzConnectionMock: srv: FritzServiceMock(serviceId=srv, actions=actions) for srv, actions in services.items() } + self.http_interface = FritzHttpMock() LOGGER.debug("-" * 80) LOGGER.debug("FritzConnectionMock - services: %s", self.services) diff --git a/tests/components/fritz/snapshots/test_sensor.ambr b/tests/components/fritz/snapshots/test_sensor.ambr index 4efae5951e8..ac437b28d99 100644 --- a/tests/components/fritz/snapshots/test_sensor.ambr +++ b/tests/components/fritz/snapshots/test_sensor.ambr @@ -825,3 +825,59 @@ 'state': '3.4', }) # --- +# name: test_sensor_setup[sensor.mock_title_cpu_temperature-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.mock_title_cpu_temperature', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + 'sensor': dict({ + 'suggested_display_precision': 1, + }), + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'CPU Temperature', + 'platform': 'fritz', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'cpu_temperature', + 'unique_id': '1C:ED:6F:12:34:11-cpu_temperature', + 'unit_of_measurement': , + }) +# --- +# name: test_sensor_setup[sensor.mock_title_cpu_temperature-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'Mock Title CPU Temperature', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.mock_title_cpu_temperature', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '69', + }) +# ---