def test_config_invalid_keys(self, mock_update): """Shouldn't store invalid keys in _config.""" config = copy(TEST_CONFIG) config.update({"a": "test_a", "b": "test_b", "c": "test_c"}) sensor = tcp.Sensor(self.hass, config) for invalid_key in "abc": assert invalid_key not in sensor._config
def test_name_not_set(self, mock_update): """Should return the superclass name property if not set in config.""" config = copy(TEST_CONFIG) del config[tcp.CONF_NAME] entity = Entity() sensor = tcp.Sensor(self.hass, config) assert sensor.name == entity.name
def test_config_uses_defaults(self, mock_update): """Should use defaults where appropriate.""" config = copy(TEST_CONFIG) for key in KEYS_AND_DEFAULTS.keys(): del config[key] sensor = tcp.Sensor(self.hass, config) for key, default in KEYS_AND_DEFAULTS.items(): assert sensor._config[key] == default
def test_update_renders_value_in_template(self, mock_select, mock_socket): """Should render the value in the provided template.""" test_value = "test_value" mock_socket = mock_socket().__enter__() mock_socket.recv.return_value = test_value.encode() config = copy(TEST_CONFIG) config[tcp.CONF_VALUE_TEMPLATE] = "{{ value }} {{ 1+1 }}" sensor = tcp.Sensor(self.hass, config) assert sensor._state == "%s 2" % test_value
def test_update_returns_if_template_render_fails(self, mock_select, mock_socket): """Should return None if rendering the template fails.""" test_value = "test_value" mock_socket = mock_socket().__enter__() mock_socket.recv.return_value = test_value.encode() config = copy(TEST_CONFIG) config[tcp.CONF_VALUE_TEMPLATE] = "{{ this won't work" sensor = tcp.Sensor(self.hass, config) assert sensor.update() is None
def test_update_receives_packet_and_sets_as_state(self, mock_select, mock_socket): """Test the response from the socket and set it as the state.""" test_value = "test_value" mock_socket = mock_socket().__enter__() mock_socket.recv.return_value = test_value.encode() config = copy(TEST_CONFIG) del config[tcp.CONF_VALUE_TEMPLATE] sensor = tcp.Sensor(self.hass, config) assert sensor._state == test_value
def test_config_valid_keys(self, *args): """Should store valid keys in _config.""" sensor = tcp.Sensor(self.hass, TEST_CONFIG) for key in TEST_CONFIG: assert key in sensor._config
def test_unit_of_measurement(self, mock_update): """Should return the configured unit of measurement.""" sensor = tcp.Sensor(self.hass, TEST_CONFIG) assert sensor.unit_of_measurement == TEST_CONFIG[tcp.CONF_UNIT]
def test_state(self, mock_update): """Should return the contents of _state.""" sensor = tcp.Sensor(self.hass, TEST_CONFIG) uuid = str(uuid4()) sensor._state = uuid assert sensor.state == uuid
def test_name(self, mock_update): """Should return the name if set in the config.""" sensor = tcp.Sensor(self.hass, TEST_CONFIG) assert sensor.name == TEST_CONFIG[tcp.CONF_NAME]
def test_update_calls_select_with_timeout(self, mock_select, mock_socket): """Should provide the timeout argument to select.""" tcp.Sensor(self.hass, TEST_CONFIG) mock_socket = mock_socket().__enter__() mock_select.assert_called_with([mock_socket], [], [], TEST_CONFIG[tcp.CONF_TIMEOUT])
def test_update_sends_payload(self, mock_select, mock_socket): """Should send the configured payload as bytes.""" tcp.Sensor(self.hass, TEST_CONFIG) mock_socket = mock_socket().__enter__() mock_socket.send.assert_called_with( TEST_CONFIG[tcp.CONF_PAYLOAD].encode())
def test_update_returns_if_select_fails(self, *args): """Should return if select fails to return a socket.""" with patch("blumate.components.sensor.tcp.Sensor.update"): sensor = tcp.Sensor(self.hass, TEST_CONFIG) assert sensor.update() is None
def test_update_returns_if_connecting_fails(self, *args): """Should return if connecting to host fails.""" with patch("blumate.components.sensor.tcp.Sensor.update"): sensor = tcp.Sensor(self.hass, TEST_CONFIG) assert sensor.update() is None
def test_update_connects_to_host_and_port(self, mock_select, mock_socket): """Should connect to the configured host and port.""" tcp.Sensor(self.hass, TEST_CONFIG) mock_socket = mock_socket().__enter__() assert mock_socket.connect.mock_calls[0][1] == (( TEST_CONFIG[tcp.CONF_HOST], TEST_CONFIG[tcp.CONF_PORT]), )
def test_init_calls_update(self, mock_update): """Should call update() method during __init__().""" tcp.Sensor(self.hass, TEST_CONFIG) assert mock_update.called