Exemple #1
0
    def __init__(self, bot: object, blink_instance: object,
                 blink_auth_instance: object) -> None:
        """
        Initial Telegram receive class instance setup

        :param bot: Telegram bot class instance object
        :type bot: object
        :param blink_instance: blink cam class instance object
        :type blink_instance: object
        :param blink_auth_instance: blink cam auth class instance object
        :type blink_auth_instance: object
        """
        Configuration.__init__(self)
        self.logger = logging.getLogger("fdia_telegram")
        self.logger.debug("reading config")
        self.bot = bot
        self.blink = blink_instance
        self.auth = blink_auth_instance
        self.blink_json_data = {}
        self.switch_actions = {
            "foto": "self.rcv_msg_foto",
            "blink": "self.rcv_msg_blink",
            "blinkcam": "self.rcv_msg_blinkcam",
            "picam": "self.rcv_msg_picam",
        }
    def setUp(self):
        with open("test/expected_conf.json") as json_file:
            self.CONFIG_DICT = json.load(json_file)

        self.patcher_os_isfile = patch("common.config_util.os.path.isfile",
                                       return_value=False)
        self.patcher_os_path = patch("common.config_util.os.path.exists",
                                     return_value=False)

        self.mock_os_isfile = self.patcher_os_isfile.start()
        with self.assertLogs("config", level="DEBUG") as self.dl_log:
            self.instance_configuration = Configuration()
    def test_request_download_foto(self):
        """Tests the request get download foto.jpg file"""
        self.instance_configuration = Configuration()

        expected_download_log = [
            "INFO:PiCam:downloading PiCam foto",
            "DEBUG:PiCam:downloading foto ended with status 200",
            "DEBUG:PiCam:end downloading foto",
        ]
        with self.assertLogs(level="DEBUG") as self.log2:
            self.download_foto1 = request_download_foto(
                self.instance_configuration.picam_url,
                self.instance_configuration.picam_image_filename,
                self.instance_configuration.common_image_path,
            )
        self.assertEqual(self.log2.output, expected_download_log)
        # expected return status code
        self.assertEqual(self.download_foto1, 200)
        self.assertNotEqual(self.download_foto1, 400)
        self.assertNotEqual(self.download_foto1, 500)

        with patch("camera.picam.os.path.exists",
                   return_value=True) as mock_file_exist:
            with patch("camera.picam.os.remove",
                       return_value=True) as mock_file_remove:
                self.download_foto2 = request_download_foto(
                    self.instance_configuration.picam_url,
                    self.instance_configuration.picam_image_filename,
                    self.instance_configuration.common_image_path,
                )
        self.assertEqual(self.download_foto1, 200)
        mock_file_exist.assert_called_with("/tmp/foto.jpg")
        mock_file_remove.assert_called_with("/tmp/foto.jpg")
    def test_request_take_foto(self):
        """Tests function to take a PiCam API foto."""
        self.instance_configuration = Configuration()

        expected_log = [
            "INFO:PiCam:take a PiCam snapshot",
            f"DEBUG:PiCam:post url={self.instance_configuration.picam_url}",
            "DEBUG:PiCam:{'rotation': 0, 'width': 640, 'filename': 'foto.jpg', 'hight': 480, 'exposure': 'auto', 'iso': 0}",
            "DEBUG:PiCam:{'content-type': 'application/json'}",
            "DEBUG:PiCam:make a snapshot ended with http status 200",
        ]
        with self.assertLogs(level="DEBUG") as self.log1:
            self.take_foto = request_take_foto(
                self.instance_configuration.picam_url,
                self.instance_configuration.picam_image_width,
                self.instance_configuration.picam_image_hight,
                self.instance_configuration.picam_image_filename,
                self.instance_configuration.picam_exposure,
                self.instance_configuration.picam_rotation,
                self.instance_configuration.picam_iso,
            )
        self.assertEqual(self.log1.output, expected_log)
        # expected return status code
        self.assertEqual(self.take_foto, 200)
        self.assertNotEqual(self.take_foto, 400)
        self.assertNotEqual(self.take_foto, 500)
Exemple #5
0
    def __init__(self, bot: object, blink_instance: object,
                 blink_auth_instance: object) -> None:
        """
        Initial Telegram receive class instance setup

        :param bot: Telegram bot class instance object
        :type bot: object
        :param blink_instance: blink cam class instance object
        :type blink_instance: object
        :param blink_auth_instance: blink cam auth class instance object
        :type blink_auth_instance: object
        """
        Configuration.__init__(self)
        self.logger = logging.getLogger('fdia_telegram')
        self.logger.debug("reading config")
        self.bot = bot
        self.blink = blink_instance
        self.auth = blink_auth_instance
        self.blink_json_data = {}
Exemple #6
0
 def __init__(self, bot: object, blink_instance: object, blink_auth_instance: object) -> None:
     """
     Initial class definition.
     
     Reads from parent class config.yaml file its configuration into class attribute
     config dict and from there into multiple attributes.
     :param blink_instance: blink class instance object
     :type blink_instance: class object
     :param telegram_instance: telegram bot class instance
     :type telegram_instance: class object
     :return: Nothing adds class instance attribues
     :rtype: None
     """
     Configuration.__init__(self)
     self.logger = logging.getLogger('door-bell')
     self.logger.debug("reading config")
     self.bot = bot
     self.blink = blink_instance
     self.auth = blink_auth_instance
