Skip to content

Commit a6f54a6

Browse files
committed
Add reboot service
1 parent 926f106 commit a6f54a6

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

custom_components/yi_hack/camera.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
CONF_MQTT_PREFIX, CONF_PTZ,
2525
CONF_TOPIC_MOTION_DETECTION_IMAGE, DEFAULT_BRAND, DOMAIN,
2626
HTTP_TIMEOUT, LINK_HIGH_RES_STREAM, LINK_LOW_RES_STREAM,
27-
MSTAR, SERVICE_PTZ, SERVICE_SPEAK)
27+
MSTAR, SERVICE_MOVE_TO_PRESET, SERVICE_PTZ,
28+
SERVICE_REBOOT, SERVICE_SPEAK)
2829

2930
_LOGGER = logging.getLogger(__name__)
3031

@@ -71,7 +72,7 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_
7172
)
7273

7374
platform.async_register_entity_service(
74-
"move_to_preset",
75+
SERVICE_MOVE_TO_PRESET,
7576
{
7677
vol.Required("preset_id"): vol.All(int, vol.Range(min=0, max=14)),
7778
},
@@ -97,6 +98,12 @@ async def async_setup_entry(hass: HomeAssistant, config: ConfigEntry, async_add_
9798
"async_perform_speak",
9899
)
99100

101+
platform.async_register_entity_service(
102+
SERVICE_REBOOT,
103+
{},
104+
"async_perform_reboot",
105+
)
106+
100107
async_add_entities(
101108
[
102109
YiHackCamera(hass, config),
@@ -383,6 +390,24 @@ async def async_perform_speak(self, language, sentence):
383390

384391
await self.hass.async_add_executor_job(self._perform_speak, language, sentence)
385392

393+
def _perform_reboot(self):
394+
auth = None
395+
if self._user or self._password:
396+
auth = HTTPBasicAuth(self._user, self._password)
397+
398+
try:
399+
response = requests.get(f"http://{self._host}:{self._port}/cgi-bin/reboot.sh", timeout=HTTP_TIMEOUT, auth=auth)
400+
if response.status_code >= 300:
401+
_LOGGER.error(f"Failed to send reboot command to device {self._host}")
402+
except requests.exceptions.RequestException as error:
403+
_LOGGER.error(f"Failed to send reboot command to device {self._host}: error {error}")
404+
405+
async def async_perform_reboot(self):
406+
"""Reboot the camera."""
407+
_LOGGER.debug(f"Reboot the camera")
408+
409+
await self.hass.async_add_executor_job(self._perform_reboot)
410+
386411
@property
387412
def brand(self):
388413
"""Camera brand."""

custom_components/yi_hack/const.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
DEFAULT_EXTRA_ARGUMENTS = "-rtsp_transport tcp"
2424

2525
SERVICE_PTZ = "ptz"
26+
SERVICE_MOVE_TO_PRESET = "move_to_preset"
2627
SERVICE_SPEAK = "speak"
28+
SERVICE_REBOOT = "reboot"
2729

2830
HTTP_TIMEOUT = 10
2931

custom_components/yi_hack/icons.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"services": {
33
"ptz": "mdi:camera-control",
44
"move_to_preset": "mdi:image-outline",
5-
"speak": "mdi:speaker-message"
5+
"speak": "mdi:speaker-message",
6+
"reboot": "mdi:restart"
67
}
78
}

custom_components/yi_hack/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
"documentation": "https://github.com/roleoroleo/yi-hack_ha_integration",
88
"iot_class": "local_push",
99
"issue_tracker": "https://github.com/roleoroleo/yi-hack_ha_integration/issues",
10-
"version": "0.5.2"
10+
"version": "0.5.3"
1111
}

custom_components/yi_hack/services.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,17 @@ speak:
9494
example: "Hello world!"
9595
selector:
9696
text:
97+
98+
reboot:
99+
name: Reboot the cam
100+
description: Reboot the camera
101+
fields:
102+
entity_id:
103+
name: Entity id
104+
description: Name of entity to reboot.
105+
required: true
106+
example: "camera.living_room_camera"
107+
selector:
108+
entity:
109+
integration: yi_hack
110+
domain: camera

0 commit comments

Comments
 (0)