mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Add camera platform to Dremel (#93882)
* Add camera platform to Dremel * unload and tests
This commit is contained in:
parent
5078bb3bef
commit
7f480849e2
@ -9,10 +9,10 @@ from homeassistant.const import CONF_HOST, Platform
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import CAMERA_MODEL, DOMAIN
|
||||||
from .coordinator import Dremel3DPrinterDataUpdateCoordinator
|
from .coordinator import Dremel3DPrinterDataUpdateCoordinator
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
PLATFORMS = [Platform.BINARY_SENSOR, Platform.CAMERA, Platform.SENSOR]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
||||||
@ -30,12 +30,19 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||||||
coordinator = Dremel3DPrinterDataUpdateCoordinator(hass, api)
|
coordinator = Dremel3DPrinterDataUpdateCoordinator(hass, api)
|
||||||
await coordinator.async_config_entry_first_refresh()
|
await coordinator.async_config_entry_first_refresh()
|
||||||
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
|
hass.data.setdefault(DOMAIN, {})[config_entry.entry_id] = coordinator
|
||||||
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
platforms = list(PLATFORMS)
|
||||||
|
if api.get_model() != CAMERA_MODEL:
|
||||||
|
platforms.remove(Platform.CAMERA)
|
||||||
|
await hass.config_entries.async_forward_entry_setups(config_entry, platforms)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Unload Dremel config entry."""
|
"""Unload Dremel config entry."""
|
||||||
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
platforms = list(PLATFORMS)
|
||||||
hass.data.pop(DOMAIN)
|
api: Dremel3DPrinter = hass.data[DOMAIN][entry.entry_id].api
|
||||||
|
if api.get_model() != CAMERA_MODEL:
|
||||||
|
platforms.remove(Platform.CAMERA)
|
||||||
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, platforms):
|
||||||
|
hass.data[DOMAIN].pop(entry.entry_id)
|
||||||
return unload_ok
|
return unload_ok
|
||||||
|
44
homeassistant/components/dremel_3d_printer/camera.py
Normal file
44
homeassistant/components/dremel_3d_printer/camera.py
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
"""Support for Dremel 3D45 Camera."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from homeassistant.components.camera import CameraEntityDescription
|
||||||
|
from homeassistant.components.mjpeg import MjpegCamera
|
||||||
|
from homeassistant.config_entries import ConfigEntry
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from . import Dremel3DPrinterDataUpdateCoordinator
|
||||||
|
from .const import DOMAIN
|
||||||
|
from .entity import Dremel3DPrinterEntity
|
||||||
|
|
||||||
|
CAMERA_TYPE = CameraEntityDescription(
|
||||||
|
key="camera",
|
||||||
|
name="Camera",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
async def async_setup_entry(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
config_entry: ConfigEntry,
|
||||||
|
async_add_entities: AddEntitiesCallback,
|
||||||
|
) -> None:
|
||||||
|
"""Set up a MJPEG IP Camera for the 3D45 Model. The 3D20 and 3D40 models don't have built in cameras."""
|
||||||
|
coordinator = hass.data[DOMAIN][config_entry.entry_id]
|
||||||
|
async_add_entities([Dremel3D45Camera(coordinator, CAMERA_TYPE)])
|
||||||
|
|
||||||
|
|
||||||
|
class Dremel3D45Camera(Dremel3DPrinterEntity, MjpegCamera):
|
||||||
|
"""Dremel 3D45 Camera."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coordinator: Dremel3DPrinterDataUpdateCoordinator,
|
||||||
|
description: CameraEntityDescription,
|
||||||
|
) -> None:
|
||||||
|
"""Initialize a new Dremel 3D Printer integration camera for the 3D45 model."""
|
||||||
|
super().__init__(coordinator, description)
|
||||||
|
MjpegCamera.__init__(
|
||||||
|
self,
|
||||||
|
mjpeg_url=coordinator.api.get_stream_url(),
|
||||||
|
still_image_url=coordinator.api.get_snapshot_url(),
|
||||||
|
)
|
@ -5,6 +5,8 @@ import logging
|
|||||||
|
|
||||||
LOGGER = logging.getLogger(__package__)
|
LOGGER = logging.getLogger(__package__)
|
||||||
|
|
||||||
|
CAMERA_MODEL = "3D45"
|
||||||
|
|
||||||
DOMAIN = "dremel_3d_printer"
|
DOMAIN = "dremel_3d_printer"
|
||||||
|
|
||||||
ATTR_EXTRUDER = "extruder"
|
ATTR_EXTRUDER = "extruder"
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
from requests.exceptions import ConnectTimeout
|
from requests.exceptions import ConnectTimeout
|
||||||
|
|
||||||
from homeassistant.components.dremel_3d_printer.const import DOMAIN
|
from homeassistant.components.dremel_3d_printer.const import DOMAIN
|
||||||
@ -14,20 +15,27 @@ import homeassistant.util.dt as dt_util
|
|||||||
|
|
||||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||||
|
|
||||||
|
MOCKED_MODEL = "homeassistant.components.dremel_3d_printer.Dremel3DPrinter.get_model"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("model", ["3D45", "3D20"])
|
||||||
async def test_setup(
|
async def test_setup(
|
||||||
hass: HomeAssistant, connection, config_entry: MockConfigEntry
|
hass: HomeAssistant, connection, config_entry: MockConfigEntry, model: str
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test load and unload."""
|
"""Test load and unload."""
|
||||||
|
with patch(MOCKED_MODEL, return_value=model) as mock:
|
||||||
await hass.config_entries.async_setup(config_entry.entry_id)
|
await hass.config_entries.async_setup(config_entry.entry_id)
|
||||||
assert await async_setup_component(hass, DOMAIN, {})
|
assert await async_setup_component(hass, DOMAIN, {})
|
||||||
assert config_entry.state == ConfigEntryState.LOADED
|
assert config_entry.state == ConfigEntryState.LOADED
|
||||||
|
assert mock.called
|
||||||
|
|
||||||
|
with patch(MOCKED_MODEL, return_value=model) as mock:
|
||||||
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
assert await hass.config_entries.async_unload(config_entry.entry_id)
|
||||||
await hass.async_block_till_done()
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||||
assert not hass.data.get(DOMAIN)
|
assert not hass.data.get(DOMAIN)
|
||||||
|
assert mock.called
|
||||||
|
|
||||||
|
|
||||||
async def test_async_setup_entry_not_ready(
|
async def test_async_setup_entry_not_ready(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user