from __future__ import annotations
from common.config_util import Configuration
from camera import blink_cam, picam, cam_common
from messaging import send_msg
import logging
import time
import json

try:
    from gpiozero import Button
except:
    pass

from datetime import datetime

config = Configuration()

logger = logging.getLogger("door-bell")


class Door(Configuration):
    """Front door subclass of configuration config watches the door bell and
        triggers
       sends telegram message and camera photo.
    """
    def __init__(self, bot: object, blink_instance: object,
                 blink_auth_instance: object) -> None:
        """
        Initial class definition.
        
        Reads from parent class config.yaml file its configuration into class
class FDIaTestCase(unittest.TestCase):
    def setUp(self):
        with open("test/expected_conf.json") as json_file:
            self.CONFIG_DICT = json.load(json_file)

        self.patcher_os_isfile = patch("common.config_util.os.path.isfile",
                                       return_value=False)
        self.patcher_os_path = patch("common.config_util.os.path.exists",
                                     return_value=False)

        self.mock_os_isfile = self.patcher_os_isfile.start()
        with self.assertLogs("config", level="DEBUG") as self.dl_log:
            self.instance_configuration = Configuration()

    def tearDown(self):
        self.patcher_os_isfile.stop()

    def test_Configuration(self):
        self.mock_os_isfile.assert_called()
        self.assertEqual(self.instance_configuration.telegram_token,
                         "telegram_bot_token_code_here")
        self.assertEqual(self.instance_configuration.telegram_chat_nr, "-4321")
        self.assertEqual(self.instance_configuration.allowed_user_ids,
                         ["123456789", "987654321"])
        self.assertEqual(self.instance_configuration.otp_password,
                         "KRSXG5CQIASCIVZQOJCA====")
        self.assertEqual(self.instance_configuration.otp_length, 9)
        self.assertEqual(self.instance_configuration.otp_interval, 30)
        self.assertEqual(self.instance_configuration.hash_type, "sha512")
        self.assertEqual(self.instance_configuration.door_bell, 1)
        self.assertEqual(self.instance_configuration.door_summer, 2)
        self.assertEqual(
            self.instance_configuration.blink_username,
            "blink_account_email_address_here",
        )
        self.assertEqual(self.instance_configuration.blink_password,
                         "blink_account_password_here")
        self.assertEqual(self.instance_configuration.blink_name,
                         "Blink_Camera_name_here")
        self.assertEqual(
            self.instance_configuration.blink_config_file,
            self.instance_configuration.base_path + "blink_config.json",
        )
        self.assertEqual(self.instance_configuration.enable_detect_daylight,
                         False)
        self.assertEqual(self.instance_configuration.common_image_path,
                         "/tmp/foto.jpg")
        self.assertEqual(self.instance_configuration.common_camera_type,
                         "blink")
        self.assertEqual(self.instance_configuration.picam_url,
                         "http://IP:8000/foto/")
        self.assertEqual(self.instance_configuration.picam_image_width, 640)
        self.assertEqual(self.instance_configuration.picam_image_hight, 480)
        self.assertEqual(self.instance_configuration.picam_image_filename,
                         "foto.jpg")
        self.assertEqual(self.instance_configuration.picam_exposure, "auto")
        self.assertEqual(self.instance_configuration.picam_rotation, 0)
        self.assertEqual(self.instance_configuration.picam_iso, 0)
        self.assertEqual(self.instance_configuration.run_on_raspberry, True)
        self.assertEqual(self.instance_configuration.config, self.CONFIG_DICT)
        expected_log = [
            "DEBUG:config:checking if config.yaml file exists",
            "INFO:config:No config.yaml file detected. Using temeplate one.",
            "DEBUG:config:reading config {} file info dict".format(
                self.instance_configuration.base_path +
                "config_template.yaml"),
        ]
        self.assertEqual(self.dl_log.output, expected_log)

    def test_define_config_file_with_exception(self):
        self.patcher_os_path.start()
        self.assertEqual(
            self.instance_configuration.config_file,
            self.instance_configuration.base_path + "config_template.yaml",
        )
        with self.assertRaises(NameError):
            self.instance_configuration.define_config_file()
        # with pytest.raises(NameError) as excinfo:
        #     self.instance_configuration.define_config_file()
        # excinfo.match("No config file found!")
        self.patcher_os_path.stop()

    def test_read_config_file_exception(self):
        with patch("builtins.open") as patched_open:
            with pytest.raises(FileNotFoundError) as excinfo:
                patched_open.side_effect = FileNotFoundError()
                self.instance_configuration.read_config(
                    self.instance_configuration.config_file)
            excinfo.match("Could not find config file")

            with pytest.raises(YamlReadError) as excinfo:
                patched_open.side_effect = YamlReadError()
                self.instance_configuration.read_config(
                    self.instance_configuration.config_file)
            excinfo.match("a YAML error is occured during parsing file")